PHPMailer Connection error

谁说胖子不能爱 提交于 2020-01-24 23:02:33

问题


i got an error with my php mailer. It says: "Message could not be sent. Mailer Error: SMTP connect() failed." I tried these smtp settings on a wordpress newsletter plugin, and they worked there, so i dont know why this dont work. The port need to be 587, because of my host.

$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:


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';
}


来源:https://stackoverflow.com/questions/24984332/phpmailer-connection-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!