我正在尝试设置 php mailer,以便在有人用信息填写我的表单时向我发送电子邮件。目前它是文件"index.php"上的单页网站
这是我的 php:
<?php
include("includes/class.phpmailer.php");
$mail = new PHPMailer;
$mail->setFrom($email, $name);
$mail->addAddress("myemail@gmail.com"); // Add a recipient
$mail->addReplyTo($email, $name);
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Message From ' . $name;
$mail->Body = "
<h2>Message Details</h2>
<p>Name: {$name} </p>
<p>Email: {$email} </p>
<p>Phone: {$phone} </p>
";
?>
请注意,为了隐私,我删除了我的电子邮件。
以下是我在同一个"索引.php"文件中的表单
<form id="contact" method="post" action="index.php">
<ul>
<h2>Contact</h2>
<li><input type="text" id="name" name="name" placeholder="Your Name" value="<?php echo $name; ?>"></li>
<li><input type="email" id="email" name="email" placeholder="Email Address" value="<?php echo $email; ?>"></li>
<li><input type="phone" id="phone" name="phone" placeholder="Phone Number" value="<?php echo $phone; ?>"></li>
<li><textarea name="message" id="message" placeholder="Your Message"> </textarea></li>
<li><input type="submit" id="submit" name="submit"></li>
</ul>
</form>
对于我的生活,我无法弄清楚为什么这不起作用,并希望得到任何帮助。谢谢
为了使此表单正常工作,您需要添加
$mail->send()
如果您简单谷歌,或者使用这些论坛大量关于表单等的类似问题,则有许多表单模板可用。
解析字段的最简单方法是,您可以在此处阅读大约 _POST 美元 http://php.net/manual/en/reserved.variables.post.php
$name = $_POST['name'];
您可能会发现本主题对于通读也很有用,这将使您更清楚地了解通过 phpmailer 发送表单数据。
PHPmailer:从表单发送
您需要解析POST
请求,这里有一种方法可以做到这一点:
if(!empty($_POST['name'] && !empty($_POST['email'] && !empty($_POST['phone']){
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
include("includes/class.phpmailer.php");
//the rest of you code ...
//and finally $mail->Send()
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
}
笔记:
看看一些 PHPMailer 示例
要实际发送消息,请在拥有主题、正文、addAddress 等后调用 Send() 函数:
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
在您的index.php
中,仅当您具有有效的 POST 值时才执行电子邮件。另外,我没有看到您实际上通过$mail->send()
方法发送电子邮件。
它应该是这样的:
if (count($_POST) > 0 && isset($_POST['submit'])) {
include("includes/class.phpmailer.php");
$mail = new PHPMailer;
$mail->setFrom($email, $name);
$mail->addAddress("myemail@gmail.com"); // Add a recipient
$mail->addReplyTo($email, $name);
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Message From ' . $name;
$mail->Body = "
<h2>Message Details</h2>
<p>Name: {$name} </p>
<p>Email: {$email} </p>
<p>Phone: {$phone} </p>
";
}
我没有看到
$mail->send()
但是我看到PHP脚本没有尽头。你没有忘记吗?
如果你有,将$SMTPDebug
设置为大于 0 并查找 phpmailer 日志中的错误
如果您
仍然无法发送电子邮件,则需要查看服务器上的托管公司电子邮件设置。很多时候,如果您使用的是流行的gmail smtp
,则需要检查smtp
设置。
您可能需要致电他们并获得一些支持,了解您为什么无法发送电子邮件。这是一个常见问题。
下面是一个使用 phpmailer 进行gmail SMTP
设置的示例。
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "yourusername@gmail.com"; // GMAIL username
$mail->Password = "yourpassword"; // GMAIL password