For example, I write a new post with content “Today I’m happy, because I finally met the girl that @David told me about bla blah…” Once the post is published, there should be an email notification sent to David about this post that mentions him. Is there already such plugin? If not please help me with my implementation. Thanks a million time! 🙂
The script needs to first parse the content of this new post. Find names after ‘@’, then go to database to match commenter’s name in order to get his/her email address (Assuming the person that I @ must have comment on my blog before)
The basic send email part is below.
function email_friend() {
$postTitle = get_the_title($id);
$post_permalink = get_permalink( $id );
$to = 'David@email.com';
$subject = 'Arch!tect mentioned you in his new post《'.$postTitle .'》';
$from = "noreplay@myWebsite.com";
$headers = "From:" . $from;
$message = "Arch!tect mentioned you in his new post《".$postTitle .
"》 check it out?nn" ."Post link:".$post_permalink
."nnnPlease don't reply this email.rn";
mail($to, $subject, $message, $headers);
}
add_action ( 'publish post', 'email_friend' );
Done with simple part!
Now please help me with the hard part…parsing name in post and fetching email address in database.
P.S.
Thank you all for helping! I managed to finish the code and it’s working fine now (tested). To mention people with name that has space in it, I’m using underscore first then change it back to space. It now send email to all people that I mention in my post and after doing that it will send a summary to myself with the result. I’ll paste the complete code below please have a look and tell me what I need to improve.
function email_friend() {
// get post object
$post = get_post($id);
// get post content
$content = $post->post_content;
// get how many people is mentioned in this post
//$mentionCount = preg_match_all('/(@(w)+)/', $content, $matches);
$mentionCount = preg_match_all('/(@[^s]+)/', $content, $matches);//support other lang
// if there is at least one @ with a name after it
if (0 !== $mentionCount) {
$friendList = array();//for storing correct names
for ($mentionIndex=0; $mentionIndex < $mentionCount; $mentionIndex++) {
$mentionName = $matches[0][$mentionIndex];
$mentionName = str_replace('_',' ',$mentionName); //change _ back to space
$mentionName = substr($mentionName, 1); //get rid of @
//for security and add wildcard
$friend_display_name_like = '%' . like_escape($mentionName) . '%';
global $wpdb;
// get correct name first
$friendCorrectName = $wpdb->get_var( $wpdb->prepare( "
SELECT comment_author
FROM $wpdb->comments
WHERE comment_author
LIKE %s ",
$friend_display_name_like
)) ;
// get friend email by comment author name
$friend_email = $wpdb->get_var( $wpdb->prepare( "
SELECT comment_author_email
FROM $wpdb->comments
WHERE comment_author
LIKE %s ",
$friendCorrectName
)) ;
if($friend_email) {// if found email address then email
$postTitle = get_the_title($id);
$post_permalink = get_permalink( $id );
$to = $friend_email;
$subject = 'Arch!tect mentioned you in his new post 《'.$postTitle .
'》';
$from = "noreplay@swotong.com";
$headers = "From:" . $from;
$message = "Arch!tect mentioned you in his new post《".$postTitle .
"》 check it out?nn" ."Post link:".$post_permalink
."nnnPlease don't reply this email.rn";
if(mail($to, $subject, $message, $headers)) {
//if send successfully put his/her name in my list
array_push($friendList, $friendCorrectName);
//array_push($friendList, $friend_email);
}
}
}
$comma_separated = implode(",", $friendList); //friend list array to string
// now send an email to myself about the result
$postTitle = get_the_title($id);
$post_permalink = get_permalink( $id );
$to = 'myOwn@email.com';
$subject = "Your new post《".$postTitle .
"》has notified ".count($friendList)."friends successfully";
$from = "noreplay@email.com";
$headers = "From:" . $from;
//list all friends that received my email
$message = "Your new post《".$postTitle .
"》has notified ".count($friendList)."friends successfully:nn".$comma_separated ;
mail($to, $subject, $message, $headers);
}
}//end of email_friend function
add_action ( 'publish_post', 'email_friend' );
Edit:
changed (w)+ to [^s]+ so it supports more languages. (Chinese tested)
Twice edited according to kaiser’s suggestions, unsecure version removed.
Use it only as starting point because:
John
from bothJohn Doe
andJohn Roe
will be e-mailed.Everything to be enhanced, but seems like ready to test.