使用Office365 SMTP设置PHPMailer [英] Setting up PHPMailer with Office365 SMTP

查看:1011
本文介绍了使用Office365 SMTP设置PHPMailer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置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

有谁知道要通过smtp.office365.com发送PHPMailer需要哪些设置? / p>

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