无法使用 smtp.gmail 使用 phpmailer
can't use smtp.gmail using phpmailer
我知道这种问题在Whosebug中被问过很多次,但我看了大部分,问题仍然存在。
我正在使用 phpMailer 通过 gmail 帐户从我的站点发送邮件。
当我在 localhost 中使用它时,一切正常,邮件发送正确,
但是只要我将我的站点上传到远程服务器,问题就会暴露出来。
当我使用端口 465
和 smtp.gmail.com
时出现 connection timeout(110) error
,当我尝试使用端口 587
和 tls
时也出现错误。
你能告诉我出了什么问题吗?
主机 php 版本为 5.4
。
这是我的代码:
require_once ("../PHPMailer/class.phpmailer.php");
class mailSender
{
protected $address;
protected $username;
protected $password;
protected $cc;
protected $bcc;
protected $body;
protected $mailer;
public function __construct( $host, $port, $usn, $psw, $isSMTP = true )
{
$this->mailer = new PHPMailer(true);
//$this->mailer->Host = "mail.yourdomain.com"; // SMTP server
$this->mailer->Host = $host; //"smtp.gmail.com"; // sets GMAIL as the SMTP server
$this->mailer->Port = $port; //465; // set the SMTP port for the GMAIL server
if( $isSMTP == true )
{
$this->mailer->IsSMTP();
//$this->mailer->SMTPDebug = 1; // enables SMTP debug information (for testing)
$this->mailer->SMTPAuth = true; // enable SMTP authentication
$this->mailer->SMTPSecure = "ssl"; // sets the prefix to the servier
}
$this->mailer->Username = $usn; //"yourusername@gmail.com"; // GMAIL username
$this->mailer->Password = $psw; //"yourpassword"; // GMAIL password
$this->mailer->CharSet = 'UTF-8';
$this->mailer->IsHTML(true);
//set High priority to prevent going to SPAM folder
$this->mailer->Priority = 1;
$this->mailer->AddCustomHeader("X-MSMail-Priority: High");
$this->mailer->AddCustomHeader("Importance: High");
}//function __construct
public function send( $address, $senderName, $replyAddr, $replyName, $fromAddr, $fromName, $subject, $body, $attachment = false, $cc = false, $bcc = false )
{
try {
//$this->mailer->AddReplyTo( $replyAddr, $replyName);
$this->mailer->AddAddress($address, $senderName);
$this->mailer->SetFrom($fromAddr, $fromName);
$this->mailer->AddReplyTo($replyAddr, $replyName);
$this->mailer->Subject = $subject; //'PHPMailer Test Subject via mail(), advanced';
$this->mailer->AltBody = 'در صورتی که قادر به تماشای محتوای ای-میل نیستید از یک نمایشگر ای-میل تحت HTML استفاده کنید!'; // optional - MsgHTML will create an alternate automatically
$this->mailer->MsgHTML($body);
//$this->mailer->MsgHTML(file_get_contents('contents.html'));
//$this->mailer->AddAttachment('images/phpmailer.gif'); // attachment
//$this->mailer->AddAttachment('images/phpmailer_mini.gif'); // attachment
$send = $this->mailer->Send();
if( $send )
{
return true;
}
else
{
return false;
}
}//try send
catch( Exception $e )
{
return false;
}//catch
}//function Send
}//class mailSender
Thanks in Advance
可能是您的 gmail 帐户被锁定了。尝试解锁验证码:
https://accounts.google.com/DisplayUnlockCaptcha.
同时在此处检查名为 Allow less secure apps 的帐户设置:
我知道这种问题在Whosebug中被问过很多次,但我看了大部分,问题仍然存在。
我正在使用 phpMailer 通过 gmail 帐户从我的站点发送邮件。
当我在 localhost 中使用它时,一切正常,邮件发送正确,
但是只要我将我的站点上传到远程服务器,问题就会暴露出来。
当我使用端口 465
和 smtp.gmail.com
时出现 connection timeout(110) error
,当我尝试使用端口 587
和 tls
时也出现错误。
你能告诉我出了什么问题吗?
主机 php 版本为 5.4
。
这是我的代码:
require_once ("../PHPMailer/class.phpmailer.php");
class mailSender
{
protected $address;
protected $username;
protected $password;
protected $cc;
protected $bcc;
protected $body;
protected $mailer;
public function __construct( $host, $port, $usn, $psw, $isSMTP = true )
{
$this->mailer = new PHPMailer(true);
//$this->mailer->Host = "mail.yourdomain.com"; // SMTP server
$this->mailer->Host = $host; //"smtp.gmail.com"; // sets GMAIL as the SMTP server
$this->mailer->Port = $port; //465; // set the SMTP port for the GMAIL server
if( $isSMTP == true )
{
$this->mailer->IsSMTP();
//$this->mailer->SMTPDebug = 1; // enables SMTP debug information (for testing)
$this->mailer->SMTPAuth = true; // enable SMTP authentication
$this->mailer->SMTPSecure = "ssl"; // sets the prefix to the servier
}
$this->mailer->Username = $usn; //"yourusername@gmail.com"; // GMAIL username
$this->mailer->Password = $psw; //"yourpassword"; // GMAIL password
$this->mailer->CharSet = 'UTF-8';
$this->mailer->IsHTML(true);
//set High priority to prevent going to SPAM folder
$this->mailer->Priority = 1;
$this->mailer->AddCustomHeader("X-MSMail-Priority: High");
$this->mailer->AddCustomHeader("Importance: High");
}//function __construct
public function send( $address, $senderName, $replyAddr, $replyName, $fromAddr, $fromName, $subject, $body, $attachment = false, $cc = false, $bcc = false )
{
try {
//$this->mailer->AddReplyTo( $replyAddr, $replyName);
$this->mailer->AddAddress($address, $senderName);
$this->mailer->SetFrom($fromAddr, $fromName);
$this->mailer->AddReplyTo($replyAddr, $replyName);
$this->mailer->Subject = $subject; //'PHPMailer Test Subject via mail(), advanced';
$this->mailer->AltBody = 'در صورتی که قادر به تماشای محتوای ای-میل نیستید از یک نمایشگر ای-میل تحت HTML استفاده کنید!'; // optional - MsgHTML will create an alternate automatically
$this->mailer->MsgHTML($body);
//$this->mailer->MsgHTML(file_get_contents('contents.html'));
//$this->mailer->AddAttachment('images/phpmailer.gif'); // attachment
//$this->mailer->AddAttachment('images/phpmailer_mini.gif'); // attachment
$send = $this->mailer->Send();
if( $send )
{
return true;
}
else
{
return false;
}
}//try send
catch( Exception $e )
{
return false;
}//catch
}//function Send
}//class mailSender
Thanks in Advance
可能是您的 gmail 帐户被锁定了。尝试解锁验证码:
https://accounts.google.com/DisplayUnlockCaptcha.
同时在此处检查名为 Allow less secure apps 的帐户设置: