phpmailerHow can I use PHPMailer with Drupal?
Using PHPMailer with Drupal is a quick and easy way to send emails from your Drupal site. The following example code shows how to use PHPMailer in your Drupal site:
<?php
// Include PHPMailer library
require 'PHPMailer/PHPMailerAutoload.php';
// Create new PHPMailer instance
$mail = new PHPMailer;
// Set mailer to use SMTP
$mail->isSMTP();
// Specify main and backup SMTP servers
$mail->Host = 'smtp1.example.com;smtp2.example.com';
// Enable SMTP authentication
$mail->SMTPAuth = true;
// SMTP username
$mail->Username = '[email protected]';
// SMTP password
$mail->Password = 'secret';
// Set from address
$mail->setFrom('[email protected]', 'Mailer');
// Set to address
$mail->addAddress('[email protected]', 'John Doe');
// Set subject
$mail->Subject = 'Here is the subject';
// Set body
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
// Send email
if (!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
Output example
Message has been sent
The code above will send an email from [email protected]
to [email protected]
using SMTP authentication and the specified SMTP servers. The email will have the subject Here is the subject
and the body This is the HTML message body <b>in bold!</b>
.
Code explanation
require 'PHPMailer/PHPMailerAutoload.php';
: This line includes the PHPMailer library.$mail = new PHPMailer;
: This line creates a new PHPMailer instance.$mail->isSMTP();
: This line sets the mailer to use SMTP.$mail->Host = 'smtp1.example.com;smtp2.example.com';
: This line specifies the main and backup SMTP servers.$mail->SMTPAuth = true;
: This line enables SMTP authentication.$mail->Username = '[email protected]';
: This line sets the SMTP username.$mail->Password = 'secret';
: This line sets the SMTP password.$mail->setFrom('[email protected]', 'Mailer');
: This line sets the from address.$mail->addAddress('[email protected]', 'John Doe');
: This line sets the to address.$mail->Subject = 'Here is the subject';
: This line sets the subject.$mail->Body = 'This is the HTML message body <b>in bold!</b>';
: This line sets the body.if (!$mail->send()) {
: This line checks if the email was sent successfully.echo 'Message could not be sent.';
: This line prints an error message if the email was not sent successfully.echo 'Message has been sent';
: This line prints a success message if the email was sent successfully.
Helpful links
More of Phpmailer
- How do I use PHPMailer to attach a ZIP file?
- How can I set up PHPMailer to use Zimbra SMTP?
- How can I configure PHPMailer to support Polish characters?
- How can I configure PHPMailer to work with GoDaddy?
- How can I use PHPMailer to send emails with a Yahoo account?
- How do I use PHPMailer to encode emails in UTF-8?
- How can I use PHPMailer in Yii 1?
- How can I use PHPMailer with XAMPP on Windows?
- How do I use PHPMailer with Yii2?
- How can I set the hostname for PHPMailer?
See more codes...