phpmailerHow can I use PHPMailer to send emails through Outlook?
PHPMailer is a library that allows you to send emails via SMTP, POP3, or IMAP. You can use it to send emails through Outlook by configuring it to use Outlook's SMTP server.
The following example code shows how to send an email using PHPMailer and Outlook's SMTP server:
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
// Load Composer's autoloader
require 'vendor/autoload.php';
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp-mail.outlook.com'; // Specify Outlook's SMTP server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '[email protected]'; // Outlook username
$mail->Password = 'secret'; // Outlook password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('[email protected]', 'Mailer');
$mail->addAddress('[email protected]', 'Joe User'); // Add a recipient
$mail->addAddress('[email protected]'); // Name is optional
$mail->addReplyTo('[email protected]', 'Information');
$mail->addCC('[email protected]');
$mail->addBCC('[email protected]');
// Attachments
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
// Content
$mail->isHTML(true); // Set email format to HTML
$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';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
The code above will output Message has been sent
if the email is sent successfully.
The code consists of the following parts:
use PHPMailer\PHPMailer\PHPMailer;
anduse PHPMailer\PHPMailer\Exception;
: These lines import the PHPMailer classes into the global namespace.require 'vendor/autoload.php';
: This line loads the Composer autoloader.$mail = new PHPMailer(true);
: This line instantiates the PHPMailer object, and passingtrue
enables exceptions.$mail->SMTPDebug = 2;
: This line sets the SMTP debug level to 2, which will output verbose debug output.$mail->isSMTP();
: This line sets the mailer to use SMTP.$mail->Host = 'smtp-mail.outlook.com';
: This line sets Outlook's SMTP server.$mail->SMTPAuth = true;
: This line enables SMTP authentication.$mail->Username = '[email protected]';
and$mail->Password = 'secret';
: These lines set the Outlook username and password.$mail->SMTPSecure = 'tls';
: This line enables TLS encryption.$mail->Port = 587;
: This line sets the TCP port to connect to.$mail->setFrom('[email protected]', 'Mailer');
: This line sets the sender of the email.$mail->addAddress('[email protected]', 'Joe User');
: This line adds a recipient to the email.$mail->addReplyTo('[email protected]', 'Information');
: This line sets the reply-to address.$mail->addCC('[email protected]');
and$mail->addBCC('[email protected]');
: These lines add CC and BCC recipients to the email.$mail->addAttachment('/var/tmp/file.tar.gz');
and$mail->addAttachment('/tmp/image.jpg', 'new.jpg');
: These lines add attachments to the email.$mail->isHTML(true);
: This line sets the email format to HTML.$mail->Subject = 'Here is the subject';
: This line sets the subject of the email.$mail->Body = 'This is the HTML message body <b>in bold!</b>';
and$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
: These lines set the body of the email.$mail->send();
: This line sends the email.
For more information about PHPMailer, see the official documentation.
More of Phpmailer
- How do I configure PHPmailer to use Zoho SMTP?
- How do I use PHPMailer to attach a ZIP file?
- How can I configure PHPMailer to support Polish characters?
- How can I configure PHPMailer to work with GoDaddy?
- How do I use PHPMailer with Yii2?
- How do I set the X-Mailer header using PHPMailer?
- How can I use PHPMailer without SMTP secure?
- How can I use PHPMailer to send emails from my WordPress site?
- How do I determine which version of PHPMailer I'm using?
- How can I send a UTF-8 encoded email using PHPMailer?
See more codes...