0

I am running this in my localhost. when i publish a post nothing happens. I have added this in my themes functions.php file. How do i check the error? I dont see any error. I am new to development.

add_action('publish_post', 'sendnotification');
        
function sendnotification($post_id) {
   $post = get_post($post_id);
   $author = get_userdata($post->post_author);
   //  $user_email = get_the_author_meta( 'user_email' , $author_id );
   $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 )."
      Thanks"
   ;
   $sent = wp_mail($author->user_email, $subject, $message);
   if($sent) {
      echo "sent";
   }//mail sent!
   else  {
      echo "failed";
   }//message wasn't sent
}
James Z
  • 12,209
  • 10
  • 24
  • 44
  • 1
    What do you mean by "nothing happens"? What have you tried to resolve the problem? Where are you stuck? – Nico Haase Aug 12 '21 at 14:29
  • Use try { // your code (use it inside function) } catch (Exception $e) { echo 'Caught exception: ', $e->getMessage(), "\n"; } To check if there is a any error. https://www.php.net/manual/en/language.exceptions.php#language.exceptions.catch – r00t-err0r Aug 12 '21 at 14:44
  • 1
    https://stackoverflow.com/questions/15965376/how-to-configure-xampp-to-send-mail-from-localhost please see this thread – Sajjad Hossain Sagor Aug 12 '21 at 16:12
  • @NicoHaase - Nothing happens means mail is not sent. Nor i got any error. So i tried adding wp_error() and got below error when clicked on ADD NEW POST: WP_Error Object ( [errors] => Array ( [wp_mail_failed] => Array ( [0] => Invalid address: (From): wordpress@localhost ) ) Can you please suggest – Lavanya Reddy Aug 13 '21 at 07:03
  • Is there anything unclear about that error message? Also, please add all clarification to your question by editing it – Nico Haase Aug 14 '21 at 09:54
  • What OS ar you using? the mail (from PHP) works different for every one. For example, on linux you can do it easy installing and starting postfix. – kj_ Aug 14 '21 at 19:48
  • I am using MACBook Air, error indicates that the FROM address is wrong and says Invalid. i get an error--> [0] => Invalid address: (From): wordpress@localhost I understood that the issue is with from address. I need to set the from address. I did set from address using header like below : $headers = "From: Lavanya ;"; this doesn’t work. or should i use WP mail SMTP – Lavanya Reddy Aug 16 '21 at 07:37

0 Answers0