2

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);
bDir
  • 173
  • 1
  • 2
  • 11

2 Answers2

8

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!

AbsoluteƵERØ
  • 7,816
  • 2
  • 24
  • 35
user3531993
  • 336
  • 2
  • 3
  • Using Pear's Mail.php this answer led to my solution. I simply had to uncomment the "extension=php_openssl.dll" line in my ini as openSSL was apparently already installed. Also for me in 2020, the host was set as "smtp.office365.com". – LittleTreeX Jan 07 '20 at 21:45
  • Since Oct 2018 your stream_get_transports should list at least tls_v1.2 see here for details: https://dirteam.com/dave/2018/01/10/office-365-only-allows-tls-1-2/ Also make sure that your PEAR installation is new enough i.e. it contains Auth/SASL.php file. – Thinkeye May 29 '20 at 16:36
1

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. e.g. 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>");
}
?>
mkysoft
  • 5,392
  • 1
  • 21
  • 30
clint
  • 11
  • 1