PHPMailer连接错误

时间:2014-07-27 18:43:23

标签: php phpmailer

我的php邮件程序出错了。它说:“无法发送消息。 邮件程序错误:SMTP连接()失败。“我在wordpress新闻稿插件上尝试了这些smtp设置,他们在那里工作,所以我不知道为什么这不起作用。端口需要587,因为我的主机。

$mail->isSMTP();                                  // Set mailer to use SMTP
$mail->Host = 'mail.mysite.com';  // Specify main and backup SMTP servers
$mail->port = "587";  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'my@mail.com';                 // SMTP username
$mail->Password = '*******';                           // SMTP password

$mail->From = 'my@mail.com';
$mail->FromName = 'My name';
$mail->addAddress($row['email'], $row['name']);     // Add a recipient
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = $subject;
$mail->Body = $body;
}

if(!$mail->send()) {
    echo 'Message could not be sent.<br>';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

1 个答案:

答案 0 :(得分:0)

You can try this code based on same PHPMailer class:

...

require_once('class.phpmailer.php');

$mail = new PHPMailer();

$mail -> IsSMTP();
$mail -> SMTPAuth = true;

$mail -> Host = 'mail.mysite.com';
$mail -> Port = "587";

$mail -> Username = 'my@mail.com';
$mail -> Password = '*******';

$mail -> SetFrom('my@mail.com', 'My name');
$mail -> AddReplyTo('my@mail.com', 'My name');
$mail -> AddAddress($row['email'], $row['name']);

$mail -> Subject = $subject;
$mail -> MsgHTML($body);

if (!$mail -> send()) {
    echo 'Message could not be sent.<br>';
    echo 'Mailer Error: ' . $mail -> ErrorInfo;
} else {
    echo 'Message has been sent';
}