使用集成在 laravel 中的 Office365 发送电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45080419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:17:34  来源:igfitidea点击:

Send emails using Office365 integrated in laravel

phplaraveloffice365

提问by Jeric

Just want to know if how can I use Office365 integrated in laravel on sending emails.

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

Thank you guys in advance.

提前谢谢你们。

回答by Hemant Kabra

you can use below php code to send emails using 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 = '[email protected]';
$mail->Password = 'YourPassword';
$mail->SetFrom('[email protected]', 'FromEmail');
$mail->addAddress('[email protected]', '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.';
}

You can uncomment the commented code in case you receive any error.

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

You can also use Swift Mailer library in Laravel to send emails. The .envfile should contain as default the following values:

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

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

These are the default values, you need to replace it with your Office365 details like below :

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

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

For more details you can refer this link

有关更多详细信息,您可以参考此链接

Hope this will help you.

希望这会帮助你。