使用 Office365 SMTP 设置 PHPMailer [英] Setting up PHPMailer with Office365 SMTP
问题描述
我正在尝试设置 PHPMailer,以便我们的一位客户能够让自动生成的电子邮件来自他们自己的帐户.我登录了他们的Office 365账号,发现PHPMailer需要的设置是:
I am attempting to set up PHPMailer so that one of our clients is able to have the automatically generated emails come from their own account. I have logged into their Office 365 account, and found that the required settings for PHPMailer are:
Host: smtp.office365.com
Port: 587
Auth: tls
我已将这些设置应用于 PHPMailer,但是没有发送电子邮件(我调用的函数适用于我们自己的邮件,它是从外部服务器(不是提供网页的服务器)发送的).
I have applied these settings to PHPMailer, however no email gets sent (The function I call works fine for our own mail, which is sent from an external server (Not the server serving the web pages)).
"host" => "smtp.office365.com",
"port" => 587,
"auth" => true,
"secure" => "tls",
"username" => "clientemail@office365.com",
"password" => "clientpass",
"to" => "myemail",
"from" => "clientemail@office365.com",
"fromname" => "clientname",
"subject" => $subject,
"body" => $body,
"altbody" => $body,
"message" => "",
"debug" => false
有谁知道让 PHPMailer 通过 smtp.office365.com 发送需要哪些设置?
Does anyone know what settings are required to get PHPMailer to send via smtp.office365.com?
推荐答案
@nitin 的代码对我不起作用,因为它在 SMTPSecure 参数中缺少tls".
@nitin's code was not working for me, as it was missing 'tls' in the SMTPSecure param.
这是一个工作版本.我还添加了两行注释掉的行,您可以在出现问题时使用它们.
Here is a working version. I've also added two commented out lines, which you can use in case something is not working.
<?php
require 'vendor/phpmailer/phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.office365.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = 'somebody@somewhere.com';
$mail->Password = 'YourPassword';
$mail->SetFrom('somebody@somewhere.com', 'FromEmail');
$mail->addAddress('recipient@domain.com', 'ToEmail');
//$mail->SMTPDebug = 3;
//$mail->Debugoutput = function($str, $level) {echo "debug level $level; message: $str";}; //$mail->Debugoutput = 'echo';
$mail->IsHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
这篇关于使用 Office365 SMTP 设置 PHPMailer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!