Ask your WordPress questions! Pay money and get answers fast! (more info)

How can I add BCC to site notifications? WordPress

  • SOLVED

Hello,


If someone can post working code I will pay full amount to them - Time is against me. Thanks


How can I add BCC to this email notification function?


Thank you.



add_filter ("retrieve_password_message", "my_awesome_retrieve_password_message", 10, 2 );
function my_awesome_retrieve_password_message($content, $key) {

global $wpdb;
$user_login = $wpdb->get_var("SELECT user_login FROM $wpdb->users WHERE user_activation_key = '$key'");

ob_start();
$email_subject = my_awesome_retrieve_password_title();
include("email_header.php");

$user = get_user_by('login', $user_login);

?>

<p>hello</p>

<?php

include("email_footer.php");
$message = ob_get_contents();
ob_end_clean();
return $message;

}

Answers (4)

2013-02-19

Dbranes answers:

you might try something like (untested, but I added some debug stuff that you can comment out if you want):

add_filter( 'wp_mail','my_mail');
function my_mail($data){
// echo "</h2>Debug: Before change:</h2>";
//print_r($data);
$data['headers']="BCC: [email protected],[email protected] \n";
// echo "<h2>Debug: After change:</h2>";
// print_r($data);
//die("debug stop!");
return $data;
}


since <em>'wp_mail'</em> is a filter used by the <strong>wp_mail()</strong> function.

<strong>Edit:</strong>

To check out for <strong>action=lostpassword</strong> you can try:


add_filter( 'wp_mail','my_mail');
function my_mail($data){
if($_GET['action']=="lostpassword"){
$data['headers']="BCC: [email protected],[email protected] \r\n";
}
return $data;
}


Josh Cranwell comments:

PErfect This did the trick thank you!

2013-02-19

Francisco Javier Carazo Gil answers:

In headers of wp_mail you can define it:

$headers[] = 'From: Me Myself <[email protected]>';
$headers[] = 'Cc: John Q Codex <[email protected]>';
$headers[] = 'Cc: [email protected]'; // note you can just use a simple email address

wp_mail( $to, $subject, $message, $headers );


Francisco Javier Carazo Gil comments:

So instead this filter, you will have to modify an action which call the wp_mail. Directly you can send a differnt email in this hook:


This is the action: add_action( 'lostpassword_post', 'sendOtherMail' );

public function sendOtherMail()
{
// send me mail
}


Josh Cranwell comments:

What about the content and key variables?

Do I just write this... function sendOtherMail($content, $key)

Will the below work???


function my_awesome_retrieve_password_message($content, $key) {

$subject = my_awesome_retrieve_password_title();

$headers = 'From: '.get_bloginfo('name').' <'.get_bloginfo('admin_email').'>' . "\r\n";
$headers .= "Bcc: [email protected]" . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

ob_start();
$email_subject = my_awesome_retrieve_password_title();
include("email_header.php");

?>

<h3 style="padding:5px 0 0 0; font-size:18px; margin-top: 0;">
Create Password Confirmation
</h3>

<p>
<a href="<?php echo wp_login_url("url") ?>?action=rp&amp;key=<?php echo $key ?>&amp;login=<?php echo $user_login ?>" style="color: #ef272c; font-weight: bold; text-decoration: none;" title="Click here" target="_blank">Please click here to confirm and create your own password.</a>
</a>

<?php

include("email_footer.php");
$message = ob_get_contents();
ob_end_clean();

wp_mail( $email, $subject, $message, $headers, $attachments );

}
add_action( 'lostpassword_post', 'my_awesome_retrieve_password_message' );


Francisco Javier Carazo Gil comments:

I think it should be enough.


Josh Cranwell comments:

As I suspected, the email works fine.

But the variables are not passed.

And because of the $content, $key variables are not passed, then the email is useless as they cannot activate there account.


