当主机和电子邮件地址不同时,无法使用Office365 smtp和PHP邮件程序发送邮件

时间:2019-03-06 04:44:18

标签: php smtp office365 phpmailer

我正在使用最新的PHPMailer通过Office365 SMTP发送电子邮件。

要求是在通过应用程序发送邮件时附加office365电子邮件签名。 为此,我们创建了一个共享邮箱“ smtp@myhosting.com”,并将所有用户电子邮件添加到其中,并设置了所有凭据。

当用户名和电子邮件地址相同时,我可以发送邮件,但是我想使用不同的用户凭据发送邮件。

下面是我的代码:

 <?php $mail = new PHPMailer(true);   
try {
    //Server settings
    $mail->SMTPDebug = 2;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp.office365.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'smtp@myhosting.com';                 // SMTP username
    $mail->Password = 'XXXXXX';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to
    //Recipients
    $mail->setFrom('user1@myhosting.com', 'User1');
    $mail->addAddress('sanjayp@gmail.com', 'sanjay');     // Add a recipient
    //$mail->addAddress('ellen@example.com');               // Name is optional
    $mail->addReplyTo('info@myhosting.com', 'Information');
//    $mail->addCC('cc@example.com');
//    $mail->addBCC('bcc@example.com');
    $mail->SMTPOptions = array(
        'ssl' => array(
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => true
        )
    );
    //Attachments
    //   $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    //   $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $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';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
} ?>

但是我遇到以下错误。

SMTP Error: data not accepted.SMTP server error: DATA END command failed Detail: STOREDRV.Submission.Exception:SendAsDeniedException.MapiExceptionSendAsDenied; Failed to process message due to a permanent exception with message Cannot submit message. 16.55847:77100000, 17.43559:0000000094000000000000000100000000000000, 20.52176:140F96861000101048050000, 20.50032:140F9686801710104D050000, 0.35180:0A00B480, 255.23226:52050000, 255.27962:0A000000, 255.27962:0E000000, 255.31418:0A000000, 16.55847:E4000000, 17.43559:00000000A0010000000000001D00000000000000, 20.52176:140F9686100010100 SMTP code: 554 Additional SMTP info: 5.2.0

任何建议都会很棒。

0 个答案:

没有答案