【发布时间】:2014-08-06 20:11:57
【问题描述】:
我的联系表格有问题,因此我读到 phpmailer 非常有用。不幸的是,我已经被困了几个小时,我不知道如何再进一步。
我想测试我是否可以发送电子邮件(之后我想将电子邮件连接到表单提交,但到目前为止没有成功。我正在使用 PHPmailer 5.2.0,我使用下面的代码(同时使用用于测试的 WAMPSERVER。有人知道我应该调整什么吗?
我已经使用了很多示例(包括来自 stackoverflow,但我只是遗漏了一些东西。你看到出了什么问题吗?每次我收到错误时:
SMTP -> ERROR: Failed to connect to server: php_network_getaddresses: getaddrinfo failed: Er is een onherstelbare fout opgetreden tijdens het zoeken in de database. (0)
SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host.
提前致谢!!
<?php
require_once('class.phpmailer.php');
include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer();
$body = file_get_contents('contactformulier.html');
//$body = eregi_replace("[\]",'',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "test@gmail.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->Host = "test@gmail.com"; // sets the SMTP server
$mail->Port = 26; // set the SMTP port for the GMAIL server
$mail->Username = "test@gmail.com"; // SMTP account username
$mail->Password = "password"; // SMTP account password
$mail->SetFrom('test@gmail.com', 'First Last');
$mail->AddReplyTo("test@gmail.com","First Last");
$mail->Subject = "PHPMailer Test Subject via smtp, basic with authentication";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "test@gmail.com";
$mail->AddAddress($address, "John Doe");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
【问题讨论】:
-
您的邮件主机名错误,AFAIK Gmail 不接受端口 26 上的连接。PHPMailer 网站上有一个sample code snippet,显示如何连接到 Gmail。我建议你照着做。
-
感谢您帮助迈克,我也尝试了那个,但这导致更多错误...您对 AFAIK 是什么意思?
-
worxware 站点已过期 - 使用当前存储库中的示例:github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps
标签: php gmail phpmailer contact-form