Setting up PHPMailer with Office365 SMTP
@nitin's code was not working for me, as it was missing 'tls' in the SMTPSecure param.
Here is a working version. I've also added two commented out lines, which you can use in case something is not working.
<?php
require 'vendor/phpmailer/phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.office365.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = 'somebody@somewhere.com';
$mail->Password = 'YourPassword';
$mail->SetFrom('somebody@somewhere.com', 'FromEmail');
$mail->addAddress('recipient@domain.com', 'ToEmail');
//$mail->SMTPDebug = 3;
//$mail->Debugoutput = function($str, $level) {echo "debug level $level; message: $str";}; //$mail->Debugoutput = 'echo';
$mail->IsHTML(true);
$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';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
php.ini & SMTP= - how do you pass username & password
PHP mail()
command does not support authentication. Your options:
- PHPMailer- Tutorial
- PEAR - Tutorial
- Custom functions - See various solutions in the notes section: http://php.net/manual/en/ref.mail.php
PHPMailer SMTP configuration
Just noticed I typed SMTP wrong in this line $mail->SMPTAuth = true;
, correcting this solved my problem.
Use default PHP mail function with SMTP server on Linux
You standard php mail function will just send to whatever is defined as the sendmail _path in php.ini
This is typically sendmail -t -i
You would need to configure sendmail to use smtp.
FWIW, most developer who do a lot of mail sending from PHP apps revile the mail() and instead use one of many mailing libraries (or services) which provide better configurability/reliability.
You could for example pipe the mail function to your own PHP script and use whatever library you wanted to in that script in order to do mail sending (and thus preserving the use of mail() function across applications).
Related Topics
Check If PHP-Page Is Accessed from an iOS Device
How to Run Cronjobs More Often Than Once Per Minute
Php.Ini Is Nonexistent Loaded Configuration File (None)
Get Date Range Between Two Dates Excluding Weekends
Best Way to Get Files from a Dir Filtered by Certain Extension in PHP
Send Cookie with File_Get_Contents
How to Make Number_Format() Not to Round Numbers Up
How to Get Beanstalkd Queue to Work for PHP
Trouble Using Posix_Kill in PHP
Run Shell Command and Send Output to File
Pkill -F Doesn't Work for Process Killing
Php: Multiple SQL Queries in One MySQL_Query Statement
Php, Display Image with Header()
Naming Convention Issues When Using Codeigniter in Windows and Linux