Send Email to multiple users when custom post or post is publish by admin
Step 1 . Download PHPMailer from Download and save extract file on path "/wp-content/themes/funding_press" with directory name "PHPMailer".
Step 2 . Paste this code in function.php file.
<?php
/*------------------------ Send Mail when Post publish by admin START -------------------------------*/
function cause_published() {
global $post;
global $wpdb;
$cause_id = $post->ID;
$posttitle = get_the_title( $cause_id );
$permalink = get_permalink( $cause_id );
$cause_cate = wp_get_object_terms($cause_id, 'project-category'); //"project-category" is a texonimy name of custom post type "project".
foreach($cause_cate as $cat) {
$cause_category = $cat->name;
$usersarrays = $wpdb->get_results("SELECT `user_email` FROM `wp_users` WHERE `ID` IN (SELECT `user_id` FROM `wp_usermeta` WHERE `meta_key` = 'user_category' AND `meta_value` = '".$cause_category."')"); //This is the query to get users email id according to condition
//print_r($usersarray);
foreach($usersarrays as $usersarrayss) {
$user_email[] = $usersarrayss->user_email; //Create array of email id of multiple users
}
//print_r($user_email); die();
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->From = 'BackYourCause';
$mail->FromName = 'BackYourCause';
foreach($user_email as $user_email_my)
{
$mail->addAddress($user_email_my);
}
//$mail->addAddress($email); // Add a recipient
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->isHTML(true);
$message ="
<table>
<tr><td><strong>Congratulations</strong></td></tr>
<tr><td style='font-weight: bold;'>Your cause has been successfully submitted.</td></tr>
<tr><td>Your cause will be published once reviewed by our admin staff.</td></tr>
<tr><td>Your Cause ID is <strong>$cause_id</strong> and Click <a href=$permalink><strong>$posttitle</strong></a> to open cause.</td></tr>
<tr><td></td></tr>
</table>
";
$mail->Subject = 'BackYourCause';
$mail->Body = $message;
$mail->Send();
return $post_ID;
}
}
add_action('publish_project', 'cause_published'); //"publish_project" is a hook call when post publish.
//"publish_project" is 'publish_{custom_post_name}' is hook for custom post type post where "project" is a name of custom post type.
/*------------------------ Send Mail when Post publish by admin END -------------------------------*/
?>
Step 1 . Download PHPMailer from Download and save extract file on path "/wp-content/themes/funding_press" with directory name "PHPMailer".
Step 2 . Paste this code in function.php file.
<?php
/*------------------------ Send Mail when Post publish by admin START -------------------------------*/
function cause_published() {
global $post;
global $wpdb;
$cause_id = $post->ID;
$posttitle = get_the_title( $cause_id );
$permalink = get_permalink( $cause_id );
$cause_cate = wp_get_object_terms($cause_id, 'project-category'); //"project-category" is a texonimy name of custom post type "project".
foreach($cause_cate as $cat) {
$cause_category = $cat->name;
$usersarrays = $wpdb->get_results("SELECT `user_email` FROM `wp_users` WHERE `ID` IN (SELECT `user_id` FROM `wp_usermeta` WHERE `meta_key` = 'user_category' AND `meta_value` = '".$cause_category."')"); //This is the query to get users email id according to condition
//print_r($usersarray);
foreach($usersarrays as $usersarrayss) {
$user_email[] = $usersarrayss->user_email; //Create array of email id of multiple users
}
//print_r($user_email); die();
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->From = 'BackYourCause';
$mail->FromName = 'BackYourCause';
foreach($user_email as $user_email_my)
{
$mail->addAddress($user_email_my);
}
//$mail->addAddress($email); // Add a recipient
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->isHTML(true);
$message ="
<table>
<tr><td><strong>Congratulations</strong></td></tr>
<tr><td style='font-weight: bold;'>Your cause has been successfully submitted.</td></tr>
<tr><td>Your cause will be published once reviewed by our admin staff.</td></tr>
<tr><td>Your Cause ID is <strong>$cause_id</strong> and Click <a href=$permalink><strong>$posttitle</strong></a> to open cause.</td></tr>
<tr><td></td></tr>
</table>
";
$mail->Subject = 'BackYourCause';
$mail->Body = $message;
$mail->Send();
return $post_ID;
}
}
add_action('publish_project', 'cause_published'); //"publish_project" is a hook call when post publish.
//"publish_project" is 'publish_{custom_post_name}' is hook for custom post type post where "project" is a name of custom post type.
/*------------------------ Send Mail when Post publish by admin END -------------------------------*/
?>
No comments:
Post a Comment