我要用phpmailer lib通过365发送电子邮件。
这是我的配置
MAIL_DRIVER=smtp
MAIL_HOST=smtp.office365.com
MAIL_PORT=587
MAIL_USERNAME=support@xxxxx.com
MAIL_PASSWORD=XXXXXX
MAIL_ENCRYPTION=STARTTLS
以下代码是发送电子邮件的函数
public function __construct()
{
date_default_timezone_set('America/Virgin');
$this->mail = new \PHPMailer;
$this->mail->isSMTP();
$this->mail->CharSet = 'UTF-8';
//Ask for HTML-friendly debug output
$this->mail->Debugoutput = 'html';
//Set the hostname of the mail server
$this->mail->Host = getenv("MAIL_HOST"); //'ssl://smtp.gmail.com';
$this->mail->Port = getenv("MAIL_PORT");
//Set the encryption system to use - ssl (deprecated) or tls
// $this->mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$this->mail->SMTPAuth = true;
$this->mail->Username = getenv("MAIL_USERNAME");
$this->mail->Password = getenv("MAIL_PASSWORD");
$this->mail->SMTPSecure = getenv("MAIL_ENCRYPTION");
$this->setSender(
$this->sender_email,
$this->sender_password,
$this->sender_display_name,
$this->sender_display_email,
$this->sender_reply_to_name,
$this->sender_reply_to_email
);
}
public function sendEmail($to_email, $to_name, $subject, $message, $template_name = '')
{
//Set who the message is to be sent to
$this->mail->addAddress($this->trim_input($to_email), $this->trim_input($to_name));
//Set the subject line
$this->mail->Subject = $subject;
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$this->mail->msgHTML($template_name);
//Replace the plain text body with one created manually
$this->mail->AltBody = $message;
//send the message, check for errors
if (!$this->mail->send()) {
return false;
// echo "Mailer Error: " . $this->mail->ErrorInfo;
} else {
return true;
}
}
我尝试在本地服务器和数字海洋服务器上发送电子邮件
在这种情况下,我已经正确地发送和接收了电子邮件,
但在godaddy服务器中有错误
怎么解决?
谢谢你的预付款。