How to send email with a function using PHPMailer?

How to send email with a function using PHPMailer?

If you want to send simple emails with PHP, you can make it easy by using PHPMailer and creating a new email sending function.

Category:Technology

Created:


If you want to send simple emails with PHP, you can make your job easier by creating a new emailing function. I say a new function because PHP already has the mail() function. However, this function does not provide any assistance for making use of popular features such as encryption, authentication, HTML messages, or attachments. And the email you send will probably end up in the spam box. Therefore, we will use the PHPMailer which is the classic email sending library for PHP.

Let's start.

In this practice, we will use PHPMailer version 6.8.0, which was released on March 6, 2023. First, install the library. You can install it via Composer or manually, and you can check the library's GitHub page for help with installation.

I prefer to use the codes by dividing them into three parts. You can think of them as the config.php file with the email settings and library integration, the functions.php file with the function, and the index.php file where you send the email. Or you can use the codes in a single file, provided they are in the same order.

After installation, we must import the classes and specify the data. Import PHPMailer classes into the global namespace and enter the information of the address to which the emails will be sent.

config.php

// Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

// Set the information of the address to which the emails will be sent
define('mailSenderAddress', '[email protected]');
define('mailSenderName', 'John Doe');
define('mailUsername', '[email protected]'); // username or mail
define('mailPassword', 'y0urP455w0rd');
define('mailHost', 'smtp.server.com');
define('mailPort', 465); // default 465
define('mailSMTPAuth', true); // default true
define('mailSMTPSecure', 'ssl'); // default ssl

Next, let's create a function that contains only the recipient's email address, the subject of the email, and the message. Let's call the function sendMail(). Thus, we can send email easily by including the settings in this function.

functions.php

function sendMail($sendMailAdress, $sendMailName, $sendMailSubject, $sendMailMessage) {

    $mail = new PHPMailer(true); // Create an instance; passing `true` enables exceptions

    //Server settings
    $mail->isSMTP(); // Set mailer to use SMTP
    $mail->Host = mailHost; // Specify main and backup SMTP servers
    $mail->SMTPAuth = mailSMTPAuth; // Enable SMTP authentication
    $mail->Username = mailUsername; // SMTP username or mail
    $mail->Password = mailPassword; // SMTP password
    $mail->SMTPSecure = mailSMTPSecure; // Enable TLS encryption, `ssl` also accepted
    $mail->Port = mailPort; // TCP port to connect to
    $mail->CharSet = 'utf-8';

    //Recipients
    $mail->setFrom(mailSenderAddress, mailSenderName);
    $mail->addAddress($sendMailAdress, $sendMailName); // Add a recipient

    //Content
    $mail->isHTML(true); // Set email format to HTML
    $mail->Subject = $sendMailSubject; // Here is the subject
    $mail->Body = $sendMailMessage; // This is the HTML message body in bold!

    if (!$mail->send()) {
        return false;
    } else {
        return true;
    }
}

Finally, it remains to run a simple function. Now we can send an email simply by entering the recipient's email address, the recipient's name, the message subject, and the message respectively.

index.php

sendMail("[email protected]", "Jane Doe", "Email subject", "and the content of the mail");

That's it.