php - 使用 Laravel 中集成的 Office365 发送电子邮件

标签 php laravel office365

只是想知道如何使用 laravel 中集成的 Office365 发送电子邮件。

先谢谢你们了。

最佳答案

您可以使用下面的php代码使用Office365发送电子邮件

<?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 'Email could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Email has been sent.';
}

如果出现任何错误,您可以取消注释代码。

您还可以使用 Laravel 中的 Swift Mailer 库来发送电子邮件。 .env 文件应默认包含以下值:

MAIL_DRIVER=null
MAIL_HOST=null
MAIL_PORT=null
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

这些是默认值,您需要将其替换为您的 Office365 详细信息,如下所示:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.office365.com
MAIL_PORT=587
MAIL_USERNAME=Office365AccountEmail
MAIL_PASSWORD=Office365AccountPassword
MAIL_ENCRYPTION=tls

更多详情可以引用this link

希望这对您有帮助。

关于php - 使用 Laravel 中集成的 Office365 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45080419/

相关文章:

office365 - 无需 Azure 帐户即可连接到 Office365 API

javascript - 在javascript中使用post数组传递数据

mysql - 将特定 CONCAT MYSQL 转换为 PDO Laravel

php - laravel seeder 根据数据库中的现有数据在列中分配一个值

office365 - 是否可以通过 Office 的 JavaScript API 设置 Outlook 360 中邮件的敏感度级别?

rest - 使用 Office 365 REST API OAuth access_token 时出现 SharePoint 错误

php - preg_replace() 根据出现顺序通过增量变量替换相同的字符串

php - 没有在 Eloquent 模型中设置表

javascript - jQuery XMLHttpRequest 调用外部 PHP 表单未提交

javascript - 如何通过一键删除按钮删除选中的项目