简体   繁体   中英

PHP smtp.office365.com for email

Is it possible to make PHP application mail integrated with Office365?

I have tried but found this error :

authentication failure [SMTP: SMTP server does not support authentication (code: 250, response: SINPR02CA025.outlook.office365.com Hello [17*.***.***.**] SIZE 78643200 PIPELINING DSN ENHANCEDSTATUSCODES STARTTLS 8BITMIME BINARYMIME CHUNKING)]

and my PHP code so far :

$host = "smtp.office365.com";
$username = "me@company.com";
$password = "example";

$headers = array(
    'From'          => $from,
    'To'            => $to,
    'Subject'       => $subject,
    'Content-Type'  => 'text/html; charset=UTF-8'
);

$recipients = $to.", ".$bcc;

$smtp = Mail::factory('smtp',
    array ('host' => $host,
    'auth' => true,
    'username' => $username,
    'password' => $password));

$mail = $smtp->send($recipients, $headers, $body);

Several points:

  • Email server names of the form pod####.outlook.com are the old standard and, while they still work for the time being, have been superseded by the generic smtp.office365.com . Just google "office365 smtp settings" and you'll see good documentation on this.
  • You'll need to specify port 587 per the documentation you'll find from item 1. just as S. Varun states.
  • While I've not reviewed your code in detail I suspect your problem is a lack of an SSL transport being loaded. The fix I'll describe is based on FreeBSD but you should be able to alter it to work on Windows (would help to know your environment):

    1. Create a PHP file that contains the following code borrowed from PHP stream_get_transports() documentation :

      $xportlist = stream_get_transports(); print_r($xportlist);

    2. When you execute this file in PHP (requires command line interface of PHP be present), if you don't see any mention of SSL, then this is your problem--no secure transports

    3. Install PHP SSL module (I use ports in FreeBSD so I go to security/php55-openssl and do make install clean )

    4. Restart Apache to make sure the new modules are loaded (ideally a graceful restart).

    5. Now re-run your get_transports PHP code and you should see SSL listed (I got SSL, SSLv3, SSLv2, TLS in addition to the original protocols)

    6. Now try your email sending code and it should work!

Hope this helps!

Assuming you have pear already installed (if you don't google how to install for your particular operating system) this code works perfectly for me in Ubuntu 12.04 LTS.

If you get a "file not found for "Mail.php" then you need to change where your pear install directory is located.

Issue this from the command line to find that out -> pear config-get php_dir . Once you know edit your include_path in php.ini.

Don't forget in linux to use a : if you need to separate multiple includes and use a ; if your using windows php server to separate multiple includes. eg for linux: include_path = ".:/usr/share/php5:/usr/share/php"

<?php
require_once('/usr/share/php/Mail.php');


$from = "John Smith <john@youroffice365emailhere.com>";
$to = "Nancy Smith <Nancy@youroffice365emailhere.com>";
$bcc = '';
$subject = "Hi!";
$body = "Hi,\n\nLooks like it worked.";

$host = 'smtp.office365.com';
$port = '587';
$username = 'Your Auth Username here.'; ##e.g. test@yourdomain.com you do not need on.microsoft.com or anything here. Use your real email address you use for authentication.
$password = 'Your Auth Password for the email address above';

$headers = array(
 'Port'          => $port,
 'From'          => $from,
 'To'            => $to,
 'Subject'       => $subject,
'Content-Type'  => 'text/html; charset=UTF-8'
);

$recipients = $to.", ".$bcc;

$smtp = Mail::factory('smtp',
 array ('host' => $host,
 'port' => $port,
 'auth' => true,
 'username' => $username,
 'password' => $password));

$mail = $smtp->send($recipients, $headers, $body);

echo "test";

if (PEAR::isError($mail)) {
   echo("<p>" . $mail->getMessage() . "</p>");
} else {
   echo("<p>Message successfully sent!</p>");
}
?>

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