我想通过PHP邮件程序使用Gmail SMTP服务器发送电子邮件。
这是我的代码
<?php
require_once('class.phpmailer.php');
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet="UTF-8";
$mail->SMTPSecure = 'tls';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->Username = '[email protected]';
$mail->Password = 'valid password';
$mail->SMTPAuth = true;
$mail->From = '[email protected]';
$mail->FromName = 'Mohammad Masoudian';
$mail->AddAddress('[email protected]');
$mail->AddReplyTo('[email protected]', 'Information');
$mail->IsHTML(true);
$mail->Subject = "PHPMailer Test Subject via Sendmail, basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->Body = "Hello";
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message sent!";
}
?>
但我收到以下错误
Mailer Error: SMTP Error: The following recipients failed: [email protected]
SMTP server error: SMTP AUTH is required for message submission on port 587
我的域名是vatandesign.ir
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "[email protected]";
$mail->Password = "password";
$mail->SetFrom("[email protected]");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("[email protected]");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
上面的代码已经过测试并为我工作过。
可能你需要$mail->SMTPSecure = 'ssl';
另外,请确保您没有为该帐户启用两步验证,因为这也会导致问题。
更新
您可以尝试将$ mail-> SMTP更改为:
$mail->SMTPSecure = 'tls';
值得注意的是,某些SMTP服务器会阻止连接。某些SMTP服务器不支持SSL
(或TLS
)连接。
Anderscc说得对。谢谢。它对我有用,但不是100%。
我必须设置
$mail->SMTPDebug = 0;
将其设置为1可能会导致错误,尤其是当您将某些数据作为json传递到下一页时。示例 - 使用json通过ajax传递数据时,如果发送邮件,则执行验证。
我不得不降低我的Gmail帐户安全设置以消除错误:“SMTP连接()失败”和“SMTP错误:密码命令失败”
解决方案:此问题可能是由于“不太安全”的应用程序尝试使用电子邮件帐户造成的(这是根据谷歌帮助,不确定他们如何判断什么是安全的,什么不是)或者如果您尝试登录多次连续或如果您更改国家/地区(例如使用VPN,将代码移动到不同的服务器或实际尝试从世界的不同地方登录)。
解决问题的链接(您必须登录到Google帐户):
注意:您可以转到以下stackoverflow应答链接以获取更详细的参考。
我认为这是连接问题,你可以在这里获取代码http://skillrow.com/sending-mail-using-smtp-and-php/
include(“smtpfile.php“);
include(“saslfile.php“); // for SASL authentication $from=”[email protected]“; //from mail id
$smtp=new smtp_class;
$smtp->host_name=”www.abc.com“; // name of host
$smtp->host_port=25;//port of host
$smtp->timeout=10;
$smtp->data_timeout=0;
$smtp->debug=1;
$smtp->html_debug=1;
$smtp->pop3_auth_host=””;
$smtp->ssl=0;
$smtp->start_tls=0;
$smtp->localhost=”localhost“;
$smtp->direct_delivery=0;
$smtp->user=”smtp username”;
$smtp->realm=””;
$smtp->password=”smtp password“;
$smtp->workstation=””;
$smtp->authentication_mechanism=””;
$mail=$smtp->SendMessage($from,array($to),array(“From:$from”,”To: $to”,”Subject: $subject”,”Date: ”.strftime(“%a, %d %b %Y %H:%M:%S %Z”)),”$message”);
if($mail){
echo “Mail sent“;
}else{
echo $smtp->error;
}
所以我刚刚解决了自己的“SMTP连接失败”错误,我想发布解决方案以防万一它可以帮助其他人。
我使用了PHPMailer示例gmail.phps文件中给出的EXACT代码。它在我使用MAMP时工作很简单,然后一旦我将其移动到我的个人服务器上,我就收到了SMTP连接错误。
我读过的所有Stack Overflow答案,以及PHPMailer的所有故障排除文档都表示这不是PHPMailer的问题。这是服务器端的设置问题。我尝试了不同的端口(587,465,25),我尝试了'SSL'和'TLS'加密。我检查了我的php.ini文件中是否启用了openssl。我检查了没有防火墙问题。一切都检查出来,但仍然没有。
解决方案是我必须删除这一行:
$mail->isSMTP();
现在一切正常。我不知道为什么,但它确实有效。我的其余代码将从PHPMailer示例文件中复制并粘贴。
另外值得注意的是,如果您启用了两个因子身份验证,则需要设置一个特定于应用程序的密码来代替您的电子邮件帐户密码。
您可以按照以下说明生成应用程序专用密码:https://support.google.com/accounts/answer/185833
然后将$mail->Password
设置为您的应用程序专用密码。
您的服务器似乎无法与Gmail SMTP服务器建立连接。以下是一些解决此问题的提示:1)检查PHP上是否正确配置了SSL(默认情况下,PHP上没有安装处理它的模块。您必须在phph.ini中检查配置)。 2)检查您的防火墙是否允许拨打所需端口的呼叫(此处为465或587)。使用telnet这样做。如果端口未打开,则需要sysdmin的一些支持来设置配置。我希望你能快点解决这个问题!
打开此Link并选择按照说明谷歌服务器阻止来自未知服务器的任何尝试,所以一旦你点击验证码检查每一件事都会没事
Google会根据可用的用户信息对Gmail帐户进行不同的处理,可能会限制垃圾邮件发送者。
在我进行电话验证之前,我无法使用SMTP。另一个帐户要仔细检查,我能够确认。
这段代码对我来说很好
$mail = new PHPMailer;
//Enable SMTP debugging.
$mail->SMTPDebug = 0;
//Set PHPMailer to use SMTP.
$mail->isSMTP();
//Set SMTP host name
$mail->Host = $hostname;
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;
//Provide username and password
$mail->Username = $sender;
$mail->Password = $mail_password;
//If SMTP requires TLS encryption then set it
$mail->SMTPSecure = "ssl";
//Set TCP port to connect to
$mail->Port = 465;
$mail->From = $sender;
$mail->FromName = $sender_name;
$mail->addAddress($to);
$mail->isHTML(true);
$mail->Subject = $Subject;
$mail->Body = $Body;
$mail->AltBody = "This is the plain text version of the email content";
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
else {
echo 'Mail Sent Successfully';
}
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
如果您使用的是cPanel,则只需单击允许您通过SMTP发送到外部服务器的wee框。
登录到CPanel>调整设置>全部>“将外发SMTP限制为root,exim和mailman(FKA SMTP Tweak)”
在这里回答: