1

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?

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 );
}); 
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 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.