I am using a cron event and adding a PHP code to send emails to users of my wordpress site. But due to some reason my wp_mail function is sending emails twice to all the users.
Here is my code, can anyone please tell me whats wrong with my code ?
$args = array(
'role' => 'employee',
'order' => 'ASC'
);
$all_users = get_users($args);
foreach ($all_users as $user) {
$to = esc_html($user->user_email) ;
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: TEST <test#test.com>' . "\r\n";
$subject = "Regarding auto emails";
$message = "<p>Dear Employee/Consultant,</p>
<p >Please send in your submissions</p>";
wp_mail($to, $subject, $message, $headers);
}
You can write mail function in this way
if(wp_mail($to, $subject, $message, $headers)){ // Condition check if mail has been sent
continue; // Continue to next iteration
}
Your PHP looks okay to me.
I have had luck scheduling cron in WordPress using the approach shared here (duplicated below). And by structuring the mail function within loops as follows:
<?php
$to = 'email#website.com';
$subj = 'Subject';
$body ='
<br/>
Dear Recipient,<br/>
<br/>
Here is an email.<br/>
<br/>';
$headers[] = 'Content-Type: text/html; charset=UTF-8';
$headers[] = 'From: Website.com <email#website.com>' . "\r\n";
wp_mail( $to, $subj, $body, $headers );
?>
You can install a plugin like Cron Manager to verify that the cron event is scheduled. "custom_cron_event" (or whatever name you choose for your function) should be listed as a scheduled cron if you've activated the plugin and it is working correctly.
Cron schedule via plugin:
<?php
/*
Plugin Name: Custom Plugin
Plugin URI:
Description: Adds function on cron
Author:
Version: 1.0
*/
/*
* When this plugin is activated, schedule
*/
function activate_custom_cron(){
wp_schedule_event( time(), 'daily', 'do_custom_cron_event');
}
register_activation_hook(__FILE__, 'activate_custom_cron');
/*
* When this plugin is deactivated, clear_schedule cron
*/
function deactivate_custom_cron(){
wp_clear_scheduled_hook('do_custom_cron_event');
}
register_deactivation_hook(__FILE__, 'deactivate_custom_cron');
/*
* Function
*/
function custom_cron_event() {
/*
* This is where your function goes
*/
}
add_action( 'do_custom_cron_event', 'custom_cron_event' );
?>
Related
I'm new in plugin development. I would like to use this simple function in Wordpress to send mail to users. I saw this code in the documentation everything is simple and straight forward but this code returns false. Why ?
require_once explode( 'wp-content', __FILE__ )[0] . 'wp-load.php';
function send_mail() {
$to = 't.testmail#gmail.com';
$subject = 'The subject';
$body = 'The email body content';
$headers = array( 'Content-Type: text/html; charset=UTF-8' );
$send_message = wp_mail( $to, $subject, $body, $headers );
if ( $send_message ) {
echo 'Email was sent';
} else {
echo 'Email sending was aborted';
}
}
send_mail();
According to the documentation (https://developer.wordpress.org/reference/functions/wp_mail/) the mail could not be sent.
Reasons could be many. Have you checked the debug log of WordPress (https://wordpress.org/support/article/debugging-in-wordpress/)?
I have a WordPress site where users can post from the front-end and the post status goes as Draft.
Now when I publish the post from Admin panel, the notification email is sent more than one time. I need to send email once.
Below my code:
if (is_admin()) {
function notifyauthor($post_id) {
$post = get_post($post_id);
$author = get_userdata($post->post_author);
$subject = "Post publish notification";
$headers = 'From: '.get_bloginfo( 'name' ).' <my_email#gmail.com>' . "\r\n";
$message = "
Hi ".$author->display_name.",
Your post, \"".$post->post_title."\" has just been published.
View post: ".get_permalink( $post_id )."
Thank You, Admin"
;
wp_mail($author->user_email, $subject, $message, $headers);
}
add_action('publish_post', 'notifyauthor');
}
I tried current_user_can('administrator') insteed to is_admin(), but same result I got.
Many hooks will actually run more than one time. The simple solution is to add a counter by way of post_meta after the first iteration, then check it doesn't exist. This isn't tested, but should work.
function notifyauthor($post_id) {
if (is_admin() && !(metadata_exists('post', $post_id, 'sent_notification_email'))) {
$post = get_post($post_id);
$author = get_userdata($post->post_author);
$subject = "Post publish notification";
$headers = 'From: '.get_bloginfo( 'name' ).' <my_email#gmail.com>' . "\r\n";
$message = "
Hi ".$author->display_name.",
Your post, \"".$post->post_title."\" has just been published.
View post: ".get_permalink( $post_id )."
Thank You, Admin";
wp_mail($author->user_email, $subject, $message, $headers);
// Set a meta key as a counter
update_post_meta($post_id, 'sent_notification_email', '1');
}
}
add_action('publish_post', 'notifyauthor');
I'm trying to send have my site send me a notification email when someone registers with the role subscriber. I can probably achieve that with a hook on mu-plugins but I don't know where to start or which hook to use. Tried adding an if statement inside a plugin but it's probably overkill to install a plugin and modify it just for this functionality. Thanks!
For your information the default register user role is "Subscriber" role, if you have set the other role you have to change the role after registered.
Try this one:
function send_welcome_email_to_new_user($user_id) {
$user = get_userdata($user_id);
$user_email = $user->user_email;
// email will send only for "Subscriber" registers
if ( in_array( 'subscriber', $user->roles )) {
$to = $user_email;
$subject = "Hi";
$body = '
<p>your message </p>
';
$headers = array('Content-Type: text/html; charset=UTF-8');
if (wp_mail($to, $subject, $body, $headers)) {
error_log("email has been successfully sent to user whose email is " . $user_email);
}
}
// email will send only for "Other Role" registers
if ( in_array( 'other_role', $user->roles )) {
$to = $user_email;
$subject = "Hi";
$body = '
<p>your message </p>
';
$headers = array('Content-Type: text/html; charset=UTF-8');
if (wp_mail($to, $subject, $body, $headers)) {
error_log("email has been successfully sent to user whose email is " . $user_email);
}
}
}
add_action('user_register', 'send_welcome_email_to_new_user');
It is enabled by default in WordPress. Check that WordPress is sending emails at all, you might have problem with the SMTP settings, you need to configure the SMTP. Where is your website hosted? Did you check the spam?
I am sending all my WordPress emails using Mailgun SMTP.
Other emails from WordPress arrive to the inbox fine, but I have just created a new function to automatically generate an email when a post is published and it always gets caught by the spam filter.
Can you see why?
Maybe something to do with my headers config?
I have tried sending with and without the attachment, it makes no difference.
add_action( 'publish_post', 'notify_on_publish' );
function notify_on_publish( $post_id ) {
global $post;
$from_name = get_bloginfo( 'name' );
$from_email = get_bloginfo( 'admin_email' );
$to_email = get_post_meta( $post_id, 'contact_email', true );
$attachment = get_post_meta( $post_id, 'post_image', true );
$headers = "From: '$from_name' <$from_email> \r\n Content-type: text/html; charset=" . get_option('blog_charset') . "\r\n";
$subject = "This is a test subject";
$message = "<p>This is some test body text</p>";
$attachment_url = $attachment['guid'];
wp_mail( $to_email, $subject, $message, $headers, $attachment_url );
}
I am trying to send mail when a post is published. For that I wrote code in my function.php file, the mail is correctly sending, but the featured image is not sent. I want to display the featured image attached to the post. Right now in mail the featured image is not displayed, but the link of fthe eatured image is displayed.
What can I do to achieve my task to display the featured image in the mail? I am attaching the code I have written in function.php file:
function mysendmail($post_id) {
$post = get_post($post_id);
$author = get_userdata($post->post_author);
$subject = "Post Published: ".$post->post_title."";
$message = "
Hi ".$author->display_name.",
Your post, \"".$post->post_title."\" has just been published.
View post: ".get_permalink( $post_id )."
Your Image: ".get_the_post_thumbnail( $post->ID )."
Thanks"
;
wp_mail($author->user_email, $subject, $message);
}
add_action('publish_post', 'mysendmail');
To attach file through wp_mail function, you need to use $attachments parameter in it. In which you need to give absolute file path of attachment.
function mysendmail($post_id) {
$post = get_post($post_id);
$author = get_userdata($post->post_author);
$subject = "Post Published: ".$post->post_title."";
$attachments = get_attached_file( get_post_thumbnail_id( $post_id ));
$headers[] = '';
$message = "
Hi ".$author->display_name.",
Your post, \"".$post->post_title."\" has just been published.
View post: ".get_permalink( $post_id )."
Thanks"
;
wp_mail($author->user_email, $subject, $message, $headers, $attachments);
}
add_action('publish_post', 'mysendmail');
You need to set the content type to text/html for the image to work. You can do that using the wp_mail_content_type filter or by adding a header into your wp_mail() function. Here's an example of the latter.
function mysendmail($post_id) {
$post = get_post($post_id);
$author = get_userdata($post->post_author);
$subject = "Post Published: ".$post->post_title."";
$headers = array('Content-Type: text/html; charset=UTF-8');
$message = "
Hi ".$author->display_name.",
Your post, \"".$post->post_title."\" has just been published.
View post: ".get_permalink( $post_id )."
Your Image: ".get_the_post_thumbnail( $post->ID )."
Thanks"
;
wp_mail($author->user_email, $subject, $message, $headers);
}
add_action('publish_post', 'mysendmail');
Try this one, just tested on a local install and it works well :) you need to change the content type for the mail as discussed, also im returning the actual URL of the image (large size).
function set_html_content_type() {
return 'text/html';
}
function mysendmail($post_id) {
add_filter( 'wp_mail_content_type', 'set_html_content_type' );
$post = get_post($post_id);
$author = get_userdata($post->post_author);
$subject = "Post Published: ".$post->post_title."";
$image = get_the_post_thumbnail_url($post_id, 'large');
$message = '
Hi '.$author->display_name.',<br/><br/>
Your post, '.$post->post_title.' has just been published.<br/>
View post: '.get_permalink( $post_id ).'<br/>
Your Image: <img src="'.$image.'" /><br/><br/>
Thanks';
wp_mail($author->user_email, $subject, $message);
remove_filter( 'wp_mail_content_type', 'set_html_content_type' );
}
add_action('publish_post', 'mysendmail');