Skip to content

Instantly share code, notes, and snippets.

@tameemsafi
Last active July 29, 2024 21:04
Show Gist options
  • Select an option

  • Save tameemsafi/81725f0b8687244e3f4fcf2a0e46662e to your computer and use it in GitHub Desktop.

Select an option

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.
<?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 );
}
?>
@giovanemachado
Copy link
Copy Markdown

That's very nice!

@kshirodpatel
Copy link
Copy Markdown

Hello,
Thanks for sharing this code.
How can we change the email from name and email from address here?

@tameemsafi
Copy link
Copy Markdown
Author

@kshirodpatel you can replace the HTML_EMAIL_HEADERS array with the one below.

$headers = array(
  'Content-Type: text/html; charset=UTF-8',
  'From: Person Name <email@here.com>'
);

wp_mail( $email, $subject, $html_message, $headers );

@ahsan-alii
Copy link
Copy Markdown

ahsan-alii commented Jul 15, 2020

@tameemsafi Thank you so much, I was stuck on this issue for hours, but your simple code made it as smooth as butter for me.
But I've a little problem that is whatever I type in From in headers arrays it is not shown, rather WordPress is written in the actual Email

@AlexSkir
Copy link
Copy Markdown

AlexSkir commented Nov 8, 2020

Thank you very much!!!!
Finally, a working and beautiful piece of code!

@salvatore-esposito
Copy link
Copy Markdown

Why do you use wp_mail?

is it $mailer->send( $to, $subject, $message, $headers, $attachments ) a better way?

@M1ckael
Copy link
Copy Markdown

M1ckael commented Feb 4, 2021

Thank you so much for this code ! Great

@elron
Copy link
Copy Markdown

elron commented Jul 18, 2021

Great!

@mkdizajn
Copy link
Copy Markdown

mkdizajn commented Oct 10, 2021

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

@tameemsafi
Copy link
Copy Markdown
Author

tameemsafi commented Oct 10, 2021

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

@cap340
Copy link
Copy Markdown

cap340 commented Oct 11, 2021

Nice!

@mmgroner
Copy link
Copy Markdown

mmgroner commented Jul 5, 2022

This filled in the missing piece for me — inline styles.
Thank you!!

@PedroSantos-Dev
Copy link
Copy Markdown

Thank you for sharing.

@sonomzy
Copy link
Copy Markdown

sonomzy commented Jul 29, 2024

Thank you so much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment