简体   繁体   中英

Send Email using Outlook through PHP Pear

I'm trying to send an email using PHP Pear. It works when I send email using Gmail with this settings:

$smtp = Mail::factory('smtp', array(
    'host' => 'ssl://smtp.gmail.com',
    'port' => '465',
    'auth' => true,
    'username' => 'username@gmail.com',
    'password' => 'password!2016'
));

But before I make this work, I have to "allow less secure apps" to access my account, which you can read here on how to do it.

I tried to use an Outlook account with this settings:

$smtp = Mail::factory('smtp', array(
    'host' => 'tls://smtp.office365.com',
    'port' => '587',
    'auth' => true,
    'username' => 'email@domain.org',
    'password' => 'greatPasswordComesWithGreatResponsibilities'
));

but I get this error:

Failed to connect to ssl://smtp.office365.com:587 [SMTP: Failed to connect socket: fsockopen(): unable to connect to ssl://smtp.office365.com:587 (Unknown error) (code: -1, response: )]

I checked the official article by Microsoft regarding their SMTP settings. So I thought I have the right settings, but it's not working.

Am I missing something? Why do I get this error? Is there a setting I have to configure in my outlook account, like with the Gmail, before a third party app be allowed to send email?

This may not help but the title of your post just said Outlook account but the link and smtp settings refer to Office365 for Business accounts. I can't shed any light on the business account but if you actually have a personal Outlook.com account the settings are different. And, there is an option on the account you must change to enable pop/smtp access as you did with gmail.

The official Microsoft article is: https://support.office.com/en-us/article/Add-your-Outlook-com-account-to-another-mail-app-73f3b178-0009-41ae-aab1-87b80fa94970?ui=en-US&rs=en-US&ad=US&fromAR=1

But, the summary is the smtp server is smtp-mail.outlook.com and to enable pop/smtp in your Outlook account look for Options > Managing your account > Connect devices and apps with POP.

Update: If you are actually using business account, you should check this post which claims to have it working: https://stackoverflow.com/a/26004690/2891120

The key item in his code is he doesn't have "tls://" in front of the host name. I have a consumer outlook account and get a similar socket failure to you with tls:// prefix. If I remove that prefix I get much farther (set debug=true in factory) but fail later with an auth failure. Using 5.2.16 PHPMailer instead I successfully send mail but the headers leave me worried about spam rejection rates (no DKIM, SPF was softfail for my AWS entity, warning about -f, and others). And, the email did end up in the junk folder of my other test recipient @outlook.com. But I digress.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM