I'm trying to send mail through an office 365 e-mail address but i can't figure out how to enable STARTTLS no matter what i search or try.
My code:
$fromemail='sender@email.com';
$host='smtp.office365.com';
$port=587;
$password='realpassword';
$headers = array('From' => $fromemail,'To' => $to,'Subject' => $subject,'Reply-To'=>$replyTo,'MIME-Version'=> '1.0','Content-type'=> 'text/html; charset=utf-8','Date' => date('r', time()));
$smtp = @Mail::factory('smtp', array('host' => $host,'port' => $port,'auth' =>false,'username' => $fromemail,'password' => $password,'debug'=>true,'secure'=>'starttls','socket_options' => array('ssl' => array('verify_peer'=>false,'verify_peer_name' => false, 'allow_self_signed' => true))));
$mail = @$smtp->send($to, $headers, $body);
I added 'secure'=>'starttls','socket_options' => array('ssl' => array('verify_peer'=>false,'verify_peer_name' => false, 'allow_self_signed' => true))
because I saw these options on the internet but it doesn't seem to work.
The response i'm getting:
DEBUG: Recv: 220 LO4P123CA0002.outlook.office365.com Microsoft ESMTP MAIL Service ready at Thu, 6 Oct 2022 15:07:58 +0000
DEBUG: Send: EHLO localhost
DEBUG: Recv: 250-LO4P123CA0002.outlook.office365.com Hello [194.65.78.182]
DEBUG: Recv: 250-SIZE 157286400
DEBUG: Recv: 250-PIPELINING
DEBUG: Recv: 250-DSN
DEBUG: Recv: 250-ENHANCEDSTATUSCODES
DEBUG: Recv: 250-STARTTLS
DEBUG: Recv: 250-8BITMIME
DEBUG: Recv: 250-BINARYMIME
DEBUG: Recv: 250-CHUNKING
DEBUG: Recv: 250 SMTPUTF8
DEBUG: Send: MAIL FROM:<sender@email.com>
DEBUG: Recv: 451 5.7.3 STARTTLS is required to send mail [LO4P123CA0002.GBRP123.PROD.OUTLOOK.COM]
DEBUG: Send: RSET
DEBUG: Send: QUIT
object(PEAR_Error)#9 (8) {
["error_message_prefix"]=>
string(0) ""
["mode"]=>
int(1)
["level"]=>
int(1024)
["code"]=>
int(10004)
["message"]=>
string(201) "Failed to set sender: sender@email.com [SMTP: Invalid response code received from server (code: 451, response: 5.7.3 STARTTLS is required to send mail [LO4P123CA0002.GBRP123.PROD.OUTLOOK.COM])]"
["userinfo"]=>
NULL
["backtrace"]=>
...
Is this something that needs to be fixed in the office365 configurations or is the problem on my end?