Skip to main content

PHPmailer 2024

Setting up PHPMailer to send emails using an outgoing SMTP server involves several steps. Below is a detailed guide to help you configure PHPMailer for SMTP:

  1. Install PHPMailer:
    • You can install PHPMailer using Composer. Open your terminal and run:


composer require phpmailer/phpmailer

 

Include PHPMailer in Your PHP Script:

  • Add the following lines to include PHPMailer in your script:

 

use PHPMailer\PHPMailer\PHPMailer;

use PHPMailer\PHPMailer\Exception;

 

require 'vendor/autoload.php'; // Adjust the path as needed

 

reate a PHPMailer Instance and Configure SMTP:

  • Below is a sample script to configure PHPMailer with SMTP:

 

$mail = new PHPMailer(true);

 

try {

    //Server settings

    $mail->SMTPDebug = 0;                                       // Enable verbose debug output (0 for no output)

    $mail->isSMTP();                                            // Set mailer to use SMTP

    $mail->Host       = 'smtp.example.com';                     // Specify main and backup SMTP servers

    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication

    $mail->Username   = This email address is being protected from spambots. You need JavaScript enabled to view it.';               // SMTP username

    $mail->Password   = 'your_password';                        // SMTP password

    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption, `PHPMailer::ENCRYPTION_SMTPS` also accepted

    $mail->Port       = 587;                                    // TCP port to connect to

 

    //Recipients

    $mail->setFrom(This email address is being protected from spambots. You need JavaScript enabled to view it.', 'Mailer');

    $mail->addAddress(This email address is being protected from spambots. You need JavaScript enabled to view it.', 'Joe User');           // Add a recipient, Name is optional

    $mail->addReplyTo(This email address is being protected from spambots. You need JavaScript enabled to view it.', 'Information');

 

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

}

 

Explanation of Configuration Parameters:

  • $mail->SMTPDebug = 0;: Sets the debug output level. Use 0 for no output, 1 for client messages, 2 for client and server messages.
  • $mail->isSMTP();: Tells PHPMailer to use SMTP.
  • $mail->Host: The SMTP server address.
  • $mail->SMTPAuth: Enables SMTP authentication.
  • $mail->Username: The SMTP username (usually your email address).
  • $mail->Password: The SMTP password.
  • $mail->SMTPSecure: Sets the encryption system to use (PHPMailer::ENCRYPTION_STARTTLS for TLS,
  • $mail->Port: The SMTP port (usually 587 for TLS).

 

Optional Settings:

  • From Name and Address:

 

$mail->setFrom(This email address is being protected from spambots. You need JavaScript enabled to view it.', 'Mailer');

 

Reply-To Address:

$mail->addReplyTo(This email address is being protected from spambots. You need JavaScript enabled to view it.', 'Information');

 

Add Multiple Recipients:

$mail->addAddress(This email address is being protected from spambots. You need JavaScript enabled to view it.', 'Recipient 1');

$mail->addAddress(This email address is being protected from spambots. You need JavaScript enabled to view it.'); // No name

 

  1. andling Errors:
    • Use a try-catch block to handle errors and exceptions. This will help you debug if something goes wrong.

By following these steps, you should be able to configure PHPMailer to send emails using mail.mysmtp.com. Adjust the server settings, username, and password according to your SMTP server details.