PHPmailer:从表单发送 [英] PHPmailer: Send from form
问题描述
好的,我有我的表单(第一段代码),并且我正在尝试使用PHPmailer发送它.但是,它发送的消息中没有来自实际表格的任何信息.我对如何使它正常工作感到迷茫.
Alright, I have my form (first snippet of code), and I am trying to use PHPmailer to send it. 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>
邮件脚本:
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'] ;
推荐答案
好,所以第一步(可选)是将发布的变量收集到局部变量中-之前,您进入了$mail=new PHPMailer()...
位.对于您提供的有限代码片段来说,这不是必需的,但是您可以在其他地方使用它们.
Ok so step one (optional) is to collect the posted variables into local variables - BEFORE you get into the $mail=new PHPMailer()...
bit. 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'] ;
现在,将$mail->Body = $comments;
行更改为:
$mail->Body="
Name: $name
Email: $email
Telephone: $telephone
Comments: $comments";
ngroot 指出;添加附件:
$mail->AddAttachment($_FILES['file']['tmp_name']);
...,您可以多次调用多个附件.由于表单上载的工作方式(文件存储在临时空间中),您需要使用此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. 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" >
这篇关于PHPmailer:从表单发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!