2016-07-07 150 views
2

我有一个问题,使用smtp邮件类发送邮件时,当我使用我的本地系统smtp发送电子邮件代码当时正在工作知府,但当我尝试代码在现场服务器时,我得到了每一个时间SMTP连接失败错误smtp连接失败的smtp邮件

我使用SMTP邮件类

require 'mail/PHPMailerAutoload.php'; 
$mail = new PHPMailer; 
$mail->isSMTP(); 
$mail->SMTPDebug = 2; 
$mail->Host = 'smtp.gmail.com'; 
$mail->SMTPAuth = true; 
$mail->Username = '[email protected]'; 
$mail->Password = 'mypassword';   
$mail->SMTPSecure = 'tls';    
$mail->From = '[email protected]'; 
$mail->FromName = 'Mailer'; 
$mail->addAddress('[email protected]', 'Joe User'); 
$mail->addAddress('[email protected]'); 
$mail->addReplyTo('[email protected]', 'Information'); 
$mail->addCC('[email protected]'); 
$mail->addBCC('[email protected]'); 
$mail->WordWrap = 50; 
$mail->isHTML(true); 
$mail->Subject = 'Here is the subject'; 
$mail->Body = 'This is the HTML message body <b>in bold!</b>'; 
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; 
if(!$mail->send()) { 
echo 'Message could not be sent.'; 
echo 'Mailer Error: ' . $mail->ErrorInfo; 
} else { 
echo 'Message has been sent';} 

这是我的代码,它在本地工作,但在直播服务器不能正常工作使用此代码发送邮件

我做了所有设定我的php.ini文件喜欢smtp和用户密码 但仍然没有工作,所以请给我正确的解决方案,或者在直播服务器运行的代码

这是错误

2016-07-07 17:23:09 SMTP ERROR: Failed to connect to server: Network is unreachable (101) 2016-07-07 17:23:09 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

+0

在.ini设置不会不管你的服务器主机地址。 phpmailer没有使用它们。您需要检查您的gmail服务器是否可以通过您正在测试的机器访问。 –

+0

将你的phpmailer errorinfo添加到你的问题。 – Progrock

+0

请参阅:http://stackoverflow.com/questions/3675897/how-to-check-by-php-if-my-script-is-connecting-to-smtp-server – Progrock

回答

1

当你在当地当时smtp.gmail.com其工作,但在现场你需要像mail.servername.com

0

你首先必须启用Gmail接受非安全应用