WordPress has its own email functionality, which calls wp_mail for sending emails. WordPress wp_mail function is allowing you the same functionality as PHP is giving to you so let’s try and use it.
Usage:
<?php wp_mail( $to, $subject, $message, $headers, $attachments ); ?>
- $to: (string or array) (required) The intended recipient(s). Multiple recipients may be specified using an array or a comma-separated string. Default: None
- $subject: (string) (required) The subject of the message. Default: None
- $message: (string) (required) Message content. Default: None
- $headers: (string or array) (optional) Mail headers to send with the message. For the string version, each header line (beginning with From:, Cc:, etc.) is delimited with a newline (“\r\n”) (advanced) Default: Empty
- $attachments: (string or array) (optional) Files to attach: a single filename, an array of filenames, or a newline-delimited string list of multiple filenames. (advanced) Default: Empty
Examples of wp_mail():
Sending an email with simple way:
<?php wp_mail( 'somebody@example.com', 'The subject', 'The message' ); ?>
Sending an email to multiple recipients:
<?php $multiple_to_recipients = array( 'somebody@example.com', 'somebodyelse@example.com' ); $subject = 'The subject'; $message = 'The message'; $headers[] = 'From: webmaster <webmaster@example.com>'; $headers[] = 'Cc: somebody <somebody@example.com>'; $headers[] = 'Cc: somebodyelse@example.com'; // note you can just use a simple email address wp_mail( $multiple_to_recipients, $subject, $message, $headers ); ?>
Sending an email with the attachment:
<?php $to = 'somebody@example.com'; $subject = 'The subject'; $message = 'The message'; $attachments = array( WP_CONTENT_DIR . '/uploads/2013/07/wallpaper-2407801-150x150.jpg' ); $headers[] = 'From: webmaster <webmaster@example.com>'; add_filter( 'wp_mail_content_type', 'my_custom_email_content_type' ); return wp_mail( $to, $subject, $message, $headers, $attachments ); function my_custom_email_content_type() { return 'text/html'; } ?>
Keep visiting for new stuff and give your feedback.
Happy Coding 😉