PHPMailer不会工作?

时间:2013-10-12 01:27:15

标签: php email

所以我把所有文件都放进去并更改了SMTP信息,但由于某种原因,我无法让它工作。以下是PHP的样子..

<?php
require 'phpmailer/class.phpmailer.php';

$mail = new PHPMailer;

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.mail.yahoo.com';  // Specify main and backup server
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'MYEMAILADDRESS';                            // SMTP username
$mail->Password = 'MYEMAILPASSWORD';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted

$mail->From = 'from@example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('josh@example.net', 'Josh Adams');  // Add a recipient
$mail->addAddress('ellen@example.com');               // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

$mail->WordWrap = 50;                                 // Set word wrap to 50 characters
$mail->isHTML(true);                                  // Set email format to HTML

$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;
   exit;
}

echo 'Message has been sent';
?>

打开页面时出现此错误。

SMTP错误:无法连接到SMTP主机。邮件无法发送.Mailer错误:SMTP错误:无法连接到SMTP主机。

请帮忙!

2 个答案:

答案 0 :(得分:0)

我注意到您正在使用TLS SMTPSecure。

尝试将smtp端口设置为587或465。

$mail->Port = 465; //SSL
$mail->Port = 587; //TLS

顺便说一句,尝试ping你的smtp服务器或者尝试telnet尝试每个端口(587或465),也许有代理阻止你通过这些端口连接。

答案 1 :(得分:0)

$ mail-&gt; SMTPSecure ='tls'; 尝试$ mail-&gt; Port = 587;