简体   繁体   English

从PHP脚本发送电子邮件

[英]Send Email from PHP Script

I wrote some code to send an email from my PHP script using PHPMailer. 我编写了一些代码,以使用PHPMailer从我的PHP脚本发送电子邮件。 For some reason, the scripts isn't sending the messages. 由于某种原因,脚本没有发送消息。


Here Is My Code: 这是我的代码:

<?php
require_once("PHPMailer/class.phpmailer.php");
$mail=new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 465;
$mail->Username = "admin@zbrowntechnology.com";
$mail->Password = "PASSHERE";
$mail->SetFrom = "admin@zbrowntechnology.com";
$mail->AddAddress("zach@zbrowntechnology.com.com");
$mail->Subject = "Confirm Web Lock Registration!";
$mail->Body = "Please confirm your Web Lock Registration by clicking here!";
$mail->WordWrap = 50;

if(!$mail->Send())
{
   echo 'Message was not sent.';
   echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
  echo "Message Sent!";
}
?>

This Is The Error Echoed: 这是错误提示:

SMTP Error: Could not connect to SMTP host. Message was not sent.Mailer error: SMTP Error: Could not connect to SMTP host.

Your error message might be caused by the firewall settings on your server. 您的错误消息可能是由服务器上的防火墙设置引起的。 This error message is commonly caused by a firewall blocking outgoing connections on the port. 此错误消息通常是由防火墙阻止端口上的传出连接引起的。

You also should make sure that you have the openssl extension enabled. 您还应该确保已启用openssl扩展名。

Original Answer that you fixed: 您修复的原始答案:

You are sending to zach@zbrowntechnology.com.com which is not the address you want. 您将发送给zach@zbrowntechnology.com.com,这不是您想要的地址。

You need to remove the second .com and change it to zach@zbrowntechnology.com 您需要删除第二个.com并将其更改为zach@zbrowntechnology.com

I'm using PHPMailer too, just tried your settings, and got the same error. 我也使用PHPMailer,只是尝试了您的设置,并遇到了相同的错误。 Here are my settings witch work for me (I show with ->> things witch you don't have, or are different for me) 这是我为女巫工作的设置(我用->>显示您没有的巫婆或与我不同的事物)

$mail->PHPMailer = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
->> $mail->SMTPSecure = "ssl";
->> $mail->Host = "smtp.gmail.com"; //not ssl://smtp.gmail.com
$mail->Port = 465; etc...

Everything else is the same, except i don't use word wrap, but i checked, it's not causing the problem. 除我不使用自动换行外,其他所有内容均相同,但是我检查了一下,这没有引起问题。

You need to specify gmail username and password because that is the what you are using in smtp settings: 您需要指定gmail用户名和密码,因为这是您在smtp设置中使用的名称:

$mail->Username = "email_address@gmail.com";
$mail->Password = "yourgmailpassword";

As a general tip, try turning on debugging: 作为一般提示,请尝试打开调试:

$mail->SMTPDebug  = 2; // enables SMTP debug information (for testing)
                       // 1 = errors and messages
                       // 2 = messages only

From SO: Debugging PHP Mail() and/or PHPMailer 从SO: 调试PHP Mail()和/或PHPMailer

就像您可能还想尝试使用Zend_Mail的一些建议一样,您可以愉快地使用它,而不必走遍整个MVC路线,它非常有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM