1
votes

I am trying to send a email out after a post is published in wordpress. Got it sending no problem, looks great, fully responsive etc.

However, the only thing is that most of my post content are custom meta. In my mail function I use get_post_meta to get all the fields and then print them into the HTML email.

For example:

function booking_confirmation_email() {
$guestname = get_post_meta( $post->ID, 'guestname', true );
$phone = get_post_meta( $post->ID, 'phone', true);
$guestemail = get_post_meta( $post->ID, 'email', true);

//The recipients    
$title = get_the_title( $ID );
$to = '[email protected]',$guestemail;
$subject = 'Booking Confirmation : ' . $title; 
$headers = 'From: Serviced City Pads';

//the message
$message = '
<loads of html>
<p>' . $title . '</p>
<p>' . $guestname . '</p>
<p>' . $phone . '</p>
<p>' . $guestemail . '</p>
</loads of html>
';

//send the email
wp_mail( $to, $subject, $message, $headers, $attachments );
}

add_action('publish_bookings', 'booking_confirmation_email');

Obviously Ive stripped this back a bit but you get the idea. The problem is nothing but the hard coded email address, $title, HTML and styling works. The custom meta values dont seem to show. They all save in the post, so I know thats working. Assume its down to the way Im calling it all into the message.

Thanks in advance

1
Where is the $ID variable coming from? Seems like if the title is working with that you could use it for the other variables as well over $post->IDjohnnyd23
I see what your saying. Ill give it a go.Alex Knopp

1 Answers

2
votes

Add this to the top of your function

global $post

Please then replace the $post->id with the get_post_meta function with:

$post->ID

Thanks.