admin管理员组

文章数量:1279177

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

eg:

$to = "[email protected]";
$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: <[email protected]>' . "\r\n";

mail($to,$subject,$message,$headers)

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

eg:

$to = "[email protected]";
$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: <[email protected]>' . "\r\n";

mail($to,$subject,$message,$headers)
Share Improve this question edited Feb 7, 2019 at 19:49 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Jan 8, 2019 at 17:39 Pratik PatelPratik Patel 1,1111 gold badge11 silver badges23 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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.

本文标签: Can I use shortcodes in mail body