简体   繁体   English

PHPmailer:从表单发送

[英]PHPmailer: Send from form

Alright, I have my form (first snippet of code), and I am trying to use PHPmailer to send it. 好了,我有我的表单(第一段代码),并且我正在尝试使用PHPmailer发送它。 However, it sends the message without any of the information from the actual form. 但是,它发送的消息中没有来自实际表格的任何信息。 I am pretty lost with how to get this to work. 我对如何使它正常工作感到迷茫。

<form action="send_form_email.php" method="post" id="ContactForm">
                    <fieldset>
              <p class="email">magazines/newspapers</p>
                        <ol>
                            <li>
                                <label for=name>Name</label>
                                <input id="name" name="name" type="text" placeholder="name" required autofocus>
                            </li>


                            <li>
                                <label for=email>Email</label>
                                <input id="email" name="email" type=email placeholder="example@domain.com" required>
                            </li>

                            <li>
                                <label for=telephone>Phone</label>
                                <input id=telephone name=telephone type=tel placeholder="Eg. 888-555-5555" required>
                            </li>

                            <li>
                            <label for="comments">note</label> 
                <textarea name=comments type=text placeholder="enter your comments" required></textarea>
                            </li>

                            <li>
                            <label for="file">File</label>
                            <input id="file" type="file" name="file" />
                            </li>

                        </ol>
                    </fieldset>

              <fieldset>
                        <button type=submit>submit</button>
                    </fieldset>

                </form>

Mail Script: 邮件脚本:

require("mail/class.phpmailer.php");

$mail = new PHPMailer();
$mail->Host = "localhost";               
$mail->From = "xxxxxx@gmail.com";
$mail->FromName  =  "Your Name";
$mail->AddAddress("xxxxxxx@gmail.com");


$mail->Subject = "Feedback form results";
$mail->Body = $comments;
$mail->WordWrap = 50;

if(!$mail->Send())
{
   echo 'Message was not sent.';
   echo 'Mailer error: ' . $mail->ErrorInfo;
}
else
{
   echo 'Thank you for your feedback.';
}
  $email = $_REQUEST['email'] ;
  $comments = $_POST['telephone'] ;
    $phone = $_REQUEST['comments'] ;
  $message = $_REQUEST['message'] ;

Ok so step one (optional) is to collect the posted variables into local variables - BEFORE you get into the $mail=new PHPMailer()... bit. 好的,第一步(可选)是将发布的变量收集到局部变量中- 进入$mail=new PHPMailer()...位之前。 This isn't necessary for the limited code fragment you provide, but you might use them somewhere else. 对于您提供的有限代码片段来说,这不是必需的,但是您可以在其他地方使用它们。

$name = $_POST['name'] ;
$email = $_REQUEST['email'] ;
$telephone = $_REQUEST['telephone'] ;
$comments = $_POST['comments'] ;

And now, change the $mail->Body = $comments; 现在,更改$mail->Body = $comments; line to: 行到:

$mail->Body="
Name: $name
Email: $email
Telephone: $telephone
Comments: $comments";

And as ngroot points out; 正如ngroot指出的那样; to add an attachment: 添加附件:

$mail->AddAttachment($_FILES['file']['tmp_name']);

... which you can call multiple times for multiple attachments. ...,您可以多次调用多个附件。 Because of the way form-uploads work (files get stored in a temporary space) you need to use this tmp_name sub variable. 由于表单上传的工作方式(文件存储在临时空间中),您需要使用此tmp_name子变量。 You'll also need to add multipart form encoding to allow file uploads, so the form line should read: 您还需要添加多部分表单编码以允许文件上传,因此表单行应显示为:

<form enctype="multipart/form-data" action="send_form_email.php" method="post" id="ContactForm"  >

似乎您发送消息正在设置$ comments变量。

You're setting the variables after trying to send the mail. 您尝试发送邮件后设置变量。 Also, I don't see a form input named message . 另外,我没有看到名为message的表单输入。 However, I do see one named file though the form's enctype is not set. 但是,尽管未设置表单的enctype,但我确实看到了一个命名file So there are many errors that need fixing. 因此,有许多错误需要修复。

Not sure what you're trying to do but it seems you don't need both comments and message . 不知道您要做什么,但似乎您既不需要comments也不需要message Remove message from the sending script and remove file from the html form to see if you can get it working like this. 从发送脚本中删除message ,并从html表单中删除file ,以查看是否可以使它像这样工作。 Also move the variable assignments from the bottom of the sending script to the top. 还要将变量分配从发送脚本的底部移到顶部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM