How to send Emails using PHPMailer from Windows

Created at 2021-09-14 Updated at 2021-09-14 Category Email Tag Windows / Resource / PHP / Laragon / Email / PHPMailer

This is a basic tutorial on how to send emails from Windows using PHPMailer. I’m using Laragon as my Windows development environment.

Find your Email setting

You need your SMTP or IMAP settings from your ISP. It should look like this:

Plusnet SMTP and IMAP settings

In your project require PHPMailer

Create an empty project folder and require phpmailer

1
composer require phpmailer/phpmailer

Use an example script

Phpmailer has many example scripts. You will need to choose the best one based on your settings from your ISP.

My ISP only requires the outgoing mail server and port (STARTTLS is Yes based on the port 587), I therefore used the smtp_no_auth.phps script as my starting point. Save it in src directory as smtp_no_auth.php, then I modified the script with setting with my ISP:

  • In my case:
    • Outgoing server: relay.plus.net = $mail->Host = 'relay.plus.net';
    • Outgoing port: 587 = $mail->Port = 587;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
// 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\SMTP;
use PHPMailer\PHPMailer\Exception;

// Load Composer's autoloader
require __DIR__ . '/../vendor/autoload.php';


try {

// Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);
// Tell PHPMailer to use SMTP
$mail->isSMTP();
// Enable SMTP debugging
// SMTP::DEBUG_OFF = off (for production use)
// SMTP::DEBUG_CLIENT = client messages
// SMTP::DEBUG_SERVER = client and server messages
$mail->SMTPDebug = SMTP::DEBUG_SERVER;

// Set the hostname of the mail server
$mail->Host = 'relay.plus.net';
// Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 587;

// We don't need to set this as it's the default value
// $mail->SMTPAuth = false;
// Set who the message is to be sent from
$mail->setFrom('from@example.com', 'Pen-y-Fan');
// Set an alternative reply-to address
// $mail->addReplyTo('replyto@example.com', 'First Last');
// Set who the message is to be sent to
$mail->addAddress('my-email@my-isp.com', 'Pen-y-Fan');


// 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}";
}

Send it!

Then you can run the script:

1
php -f src/smtp_no_auth.php

You will see the debug message in your console:

1
2
3
4
5
6
...
2021-08-24 23:01:05 SERVER -> CLIENT: 250 IfPhmrsuy6wwFIfPim0eEe mail accepted for delivery
2021-08-24 23:01:05 CLIENT -> SERVER: QUIT
2021-08-24 23:01:05 SERVER -> CLIENT: 221 avasout07 smtp closing connection
Message has been sent
Process finished with exit code 0

Check your email client

Wait a few seconds and the email will be delivered to your inbox.

Email Successfully sent

Conclusion

You will need to adjust the script you use based on your authentication and setting from your ISP.

Laragon automatically added a self-signed certificate to php.ini to allow secure transmission.

I hope this helps.

Site by Pen y Fan using Hexo & Random
Pen y Fan

Posts about my coding experiences, primarily for my learning path with PHP, good clean professional code.

Hide