9951 explained code solutions for 126 technologies


phpmailerHow do I use PHPMailer to send emails through Namecheap?


PHPMailer is an open source library for sending emails in PHP. To use PHPMailer to send emails through Namecheap, you will need to:

  1. Install PHPMailer using Composer, the PHP package manager:
composer require phpmailer/phpmailer
  1. Create a new PHPMailer instance with your Namecheap credentials:
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.namecheap.com';
$mail->SMTPAuth = true;
$mail->Username = 'username';
$mail->Password = 'password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
  1. Set the sender and recipient addresses:
$mail->setFrom('[email protected]', 'John Doe');
$mail->addAddress('[email protected]', 'Jane Doe');
  1. Set the subject and body of the email:
$mail->Subject = 'Hello';
$mail->Body = 'This is a test message.';
  1. Send the email:
if (!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}
  1. Output:
Message has been sent

For more information, see the PHPMailer documentation and the Namecheap SMTP setup guide.

Edit this code on GitHub