wp_mail_from: different from address each time - wordpress

I have a form. Where user gives their name and email address. The I use the custom plugin to send the mail. The requirement is I need to send the mail on behalf of the user who signed.
Now in wp_mail how to achieve that?
I know about this filter: wp_mail_from. But how to call it every time wp_mail is called and set different from address?
Finally I also want to clear the wp_mail_from filter so that it doesn't affect the other forms.
Thanks,

wp_mail accepts "headers"
http://codex.wordpress.org/Function_Reference/wp_mail#Using_.24headers_To_Set_.22From:.22.2C_.22Cc:.22_and_.22Bcc:.22_Parameters
<?php
$headers = 'From: My Name <myname#example.com>' . "\r\n";
wp_mail('test#example.org', 'subject', 'message', $headers );
?>

Related

WordPress notifications not received in G-mail with altered header information

I have customized the default welcome e-mail send out by WordPress when you add a user, but when I change the default "From" information it no longer gets delivered to #gmail.com e-mailaddresses. I have tried it with multiple accounts, but always the same results. I haven't had any problems with #hotmail.com or any custom domain e-mailaddresses.
Below is the function I used to alter the default e-mail:
// Change the default welcome e-mail
add_filter( 'wp_new_user_notification_email', 'welcome__email', 10, 3 );
function welcome__email( $wp_new_user_notification_email, $user, $blogname ) {
$wp_new_user_notification_email['subject'] = sprintf(__( 'Company Name | Complete registration' ), $blogname, $user->user_login );
// Set password link
$key = get_password_reset_key( $user );
// Build the email
$message = sprintf(__('Welcome!')) . "\r\n\r\n";
$message .= 'By clicking the link below you can activate your account:' . "\r\n";
$message .= network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user->user_login), 'login') . "\r\n\r\n";
$message .= "After setting a password you can login." . "\r\n";
$wp_new_user_notification_email['message'] = $message;
// Change header information
$wp_new_user_notification_email['headers'] = 'From: Company Name <noreply#example.com>';
return $wp_new_user_notification_email;
}
If I comment out the last part about the 'headers' the e-mail with link to set a password does get delivered to #gmail.com e-mailaddresses, but ofcourse the default name and e-mailaddress are show then.
I can't see what I am missing out here to make sure the e-mails get delivered to #gmail.com addresses, so I'm hoping anybody here is able to help me in the right direction.
PS: I do not have any SMPT plug-in set-up.
I just noticed the e-mails are not send to #gmail.com addresses because the sender e-mailaddress does not belong to the same domain as where the website is on. Once these match, there no longer is an issue.

Sending mail to thousands of customers at a time ( wp_mail -Wordpress)

