1<?php
2/**
3 * This example shows making an SMTP connection with authentication.
4 */
5
6//Import the PHPMailer class into the global namespace
7use PHPMailer\PHPMailer\PHPMailer;
8use PHPMailer\PHPMailer\SMTP;
9
10//SMTP needs accurate times, and the PHP time zone MUST be set
11//This should be done in your php.ini, but this is how to do it if you don't have access to that
12date_default_timezone_set('Etc/UTC');
13
14require '../vendor/autoload.php';
15
16//Create a new PHPMailer instance
17$mail = new PHPMailer;
18//Tell PHPMailer to use SMTP
19$mail->isSMTP();
20//Enable SMTP debugging
21// SMTP::DEBUG_OFF = off (for production use)
22// SMTP::DEBUG_CLIENT = client messages
23// SMTP::DEBUG_SERVER = client and server messages
24$mail->SMTPDebug = SMTP::DEBUG_SERVER;
25//Set the hostname of the mail server
26$mail->Host = 'mail.example.com';
27//Set the SMTP port number - likely to be 25, 465 or 587
28$mail->Port = 25;
29//Whether to use SMTP authentication
30$mail->SMTPAuth = true;
31//Username to use for SMTP authentication
32$mail->Username = 'yourname@example.com';
33//Password to use for SMTP authentication
34$mail->Password = 'yourpassword';
35//Set who the message is to be sent from
36$mail->setFrom('from@example.com', 'First Last');
37//Set an alternative reply-to address
38$mail->addReplyTo('replyto@example.com', 'First Last');
39//Set who the message is to be sent to
40$mail->addAddress('whoto@example.com', 'John Doe');
41//Set the subject line
42$mail->Subject = 'PHPMailer SMTP test';
43//Read an HTML message body from an external file, convert referenced images to embedded,
44//convert HTML into a basic plain-text alternative body
45$mail->msgHTML(file_get_contents('contents.html'), __DIR__);
46//Replace the plain text body with one created manually
47$mail->AltBody = 'This is a plain-text message body';
48//Attach an image file
49$mail->addAttachment('images/phpmailer_mini.png');
50
51//send the message, check for errors
52if (!$mail->send()) {
53    echo 'Mailer Error: ' . $mail->ErrorInfo;
54} else {
55    echo 'Message sent!';
56}
57