我正在尝试使用代码发送邮件 但它正在显示
Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
并且gmail通知我登录尝试已被阻止
<?php
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'sss@gmail.com';
$mail->Password = '*******';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->From = 'abc@gmail.com';
$mail->FromName = 'asdf ';
$mail->addAddress('abc@gmail.com', 'sadf ');
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = "Using PHPMailer";
$mail->Body = "Hi Iam using PHPMailer library to sent SMTP mail from localhost";
if(!$mail->send()) {
echo "Message could not be sent.";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>
如何解决上述问题?
答案 0 :(得分:2)
您需要设置使用 gmail 发送邮件的权限。
- 登录Google帐户
- 去隐私页面
- 允许第三方应用
尝试此代码后:
$mail = new PHPMailer();
$body = file_get_contents('contents.html');
$body = eregi_replace("[\]",'',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.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->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "yourusername@gmail.com"; // GMAIL username
$mail->Password = "yourpassword"; // GMAIL password
$mail->SetFrom('name@yourdomain.com', 'First Last');
$mail->AddReplyTo("name@yourdomain.com","First Last");
$mail->Subject = "PHPMailer Test Subject via smtp (Gmail), basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "whoto@otherdomain.com";
$mail->AddAddress($address, "John Doe");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
答案 1 :(得分:2)
请尝试以下步骤:
启用调试模式以捕获可能的错误
$mail->SMTPDebug = 1;
启用SMTP身份验证
$mail->SMTPAuth = true;
还检查php配置文件(php.ini
)
extension=php_openssl.dll
答案 2 :(得分:1)
更改代码中的以下内容
isSMTP()
至IsSMTP()
,addAddress()
至AddAddress()
&amp; isHTML()
至IsHTML()
。
然后检查端口。 sometoimes端口也关闭,不允许建立连接。
希望它能奏效!
答案 3 :(得分:1)
由于您收到了来自Google的电子邮件,因此它描述了该电子邮件正在尝试发送但Google已阻止该电子邮件。执行以下步骤。
我希望这会有所帮助。
答案 4 :(得分:1)
我认为你必须在你的游戏中启用POP和IMAP。试试这个
<?
$account="email_address@gmail.com";
$password="accountpassword";
$to="mail@subinsb.com";
$from="email_address@gmail.com";
$from_name="Name";
$msg="<strong>This is a bold text.</strong>"; // HTML message
$subject="Database Backup";
/*End Config*/
include("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth= true;
$mail->Port = 465; // Or 587
$mail->Username= $account;
$mail->Password= $password;
$mail->SMTPSecure = 'ssl';
$mail->From = $from;
$mail->FromName= $from_name;
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $msg;
$mail->addAddress($to);
if(!$mail->send()){
echo "Mailer Error: " . $mail->ErrorInfo;
}else{
echo "E-Mail has been sent";
}
?>
&#13;