Skip to content

Instantly share code, notes, and snippets.

@mkdizajn
Forked from tameemsafi/emails.php
Created October 10, 2021 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkdizajn/5c4d0118c3f7b9f29aeef512bb5c32e4 to your computer and use it in GitHub Desktop.
Save mkdizajn/5c4d0118c3f7b9f29aeef512bb5c32e4 to your computer and use it in GitHub Desktop.
Send an email programmatically in wordpress with wp_mail using the woocommerce transaction emails template.
<?php
// Define a constant to use with html emails
define("HTML_EMAIL_HEADERS", array('Content-Type: text/html; charset=UTF-8'));
// @email - Email address of the reciever
// @subject - Subject of the email
// @heading - Heading to place inside of the woocommerce template
// @message - Body content (can be HTML)
function send_email_woocommerce_style($email, $subject, $heading, $message) {
// Get woocommerce mailer from instance
$mailer = WC()->mailer();
// Wrap message using woocommerce html email template
$wrapped_message = $mailer->wrap_message($heading, $message);
// Create new WC_Email instance
$wc_email = new WC_Email;
// Style the wrapped message with woocommerce inline styles
$html_message = $wc_email->style_inline($wrapped_message);
// Send the email using wordpress mail function
wp_mail( $email, $subject, $html_message, HTML_EMAIL_HEADERS );
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment