问题
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