Jos*_*one 10 php email ssl phpmailer office365
我正在尝试设置PHPMailer,以便我们的一个客户能够自动生成的电子邮件来自他们自己的帐户.我已登录到他们的Office 365帐户,发现PHPMailer所需的设置是:
Host: smtp.office365.com
Port: 587
Auth: tls
Run Code Online (Sandbox Code Playgroud)
我已将这些设置应用于PHPMailer,但是没有发送电子邮件(我调用的函数适用于我们自己的邮件,它是从外部服务器发送的(不是服务于网页的服务器)).
"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
Run Code Online (Sandbox Code Playgroud)
有谁知道让PHPMailer通过smtp.office365.com发送所需的设置?
小智 18
@ nitin的代码对我不起作用,因为它在SMTPSecure参数中缺少'tls'.
这是一个工作版本.我还添加了两个注释掉的行,您可以使用这些行以防万一.
<?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';
}
Run Code Online (Sandbox Code Playgroud)
使用已接受的答案使用Office 365发送电子邮件很有可能无法正常工作,因为 Microsoft 正在推动他们的Microsoft Graph(目前唯一支持的 PHP 框架是Laravel)。如果幸运的是您仍然能够在您的应用程序中使用它,电子邮件将转到收件人的垃圾邮件、垃圾邮件或垃圾邮件文件夹,这是您不希望发生的。
我遇到的常见错误是:
Failed to authenticate password. // REALLY FRUSTRATED WITH THIS ERROR! WHY IS MY PASSWORD WRONG?!
Run Code Online (Sandbox Code Playgroud)
或者
Failed to send AUTH LOGIN command.
Run Code Online (Sandbox Code Playgroud)
或者
Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.
Run Code Online (Sandbox Code Playgroud)
为了仍然使用已接受的答案,我们只需要更改一行,即 Password 参数行:
$mail->Password = 'YourOffice365Password';
Run Code Online (Sandbox Code Playgroud)
您不必使用登录 Office365 帐户时使用的密码来设置密码,而是必须使用应用程序密码。
首先,为了创建应用程序密码,应启用 Office 365 帐户的多重身份验证(您可能必须联系您的管理员才能启用此功能)。
之后,在您喜欢的浏览器中登录您的 Office 365
复制密码后,返回您的工作代码并将密码参数替换为复制的密码。您的应用程序现在应该能够使用 Office 365 正确发送电子邮件。
参考:
小智 6
所以我在这个问题上非常努力。对于具有 Exchange Online 的企业帐户和 Microsoft 管理中心的访问权限,我可以为此提供答案。
TLDR:转到管理中心并选择要发送邮件的用户。然后在设置“经过身份验证的SMTP”后查看电子邮件和电子邮件应用程序后的设置,只需启用它。
还是行不通?我让你知道了,这是我如何让它完全工作的。
<?php
//Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer; //important, on php files with more php stuff move it to the top
use PHPMailer\PHPMailer\SMTP; //important, on php files with more php stuff move it to the top
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');
require 'path/to/vendor/autoload.php'; //important
//Enable SMTP debugging
// SMTP::DEBUG_OFF = off (for production use)
// SMTP::DEBUG_CLIENT = client messages
// SMTP::DEBUG_SERVER = client and server messages
//$mail->SMTPDebug = SMTP::DEBUG_off;
//SMTP
$mail = new PHPMailer(true); //important
$mail->CharSet = 'UTF-8'; //not important
$mail->isSMTP(); //important
$mail->Host = 'smtp.office365.com'; //important
$mail->Port = 587; //important
$mail->SMTPSecure = 'tls'; //important
$mail->SMTPAuth = true; //important, your IP get banned if not using this
//Auth
$mail->Username = 'yourname@mail.org';
$mail->Password = 'yourpassword';
//Set who the message is to be sent from, you need permission to that email as 'send as'
$mail->SetFrom('hosting@mail.org', 'Hosting Group Inc.'); //you need "send to" permission on that account, if dont use yourname@mail.org
//Set an alternative reply-to address
$mail->addReplyTo('no-reply@mail.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('customer@othermail.com', 'SIMON MÜLLER');
//Set the subject line
$mail->Subject = 'PHPMailer SMTP test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('replace-with-file.html'), __DIR__); //you can also use $mail->Body = "</p>This is a <b>body</b> message in html</p>"
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
//$mail->addAttachment('../../../images/phpmailer_mini.png');
//send the message, check for errors
if (!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
}
Run Code Online (Sandbox Code Playgroud)
如果您使用 MFA,请确保使用/sf/answers/4295140531/ 中提到的应用程序密码
运行脚本
我希望这可以帮助别人。我花了很长时间才找到这个选项。
归档时间: |
|
查看次数: |
59514 次 |
最近记录: |