我一直在使用下面的代码尝试向在我的网站上注册的人发送电子邮件,但是我无法让代码与我的 namecheap 托管电子邮件帐户一起使用。使用了这个namecheap 电子邮件配置,但它似乎仍然给我一个错误。我已经用 hotmail 设置尝试了相同的代码,它工作得很好。
错误
2016-06-17 09:28:33 SMTP ERROR: Failed to connect to server: Connection refused (61) 2016-06-17 09:28:33 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Mailer: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
代码
require_once('PHPMailerAutoload.php');
function sendmail($host, $username, $password, $to, $subject, $message, $fromName, $toName, $port, $debug) {
$mail = new PHPMailer();
$body = $message;
$mail->IsSMTP();
$mail->Host = $host;
if ($debug) $mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Port = $port;
$mail->Username = $username;
$mail->Password = $password;
$mail->SetFrom($to, $name);
$mail->AddReplyTo($to, $name);
$mail->Subject = $subject;
$mail->AltBody = $message;
$mail->MsgHTML($body);
$address = $to;
$mail->AddAddress($address, $name);
if($mail->Send()) {
echo "Message sent";
} else {
echo "Mailer: " . $mail->ErrorInfo;
}
}
echo sendMail("mail.privateemail.com", "noreply@example.com", "password", "recipient@example.com", "Subject", "Message", "Name", "Sender Name", 25, true);