I want to send a total of 1650 mails on Christmas eve from a WordPress website.
This is my current code:
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=" . get_bloginfo('charset') . "" . "\r\n";
$customers = get_users($args);
$cus_arr = array();
foreach($customers as $customer){
array_push($cus_arr,$customer->data->user_email);
}
wp_mail($cus_arr, $subject, $message, $headers);
/*another way*/
...
foreach($customers as $customer){
wp_mail($customer->data->user_email, $subject, $message, $headers);
}
Question 1 : Is the above way a good choice for sending a mail to this amount of customers? May there be any problem? Is there any better way of doing it?
Also I dont want customers to see each others mail. This is my current code:
wp_mail(array("BCC: xyz#xyz.com","BCC: abc#abc.com"), $subject, $message, $headers);
But it does not work. Without BCC: the mails are actually sent.
Question 2. How can I prevent users from seeing other mail ids?
Question 1: It's good practice to use a rate limit(throttling), e.g. max 100 mails/10 minutes. You can do this by storing the recipients in a database, and use a cronjob to send mail every 10 minutes. Another option is to use a 3rd party mail service, such as Mandrill or Sendgrid, they'll handle throttling for you.
Question 2: Your current code calls wp_mail for each recipient, so they'll never see other mail addresses (no need for BCC headers). However if you decide to use wp_mail to send to multiple recipients in 1 call, you need to use bcc headers, e.g.:
wp_mail('', $subject, $message, array("BCC: xyz#xyz.com","BCC: abc#abc.com"));
Hey there I think your code is a bad idea because of the following reasons:
The script may exceed the max execution time or the memory limit of the server
it may work, but wp_mail is just not a good solution for sending this huge amount of emails
What I would recommend is to use a library for sending bulk e-mails. There are existing WordPress Plugins for this purpose however you can easily do it yourself - here is my solution with the classic, well tested PHPMailerLibrary (https://github.com/PHPMailer/PHPMailer):
In your functions.php:
require("libs/phpmailer/PHPMailerAutoload.php");
/**
#param $from: The senders E-Mail
#param $from_name: The senders Name
#param $subject: The E-Mail subject
#param $mesesage: The E-Mail content HTML
#param $to: An array of receivers
*/
function my_custom_send_mass_mail($from,$from_name,$subject,$message,$to) {
// first use PHPMailer to send all the emails
$email = new PHPMailer();
$email->isSMTP();
$email->CharSet = 'utf-8';
$email->From = $from;
$email->FromName = $from_name;
$email->Subject = $subject;
$email->Body = $message;
$email->IsHTML();
if(is_array($to)) {
foreach($to as $t) {
$email->addBCC($t);
}
}
$ac = time();
update_option('mailsent-'.$ac,$email);
$success = $email->send();
if($success) {
update_option('mailsent-suc-'.$ac,"yes");
return "Bulk E-Mail successfully sent.";
}
update_option('mailsent-suc-'.$ac,$email->ErrorInfo);
return "Bulk E-Mail Error: Please contact admin.";
}
Just call this function and make sure $to is the array of receiver e-mails.
This is the solution I am using for a customer sending a 10000 E-Mail newsletter weekly and it works great so far.

Buddypress - activation link not sent to user's email address

I recently installed buddypress in my wordpress.
When a person registers on my website using registration form
a message appears saying that an activation link has been sent to the email address you provided, but user receives no email.
Please tell me to solve this.
is there any problem in wp_mail or do I need to config
something in my hosting file manager?
First , test the wp_mail function with the following code by creating wp-test.php in root folder.
include 'wp-load.php';
$to = 'example#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);
After that if it's still not working , you need to install and configure Easy WP SMTP and setup the SMTP settings from your host.

sending a mail with Drupal's user_save

I have a website that has to insert users in a Drupal database. I include the Drupal bootstrap file and call the user_save() function.
The paramenters I pass to user_save are: the first one a stdClass with property 'status'=1, and the second parameter is (what's expected to be sent, because the insert works just fine).
The problem I'm having is that the user receives no confirmation email. I think user_save should send the user an email, but it doesn't. Maybe I'm missing something here, so your help is much appreciated!
The second paramater should be an array:
user_save($account, array('status' => 1));
You can always look at http://api.drupal.org/api/drupal/modules%21user%21user.module/function/user_save/6 to understand the user_save function in detail..
Just to make sure, you have checked the settings for sending email on registration?
admin/config/people/accounts
Sometimes there are problem with email on crappy servers/some hosts are limiting the php mail function. Here is a simple php mail call you could try if you're not sure if the mail-function runs without problem on your server.
<?php
$to = 'youremail#yourhost.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: youremail#yourhost.com' . "\r\n" . 'Reply-To: youremail#yourhost.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>

Wordpress Php auto email to comment author

I'd love to be able to automatically send a response to the person who comments on a post on my site. Their email is required so I feel as though I should be able to grab that and use php to send an email back to that email address...
I know the basics for a php email go as follows... So I just need help grabbing the authors email and putting it into the mailTo variable
<?php
$subject = 'My subject';
$message = "The Message I'd like to send back to the commenter";
$mailTo = get_comment_author_email_link
mail($mailTo, $subject, $message);
?>
Thanks!
I think what you need is to hook to the comment post action with your defined own function as such:
<?php
function sendMail($id){
$subject = 'My subject';
$message = "The Message I'd like to send back to the commenter";
$comment=get_comment($id);
$mailTo = $comment->comment_author_email ;
mail($mailTo, $subject, $message);
}
add_action('comment_post', 'sendMail');
?>
you can use this , but dont forget the comment of webarto :
http://wordpress.org/extend/plugins/wp-comment-auto-responder/

Resources