Hot answers tagged

88

As an alternative, you can specify the Content-Type HTTP header in the $headers parameter: $to = 'sendto@example.com'; $subject = 'The subject'; $body = 'The email body content'; $headers = array('Content-Type: text/html; charset=UTF-8'); wp_mail( $to, $subject, $body, $headers );


58

from wp_mail codex page: The default content type is 'text/plain' which does not allow using HTML. However, you can set the content type of the email by using the 'wp_mail_content_type' filter. // In theme's functions.php or plug-in code: function wpse27856_set_content_type(){ return "text/html"; } add_filter( 'wp_mail_content_type','...


31

First of all, if we take a look at implementation of wp_mail function, we will see that this function uses PHPMailer class to send emails. Also we could notice that there is hard coded function call $phpmailer->IsMail();, which sets to use PHP's mail() function. It means that we can't use SMTP settings with it. We need to call isSMTP function of PHPMailer ...


23

wp_mail() is a pluggable function: It can be replaced by plugins. That’s useful in cases where the regular mail() doesn’t work (good enough), for example when you need extra authentication details. Example: WP Mail SMTP wp_mail() uses PHPMailer by default, a sophisticated PHP class which offers a lot of useful preprocessing and workarounds for cases where ...


21

The $attachment argument for wp_mail takes a file (or array of files) - but the file path has to be fully specified. For example: <?php $attachments = array(WP_CONTENT_DIR . '/uploads/file_to_attach.zip'); $headers = 'From: My Name <myname@mydomain.com>' . "\r\n"; wp_mail('test@test.com', 'subject', 'message', $headers, $attachments); ?>...


15

The following version of wp_mail() is with the patch applied of @rmccue/@MattyRob in the ticket https://core.trac.wordpress.org/ticket/15448, refreshed for 4.2.2, which allows $message to be an array containing content-type keyed alternates: /** * Send mail, similar to PHP's mail * * A true return value does not automatically mean that the user received ...


13

If I'm understanding you right you just are having trouble getting the from set? The easiest way would just be adding the from in the email headers in the wp_mail function. Here is an example of one of my old simple email sending functions that works: function contact_send() { $title = 'New message Received'; $headers = array('From: '.$_POST['...


13

Using the PHPMailer class with an action callback: I did some digging into the PHPMailer class and found that it supports a custom action. Here's how the callback is activated with the doCallback() method in the class. There's also a PHPMailer test on GitHub using this feature via the callbackAction() callback. We can set it up in WordPress with: $...


10

Don't forget to remove the content type filter after you use the wp_mail function. Following the accepted answer naming you should do this after wp_mail is executed: remove_filter( 'wp_mail_content_type','wpse27856_set_content_type' ); Check this ticket here - Reset content-type to avoid conflicts -- http://core.trac.wordpress.org/ticket/23578


10

You may call the function too early. You have to wait until the action 'plugins_loaded' fires. wp_mail() is defined in wp-includes/pluggable.php. pluggable.php is loaded in wp-settings.php after the plugins are loaded but before 'plugins_loaded' is called. See this answer for an example.


10

The default content type is 'text/plain' which does not allow using HTML. You can set the content type of the email by including a header like "Content-type: text/html" $headers = 'Content-type: text/html;charset=utf-8' . "\r\n"; $headers .= 'From: XXXXXX.com <info@xxxxx.com>' . "\r\n"; $subject = 'Registration from xxxxx.com' . "\r\n"; $...


9

Step by step: First find the file where the error message appear. I use Notepad++ and the CTRL + F command to search in files. It is a good idea to search only the first few words of the error message, because some error messages are combined of different messages. Your error message appear in wp-login.php and holy luck, only there. So let's have a look why ...


9

Yes it's possible, $to accepts an array or comma-separated list of email addresses to send message. You can read more here, about optional headers parameter that can add from, cc, content-type, fields. If you want to send automaticaly WordPress admin notifications, you can have a look to this.


7

