Setting up PHPMailer with Office365 SMTP

后端 未结 6 1836
盖世英雄少女心
盖世英雄少女心 2020-12-06 11:01

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 Offic

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-06 11:49

    UPDATE: August 2020

    So i was struggeling at this Problem really hard. For Business Accounts with Exchange Online and access to the Microsoft Admin Center i can provide the answer for this.

    TLDR: Goto the Admin Center and select the User you want to send the Mail. Then look under settings after E-Mail and E-Mail Apps after the Setting "authenticated SMTP", simply enable it.

    Still not working? I got you covered, here is how i got it fully working.

    1. Use PHP composer, saves a lot of work actually.
    2. Replace your Code with my Code and change it after test
    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 = "

    This is a body message in html

    " //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 { }
    1. This may looks like your file, whats ok, but now comes the easy-tricky part. As like as Google, Microsoft implemented a "switch" for the SMTP stuff. Simply go to your Admin Center from your Business Account, or kindly ask someone with permission to do that part:
    • navigate to https://admin.microsoft.com/AdminPortal/Home#/users
    • select to user where you want to send the email from
    • under the tab "E-Mail" search for "E-Mail-Apps", click on "manage E-Mail-Apps"
    • here you can select "authenticated SMTP", make sure that option is checked and save the Changes
    1. if you use MFA, then make sure you use an app password as mentioned in https://stackoverflow.com/a/61359150/14148981

    2. Run the script

    I hope this helps someone. Took me hell long to find this option on myself.

提交回复
热议问题