How to send test email in PHP
Introduction - sending email in PHP
If you're writing software using PHP, you're probably sending emails, either as part of sign up flows, or for sending notifications or maybe you just like sending pictures of Chameleons!
The standard way to send email is via an SMTP server, usually hosted by a specialist email service provider but can also be hosted yourself.
PHP provides a built-in function called mail()
but this is no longer recommended for sending email and does not provide STARTTLS, a standard for encrypting your email traffic.
Instead, the standard approach is to make use of the PhpMailer library
The following code illustrates how to send an email:
<?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 'vendor/autoload.php';
//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'smtp.imitate.email'; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = 'user@example.com'; //SMTP username
$mail->Password = 'secret'; //SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; //Enable STARTTLS encryption
$mail->Port = 587; //TCP port to connect to
//Recipients
$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net', 'Joe User'); //Add a recipient
$mail->addAddress('ellen@example.com'); //Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');
//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}";
}
Sending Test Email: Set up Imitate Email
If you've not already done so please sign up to Imitate Email.
For Imitate Email, when sending test emails you use the following server details:
Host: smtp.imitate.email
Port: 587
Imitate Email uses STARTTLS which ensures that your emails are sent over an encrypted connection. To do that in C# ensure that smtpClient.EnableSsl = true
.
Then, for your username and password you need to decide if you're using your personal mailbox or have set up a project in Imitate Email.
Locate your mailbox credentials
Your username
To locate your username go to Settings > My Mailbox where you will find it (it is the same for all mailboxes in your account)
For the password
If you're on the Free or Developer plan or you're setting this up for your personal mailbox copy the password from the same place as the username.
If you're setting this up for a team mailbox go to Settings > Projects and you will find the password next to the symbol for your mailbox. It will look like the following 6bcb69b2-08ac-4c67-911a-10442f7d84b3
That's it!