Addition to @EugeneManuilov answer. SMTP settings By default those can only get - as @EugeneManuilov already answered - be set by during a callback attached to an do_action_ref_array(). Source/core. <?php defined( 'ABSPATH' ) OR exit; /** * Plugin Name: (WCM) PHPMailer SMTP Settings * Description: Enables SMTP servers, SSL/TSL authentication and SMTP ...


7

Finally, I wrote some code and it worked very well. I hope it helps. Put this in your functions.php file //remove sitename from email subject add_filter('wp_mail', 'email_subject_remove_sitename'); function email_subject_remove_sitename($email) { $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES); $email['subject'] = str_replace("["...


7

Thanks to the above, I've realized my central mistake -- I didn't quite realize that the arguments being passed in were a multi-dimensional array. For now, I've re-implemented the function thus: function ws_add_site_header($email) { $email['headers'][] = 'X-WU-Site: ' . parse_url(get_site_url(), PHP_URL_HOST) ; return $email; } My ...


6

You can try something like this in your functions.php function send_comment_email_notification( $comment_ID, $commentdata ) { $comment = get_comment( $comment_id ); $postid = $comment->comment_post_ID; $master_email = get_post_meta( $postid, 'master_email', true); if( isset( $master_email ) && is_email( $master_email ) ) { ...


6

You can use the 'wp_mail_failed' action to catch a send error. https://developer.wordpress.org/reference/hooks/wp_mail_failed/


6

Attachments should always use the absolute filesystem path. Also to change the Content-Type of the email you should use the wp_mail_content_type filter. <?php function my_custom_email() { $to = 'my@email.com'; $subject = 'WordPress wp_mail'; $message = ' <html> <body> <table rules="all" style="border-color: #...


6

the given answers have good intentions. But are not good. There are more parameters to the filter function call, 4 to be exact. this worked for me for the message (i wanted to replace username with user email) note we have a multi site page. You might want to replace network_site_url with get_site_url() typically in your functions.php of your wordpress ...


6

$headers can be a string or an array, but it may be easiest to use in the array form. To use it, push a string onto the array, starting with "From:", "Bcc:" or "Cc:" (note the use of the ":"), followed by a valid email address. https://codex.wordpress.org/Function_Reference/wp_mail#Using_.24headers_To_Set_.22From:.22.2C_.22Cc:.22_and_....


6

Here's an alternative using directly the AddCustomHeader method of the PHPMailer instance: /** * Add a custom header. * $name value can be overloaded to contain * both header name and value (name:value) * @access public * @param string $name Custom header name * @param string $value Header value * @return void */ public function addCustomHeader($...


6

Unfortunately this is not possible, not because WordPress does not support it, but because email does not support it. There is no way to specify multiple Reply To: addresses in an Email. If you did, it would be stripped out by mail servers as invalid. Instead, consider using a mailing list and use the mailing lists address as the reply to field


5

You could try to debug the output like this: function test_phpmailer_init( $phpmailer ) { echo '<pre>'; var_dump( $phpmailer ); echo '</pre>'; return $phpmailer; } add_action( 'phpmailer_init', 'test_phpmailer_init' ); The code in your question is correct, the problem is with your local SMTP application. If you are using a ...


5

You can filter the $phpmailer object. Something like this should do the trick (not tested): add_action( 'phpmailer_init', 'wpse_52555_msg_id' ); function wpse_52555_msg_id( &$phpmailer ) { $msg_id = get_post_meta( get_the_ID(), 'messageID', TRUE ); '' !== $msg_id and $phpmailer->MessageID = $msg_id . '@test.com'; }


5

The function get_template_part() doesn't return the HTML but echo it (it uses locate_template() which loads the file - echo). You can either turn output buffering on using ob_start() and put the buffer in to your variable or you can use file_get_contents(). On your case I think the best solution is this: $body = file_get_contents(TEMPLATEPATH . 'includes/...


5

Per my comment to your question, I believe the problem is that includeing files, whether directly or using get_template_part isn't likely to give you a string to pass to $body and that is going to cause errors in the code, or at the very least unespected behavior. I would avoid reading files into memory and just create a function that returns your $body ...


5

What am i doing wrong here? Many, many things, not all performance related. Lets begin with the critical parts, then conclude on your performance issues and what can be done to mitigate and help AJAX API Firstly, you're not using the AJAX API, and reinventing the wheel. It's quite simple: JS: $.post( do_mail_sending.ajaxurl, { action: '...


5

You can use the following two hooks to change name and email address Use the following in your active theme's functions.php file. add_filter( 'wp_mail_from', 'wpse_new_mail_from' ); function wpse_new_mail_from( $old ) { return 'your email address'; // Edit it with your email address } add_filter('wp_mail_from_name', 'wpse_new_mail_from_name'); ...


5

There are multiple ways of doing this. You can consider any of the following. 1.My preferred: foreach($group_emails as $email_address) { wp_mail($email_address, 'my subject', 'my message', $headers); } 2.Another way Define the array as follows. $group_emails = array('ceri@test.com', 'craigj@test.com', 'danyob@test.com', 'geoffh@test.com', 'ianc@test....


Only top voted, non community-wiki answers of a minimum length are eligible