Last active
July 29, 2024 21:04
-
-
Save tameemsafi/81725f0b8687244e3f4fcf2a0e46662e to your computer and use it in GitHub Desktop.
Send an email programmatically in wordpress with wp_mail using the woocommerce transaction emails template.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
$mailer->send( $email, $subject, $html_message, HTML_EMAIL_HEADERS ); | |
} | |
?> |
Thank you so much for this code ! Great
Great!
Why do you use wp_mail?
is it $mailer->send( $to, $subject, $message, $headers, $attachments ) a better way?
I agree with salvatore - use $mailer instead of wp_mail
Why do you use wp_mail?
is it $mailer->send( $to, $subject, $message, $headers, $attachments ) a better way?I agree with salvatore - use $mailer instead of
wp_mail
I have updated it to use the $mailer->send
method
Nice!
This filled in the missing piece for me — inline styles.
Thank you!!
Thank you for sharing.
Thank you so much
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why do you use wp_mail?
is it $mailer->send( $to, $subject, $message, $headers, $attachments ) a better way?