phpmailer 6.0成功消息但没有收到使用gmail作为中继的邮件

时间:2017-12-27 07:08:07

标签: email gmail phpmailer

我尝试过对我的代码进行一些修改但没有任何效果。代码本身不会返回任何错误,而是提供成功消息。我正在使用gmail作为我的接力。

P.S,我评论了$mail->IsSMTP();,因为我在这里看到一个类似的问题,用它作为修复,我得到了一个“smtp无法连接”错误。

我正在使用PHPmailer 6.0。

这是我的代码:

<?php

  require_once('vendor/autoload.php');

  define('GUSER', 'example@gmail.com'); // GMail username
  define('GPWD', '*********'); // GMail password

  function smtpmailer($to, $from, $from_name, $subject, $body) {
	global $error;
	$mail = new PHPMailer\PHPMailer\PHPMailer(true);  // create a new object
	//$mail->IsSMTP(); // enable SMTP
	$mail->SMTPDebug = 4;  // debugging: 1 = errors and messages, 2 = messages only
	$mail->SMTPAuth = true;  // authentication enabled
	$mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for GMail
	$mail->Host = 'smtp.gmail.com';
	$mail->Port = 465;
	$mail->Username = GUSER;
	$mail->Password = GPWD;
	$mail->SetFrom($from, $from_name);
	$mail->Subject = $subject;
	$mail->Body = $body;
	$mail->AddAddress($to);
	if(!$mail->Send()) {
		$error = 'Mail error: '.$mail->ErrorInfo;
		return false;
	} else {
		$error = 'Message sent!';
		return true;
	}
  }

  smtpmailer('to@mail.com', 'from@mail.com', 'yourName', 'test mail message', 'Hello World!');
  if (smtpmailer('to@mail.com', 'from@mail.com', 'yourName', 'test mail message', 'Hello World!')) {
	// do something
  }
  if (!empty($error)) echo $error;  

 ?>

如果我取消注释$mail->IsSMTP();,我会收到此错误日志:

  

2017-12-27 07:58:54连接:打开smtp.gmail.com:465,timeout = 300,options = array()   2017-12-27 07:58:54连接失败。错误#2:stream_socket_client():无法连接到smtp.gmail.com:465(网络无法访问)[/srv/disk2/2564570/www/consorttest.dx.am/vendor/phpmailer/phpmailer/src/SMTP。 php line 325]   2017-12-27 07:58:54 SMTP错误:无法连接到服务器:网络无法访问(101)   SMTP connect()失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

致命错误:未捕获PHPMailer \ PHPMailer \ Exception:SMTP connect()失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting在/srv/disk2/2564570/www/consorttest.dx.am/vendor/phpmailer/phpmailer/src/PHPMailer.php:1726堆栈跟踪:#0 / srv / disk2 / 2564570 / www / consorttest。 dx.am/vendor/phpmailer/phpmailer/src/PHPMailer.php(1481):PHPMailer \ PHPMailer \ PHPMailer-&gt; smtpSend('Date:Wed,27 D ...','Hello World!\ r \ n' )#1 /srv/disk2/2564570/www/consorttest.dx.am/vendor/phpmailer/phpmailer/src/PHPMailer.php(1320):PHPMailer \ PHPMailer \ PHPMailer-&gt; postSend()#2 / srv / disk2 /2564570/www/consorttest.dx.am/mailtest.php(23):PHPMailer \ PHPMailer \ PHPMailer-&gt; send()#3 /srv/disk2/2564570/www/consorttest.dx.am/mailtest.php( 32):smtpmailer('to@mail.com','from @mail.com','yourName','测试邮件消息......','Hello World!')#4 {main}抛出/ srv /第1726行的disk2 / 2564570 / www / consorttest.dx.am / vendor / phpmailer / phpmailer / src / PHPMailer.php

1 个答案:

答案 0 :(得分:0)

如果您注释掉isSMTP(),那么您不是“使用gmail作为中继”,因为它根本不使用SMTP,并且会忽略您的所有SMTP设置。您使用PHP的内置mail函数通过本地邮件服务器发送。

通过gmail发送时,您不能使用任意地址,但您可以在Gmail帐户中预设别名。

您的代码基于一个非常陈旧且过时的示例 - 使用随PHPMailer提供的gmail。

错误输出中最重要的部分是:Network is unreachable - 这可能意味着您的ISP阻止出站SMTP - 您是否有机会使用GoDaddy?

接下来,您有一个基本的错误配置:您使用SMTPSecure = 'tls'连接到端口465,这意味着它将尝试使用SMTP + STARTTLS显式TLS加密,而这将无法在端口465上运行这是使用提供的示例的一个关键原因 - 它们不会产生这样的基本错误。

这些内容中的每一项都包含在the troubleshooting guide错误链接中。

相关问题