我编写了以下代码来从我托管在 Namecheap 的网站发送电子邮件。
<?php
include("constants.php");
require_once('libs/phpmailer/class.phpmailer.php');
require_once("libs/phpmailer/class.smtp.php");
require ("libs/phpmailer/PHPMailerAutoload.php");
try {
$mailer = new PHPMailer();
$mailer->IsSMTP();
$mailer->SMTPSecure = 'tls';
$mailer->Host = 'smtp.zoho.com';
$mailer->SMPTDebug = 2;
$mailer->Port = 587;
$mailer->Username = EMAIL;
$mailer->Password = PASSWORD;
$mailer->SMTPAuth = true;
$mailer->From = EMAIL;
$mailer->FromName = "User";
$mailer->Subject = 'New Query Received';
$mailer->isHTML(true);
$mailer->Body = '<p>Hello,</p><p> The following query was received from '.$_POST['name']. '.<br>"'. $_POST['message']. '"</p><p>'.'His email is '.$_POST['email'].'</p><p>Hope you will have a great day.</p><p>Best Regards,</p>';
$mailer->AddReplyTo(EMAIL_REC, 'Contact');
$mailer->AddAddress(ADDRESS, NAME);
$mailer->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
));
if ($mailer->Send()) {
echo "Sent";
}
else {
echo $mailer->ErrorInfo;
}
}
catch (phpmailerException $e) {
echo $e->errorMessage();
} catch (Exception $e) {
echo $e->getMessage();
}
header('Location:index.html');
exit;
?>
我正在为我的电子邮件主机使用 zoho mail,但我也尝试过使用 gmail 帐户。我已经交叉验证了 EMAIL 及其密码。
该代码在我的本地主机中运行良好,但在 Namecheap 服务器中导致“SMTP 连接()失败”。
如果我评论 $mailer->IsSMTP(); 然后不会出现错误,但不会发送电子邮件。