【问题标题】:PHP upload file to a contact form and email itPHP将文件上传到联系表格并通过电子邮件发送
【发布时间】:2021-04-16 00:55:39
【问题描述】:

我需要在联系表单中添加一个部分,供人们上传他们的信息(PDF 或图像 (JPG/PNG/ETC))。我得到了上传文件的部分,但它没有通过电子邮件发送 PDF(电子邮件工作正常,但它在没有上传文件的情况下到达,我不知道为什么)。我会把我的 HTML/PHP 代码放在上面,所以如果你能帮助我知道问题出在哪里或者我如何实现上传文件,那就太好了!

HTML

<form class="mb-2" action="freetest.php"  onsubmit="return validateForm();" method="post">
                <div class="form-group">
                    <div>
                        <label class="mb-0">Name</label>
                    </div>
                    <div class="form-group">
                        <input class="form-control-input" type="text" id="firstName" name="firstName" placeholder="Your Name" required>
                    </div>
                </div>

                <div class="form-group">
                    <div>
                        <label class="mb-0">Last Name</label>
                    </div>
                    <div class="form-group">
                        <input class="form-control-input" type="text" id="lastName" name="lastName" placeholder="Amelie" required>
                    </div>
                </div>

                <div class="form-group">
                    <div>
                        <label class="mb-0">Mail</label>
                    </div>
                    <div>
                        <input class="form-control-input" type="text" name="email" id="yourEmail" placeholder="Example@gmail.com" required>
                    </div>
                </div>
                <div class=form-group>
                <label class="mb-0">¿Any Question?</label>
                <textarea class="form-control-textarea" rows="8" name="message" required></textarea>
                </div>
                    <div class="form-group">
                        <div>
                            <label for="file-upload" class="custom-file-upload mt-2"><i class="fas fa-cloud-upload-alt"></i> Upload File</label>
                            <input id="file-upload" type="file" name="file" multiple="multiple" required>
                        </div>
                    </div>
                <button type="submit" class="btn-solid-lg">Send</button>
            </form>

PHP

 <?php
$ToEmail = 'mymail@gmail.com'; 
        $EmailSubject = 'Free test'; 
        $mailheader = "From: ".$_POST["lastName"]."\r\n"; 
        $mailheader .= "Reply-To: ".$_POST["email"]."\r\n"; 
        $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
        $MESSAGE_BODY = "First Name: ".$_POST["firstName"]."<br/>"; 
        $MESSAGE_BODY .= "Last Name: ".$_POST["lastName"]."<br/>"; 
        $MESSAGE_BODY .= "Email: ".$_POST["email"]."<br/>"; 
        $MESSAGE_BODY .= "Comment: ".nl2br($_POST["message"]).""; 
        mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
?>

【问题讨论】:

  • 如果您想使用mail()功能,请查看回复
  • 我建议使用 PHP Mailer
  • 我在任何地方都看不到&lt;input type='file' /&gt;。无论如何,您可能都希望动态创建该元素,然后单击它而不看到它。一旦你有一个,你将需要FormDateInstance.append(yourFile)。伪代码:const fd = new FormData, upload = document.querySelector('input[type=file]'); upload.onchange = function(){ if(this.files){ const file = this.files[0], name = file.name, dot = name.lastIndexOf('.'); if(dot !== -1 &amp;&amp; name.slice(dot).toLowerCase() === '.pdf'){ fd.append('fileGlobal', this.files[0], 'uploadName.pdf'); } } }。以$_FILES['fileGlobal'] 访问。
  • 关于我的最后一条评论,顺便说一下,您通过XMLHttpRequest 发送FormData

标签: javascript java php html email


【解决方案1】:

但它没有上传文件就到了,我不知道为什么

我可以告诉你为什么:

您不要将文件附加到邮件中。

您只发送文本。

有关如何执行此操作的一些解决方案,请访问此线程,该线程将解释如何使用和不使用 phpmailer 进行此操作

Send attachments with PHP Mail()?

【讨论】:

    【解决方案2】:

    希望对您有所帮助。我将对其进行测试并成功发送带有 .zip 文件附件的邮件。

    $my_file = "somefile.zip";
    $my_path = "/your_path/to_the_attachment/";
    $my_name = "Olaf Lederer";
    $my_mail = "my@mail.com";
    $my_replyto = "my_reply_to@mail.net";
    $my_subject = "This is a mail with attachment.";
    $my_message = "Hallo,rndo you like this script? I hope it will help.rnrngr. Olaf";
    mail_attachment($my_file, $my_path, "recipient@mail.org", $my_mail, $my_name, $my_replyto, $my_subject, $my_message);
    

    【讨论】:

      猜你喜欢
      • 2014-03-27
      • 1970-01-01
      • 2012-02-25
      • 1970-01-01
      • 1970-01-01
      • 2020-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多