This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
WordPress has its own email function for sending emails, i.e. wp_mail(). With wp_mail, you can send email as easily as in general PHP function: mail().
The following is the syntax for sending an email with WordPress:
weplugins_wp_mail( $to, $subject, $message, $headers, $attachments );
Where:
- $to (required) is the intended recipients. You can also specify multiple recipients by using an array or comma-separated email ids.
- $subject (required) is the subject of your message.
- $message (required) is the content of your message.
- $headers (optional) is the mail headers to be sent with the message.
- $attachments (optional) is the file name, to be attached to the message. You can also define the filenames with an array.
Since $headers and $attachments are optional parameters, a simple example would look like this:
weplugins_wp_mail("To Email","Email Subject", "Message");
Set HTML Content Type in wp_mail Function
WP Core Emails ProCustomize and manage your WordPress emails with ease— no coding required.
Personalize Emails - Get Started
|
If your message contains the HTML tags, you’ve to set the HTML content type to ‘text/html’ as follows:
add_filter( 'wp_mail_content_type', 'weplugins_set_html_content_type' ); weplugins_wp_mail( 'To Email', 'Subject', '<h1>Html Email</h1>' ); function weplugins_set_html_content_type() { return 'text/html'; }
And don’t forget to use the following filter after using wp_mail(), as to avoid conflicts:
remove_filter( 'wp_mail_content_type', 'weplugins_set_html_content_type' );
Add CC/BCC Recipients in wp_mail Function
If you want to use extra information with the message, like From, CC, BCC, you can simply put those in $headers.
$headers[] = "From: YourName <first email>"; $headers[] = "Cc: ReceiverName <second email>"; $headers[] = "Bcc: Receiver2Name <third email>";
If you want to attach a file with your message, you can simply put the file name as follows:
$attachments = array (WP_CONTENT_DIR . '/uploads/file_to_attach.doc');
Send Emails Using SMTP in WordPress
The function wp_mail() requires you to configure the SMTP at your server. To configure the SMTP, you have to use the PHPMailer Class. This class contains the SMTP settings and is responsible for sending emails. To use the PHPMailer Class, we’ve to use action: phpmailer_init as follows:
add_action( 'phpmailer_init', 'weplugins_configure_smtp' ); function weplugins_configure_smtp( PHPMailer $phpmailer ){ $phpmailer->isSMTP(); //switch to smtp $phpmailer->Host = 'mail.mydomain.com'; $phpmailer->SMTPAuth = true; $phpmailer->Port = 25; $phpmailer->Username = 'Username Here'; $phpmailer->Password = 'myemailpassword'; $phpmailer->SMTPSecure = false; $phpmailer->From = 'From Email Here'; $phpmailer->FromName = 'Sender Name'; }
Inside of our function, we simply call isSMTP(), set SMTPAuth to true, and provide the appropriate SMTP host and credentials. This is all we have to do to make WordPress send emails through our SMTP server.
Display SMTP Errors in WordPress
Still, sometimes email couldn’t send successfully and there is no way to check SMTP Errors without doing a little hack in wp_mail() function.
if(!$phpmailer->Send()) { echo 'Message could not be sent.'; echo 'Mailer Error: ' . $phpmailer->ErrorInfo; exit; }
WP Core Emails ProCustomize and manage your WordPress emails with ease— no coding required.
Personalize Emails - Get Started
|
Explore the latest in WordPress
Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.