Can I put any type of custom shortcode for some content in mail body?


$to = "test@test.com";
$subject = "Test Email";

$packages = do_shortcode('[test id="0000"]');

$message = $packages;

// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// More headers
$headers .= 'From: <webmaster@example.com>' . "\r\n";


The shortcode generation should output a string, so that should in theory be possible if you know and trust the shortcode's output.

But many shortcodes are generated with the front-end in mind and within the loop context, so the mail context might not always work though, if they depend on conditional checks like is_single().

There is the shortcode_exists() function to make sure the shortcode exists.

It's also possible to use the shortcode's callback directly, instead of having to run the complicated parsing of do_shortcode().

You might also add some validations regarding the shortcode's output, before mailing it. Like do you want allow sending an empty string?

You should also hook your mailing code, after the shortcode has been added within the init action.

Note that WordPress uses the wp_mail() wrapper that plugins can hook into.

|improve this answer|||||

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.