function my_awesome_retrieve_password_message($content, $key) {

$subject = my_awesome_retrieve_password_title();

$headers = 'From: '.get_bloginfo('name').' <'.get_bloginfo('admin_email').'>' . "\r\n";
$headers .= "Bcc: [email protected]" . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

ob_start();
$email_subject = my_awesome_retrieve_password_title();
include("email_header.php");

?>

<h3 style="padding:5px 0 0 0; font-size:18px; margin-top: 0;">
Create Password Confirmation
</h3>

<h1 style="padding:5px 0 0 0; font-size:24px;">
Hello <?php echo $user->first_name; ?>,
</h1>

<p>
You have requested to create a password via <a href="<?php echo wp_login_url("url") ?>" target="_blank" title="honda-mc-pressrides.com" style="color: #ef272c; text-decoration: none;">my-site.com</a>
</p>

<p>
<a href="<?php echo wp_login_url("url") ?>?action=rp&amp;key=<?php echo $key ?>&amp;login=<?php echo $user_login ?>" style="color: #ef272c; font-weight: bold; text-decoration: none;" title="Click here" target="_blank">Please click here to confirm and create your own password.</a>
</a>

<p>
If you did not request a new password, please ignore this email.
</p>

<?php

include("email_footer.php");
$message = ob_get_contents();
ob_end_clean();

wp_mail( $email, $subject, $message, $headers, $attachments );

}
add_action( 'lostpassword_post', 'my_awesome_retrieve_password_message' );


Francisco Javier Carazo Gil comments:

Yes, but ya can force to apply the filter with this function: http://codex.wordpress.org/Function_Reference/apply_filters and with this you will have the vars as you want.

2013-02-19

Arnav Joy answers:

read this article if you are using wp_mail function

http://codex.wordpress.org/Function_Reference/wp_mail

Using $headers To Set "From:", "Cc:" and "Bcc:" Parameters


Josh Cranwell comments:

I cant use headers in this...


add_filter ("retrieve_password_message", "my_awesome_retrieve_password_message", 10, 2 );
function my_awesome_retrieve_password_message($content, $key) {

global $wpdb;
$user_login = $wpdb->get_var("SELECT user_login FROM $wpdb->users WHERE user_activation_key = '$key'");

ob_start();
$email_subject = my_awesome_retrieve_password_title();
include("email_header.php");
$user = get_user_by('login', $user_login);

?>

<p>hello</p>

<?php

include("email_footer.php");
$message = ob_get_contents();
ob_end_clean();
return $message;

}

2013-02-19

Christianto answers:

If you just return $content or $key, is it work?
function my_awesome_retrieve_password_message($content, $key) {
return $content;
}


Josh Cranwell comments:

Would I do this...

function my_awesome_retrieve_password_message($content, $key) {

return $content;
return $key;

}


Christianto comments:

we can return one variable, $content or $key..
or combine both
function my_awesome_retrieve_password_message($content, $key) {
return $content.' this is key:'.$key;
}

You said that the value of $content and $key aren't pass to the function,
I check wp-login.php source code where this filter run, it should be pass correctly especially $message..


Josh Cranwell comments:

Ok i've tried this and it does not work. I'm struggling. I have active existing members flooding in and reseting passwords.

I need to set up a test site. It's on a multi site so dont know wether that is causing issue. See my latest code below you sugested...



function my_awesome_retrieve_password_message($content, $key) {

return $content;

$subject = my_awesome_retrieve_password_title();

$headers = 'From: '.get_bloginfo('name').' <'.get_bloginfo('admin_email').'>' . "\r\n";
$headers .= "Bcc: [email protected]" . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

ob_start();
$email_subject = my_awesome_retrieve_password_title();
include("email_header.php");

?>

<h3 style="padding:5px 0 0 0; font-size:18px; margin-top: 0;">
Create Password Confirmation
</h3>

<h1 style="padding:5px 0 0 0; font-size:24px;">
Hello <?php echo $user->first_name; ?>,
</h1>

<p>
You have requested to create a password via <a href="<?php echo wp_login_url("url") ?>" target="_blank" title="help.com" style="color: #ef272c; text-decoration: none;">help.com</a>
</p>

<p>
<a href="<?php echo wp_login_url("url") ?>?action=rp&amp;key=<?php echo $key ?>&amp;login=<?php echo $user_login ?>" style="color: #ef272c; font-weight: bold; text-decoration: none;" title="Click here" target="_blank">Please click here to confirm and create your own password.</a>
</a>

<p>
If you did not request a new password, please ignore this email.
</p>

<?php

include("email_footer.php");
$message = ob_get_contents();
ob_end_clean();

wp_mail( $email, $subject, $message, $headers, $attachments );

}
add_action( 'lostpassword_post', 'my_awesome_retrieve_password_message' );