Using wp_schedule_event to send emails including links

I’m trying to write a simple plugin using wp_schedule_event to send emails.
The first example below works fine, but in the second (where I try to include a url within the email) nothing happens.

Why? And how can I include a link within the email?

Read More

EXAMPLE 1

add_action('init', function() {
    if ( !wp_next_scheduled('my_email') ) {
    wp_schedule_event(time(), 'hourly', 'my_email' );
    }
});

add_action('my_email', function() {
$body = "A text message including a link.";
    wp_mail('myaddress@gmail.com', 'Email Subject', $body );
}); 

EXAMPLE 2

add_action('init', function() {
    if ( !wp_next_scheduled('my_email') ) {
    wp_schedule_event(time(), 'hourly', 'my_email' );
    }
});

add_action('my_email', function() {
$my_url = site_url('/resources/links/', 'http');
$link ="<a href=" . $my_url . ">link</a>";
$body = "A text message including a $link.";
    wp_mail('myaddress@gmail.com', 'Email Subject', $body );
}); 

Related posts

Leave a Reply

1 comment

  1. I guess the problem is just a syntax error..

    it should read:

    $body = "A text message including a {$link} .";
    

    but in this way it will just print out the html as text. you must also add headers to tell that it’s an html email.

    $to = 'sendto@example.com';
    $subject = 'The subject';
    $body = 'The email body content';
    $headers = array('Content-Type: text/html; charset=UTF-8');
    
    wp_mail( $to, $subject, $body, $headers );