【问题标题】:Problems with contact form, phpmailer() and gmailadress联系表格、phpmailer() 和 gmailadress 的问题
【发布时间】: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


【解决方案1】:

正如 Mike 所说,您使用了一些非常奇怪的值。关注 PHPMailer repo 中的this example,它本质上涉及这些设置:

$mail->Host       = 'smtp.gmail.com';
$mail->Port       = 587;
$mail->SMTPSecure = 'tls';

您还使用旧版本的 PHPMailer - 自该版本以来已经修复了很多错误,所以get the latest

否则,简单的连接失败通常归结为 DNS 或本地网络问题,而不是 PHPMailer 中的任何问题。

【讨论】:

    【解决方案2】:

    感谢大家的帮助,现在邮件工作正常!我的端口和主机错误(我询问了我的提供商)。

    还有什么,你们知道发送邮件后如何返回网页吗?我认为这并不难,但我很难做到......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-15
      • 2016-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多