用PHP发送Outlook邮件

5

以下是我的代码(它可以与 Gmail 账户一起正常工作,但无法与 Outlook 一起工作) 我更改了 Host、SMTPDebug(从 0 改为 2)、Port(从 465 改为 587),但仍然无法使用

    $mailto = $mail;
    $mailSub = "Invitation";
    $mailMsg = "Bonjour $nom  $prenom de cin: $CIN, <br> Votre entretien est 
    le $date à $heure <br> Merci d'être à l heure <br> Cordialement ";
    require '../PHPMailer-master/PHPMailerAutoload.php';
    $mail = new PHPMailer();
    $mail->IsSmtp();
    $mail->SMTPDebug = 2;

    $mail->SMTPAuth = true;
    $mail->SMTPSecure = 'ssl';
    $mail->Host = "smtp.live.com";
    $mail->Port = 587; 

    $mail->IsHTML(true);
    $mail->Username = "*******@******";
    $mail->Password = "*********";
    $mail->setFrom("*****@******");
    $mail->Subject = $mailSub;
    $mail->Body = $mailMsg;
    $mail->AddAddress($mailto);
    $mail->Send();

1
如果在 Gmail 上可以使用,但在 Outlook 上无法使用,那么肯定是账户细节的问题吧? - treyBake
我检查了很多次...我复制了一遍以确保,但它还是不起作用。 - Omar Krichen
3个回答

9

1
我修改了 $mail->SMTPSecure = 'tls'; 然后它就可以工作了。谢谢!! - Omar Krichen
3
好的,很高兴能帮忙。不用谢! - Nauman Moazzam

3
我刚刚替换了这一行代码,现在它可以正常工作:
      $mail->SMTPSecure = 'ssl';

转换为:

      $mail->SMTPSecure = 'tls';

1
等效常量是'ssl',也称为PHPMailer::ENCRYPTION_SMTPStls也称为PHPMailer::ENCRYPTION_STARTTLS。将tls称为starttls非常误导人,除非'tls'不是tls而实际上是starttls... - ThorSummoner

0
只是评论这个: //$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
     //Server settings
    $mail->SMTPDebug = SMTP::DEBUG_SERVER; 
    $mail->isSMTP();          
                                     
    $mail->Host       = 'smtp.office365.com'; 
    $mail->SMTPAuth   = true;         
    $mail->Username   = 'xxxxxxxxx@outlook.com';                     //SMTP username
    $mail->Password   = 'xxxxxxxxx';                               //SMTP password
    //$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
    $mail->Port       = 587;                                      //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`

    //Recipients
    $mail->setFrom('xxxxx@outlook.com', 'Mailer');
    $mail->addAddress('xxxx@gmail.com', 'Joe User');     //Add a recipient
    $mail->addAddress('xxxx@gmail.com');               //Name is optional
    //$mail->addReplyTo('info@example.com', 'Information');
    //$mail->addCC('cc@example.com');
    //$mail->addBCC('bcc@example.com');

    //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}";
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接