[Tutorial] How to send emails using PHPMailer


  • | 16270 points

    This is a basic tutorial on how to send emails from Laragon using PHPMailer.

    In your project require PHPMailer

    composer require phpmailer/phpmailer
    

    Find your SMTP setting

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

    Find your SMTP setting from your ISP

    Use an example script

    You will need to modify one of the example scripts based on your ISP settings. 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.

    <?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';
    
    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');
    //Set the subject line
    //    $mail->Subject = 'PHPMailer SMTP without auth test';
    //Read an HTML message body from an external file, convert referenced images to embedded,
    //convert HTML into a basic plain-text alternative body
    //Replace the plain text body with one created manually
    //    $mail->AltBody = 'This is a plain-text message body';
    //Attach an image file
    
        //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 and see the debug message in your console:

    ...
    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

    Email Successfully sent

    Conclusion

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

    I hope this helps


Log in to reply
 

Looks like your connection to Laragon was lost, please wait while we try to reconnect.