Last active
May 27, 2021 07:32
-
-
Save A-Maged/7ebfd7918d26c63c9404c0b09b30c8de to your computer and use it in GitHub Desktop.
inline images with wp_mail
This file contains hidden or 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 | |
/* | |
* Send HTML Emails with inline images | |
*/ | |
class Custom_Mailer | |
{ | |
public $email_attachments = []; | |
public function send($to, $subject, $body, $headers, $attachments) | |
{ | |
/* Used by "phpmailer_init" hook to add attachments directly to PHPMailer */ | |
$this->email_attachments = $attachments; | |
/* Setup Before send email */ | |
add_action('phpmailer_init', [$this, 'add_attachments_to_php_mailer']); | |
add_filter('wp_mail_content_type', [$this, 'set_content_type']); | |
add_filter('wp_mail_from', [$this, 'set_wp_mail_from']); | |
add_filter('wp_mail_from_name', [$this, 'wp_mail_from_name']); | |
/* Send Email */ | |
$is_sent = wp_mail($to, $subject, $body, $headers); | |
/* Cleanup after send email */ | |
$this->email_attachments = []; | |
remove_action('phpmailer_init', [$this, 'add_attachments_to_php_mailer']); | |
remove_filter('wp_mail_content_type', [$this, 'set_content_type']); | |
remove_filter('wp_mail_from', [$this, 'set_wp_mail_from']); | |
remove_filter('wp_mail_from_name', [$this, 'wp_mail_from_name']); | |
return $is_sent; | |
} | |
public function add_attachments_to_php_mailer(&$phpmailer) | |
{ | |
$phpmailer->SMTPKeepAlive=true; | |
/* Sendgrid */ | |
if (defined('SENDGRID_PASSWORD')) { | |
$phpmailer->IsSMTP(); | |
$phpmailer->Host="smtp.sendgrid.net"; | |
$phpmailer->Port = 587; | |
$phpmailer->SMTPAuth = true; | |
$phpmailer->SMTPSecure = 'tls'; | |
$phpmailer->Username="apikey"; | |
$phpmailer->Password = SENDGRID_PASSWORD; /* api key from sendgrid */ | |
} | |
/* Add attachments to mail */ | |
foreach ($this->email_attachments as $attachment) { | |
if (file_exists($attachment['path'])) { | |
$phpmailer->AddEmbeddedImage($attachment['path'], $attachment['cid']); | |
} | |
} | |
} | |
public function set_content_type() | |
{ | |
return "text/html"; | |
} | |
public function set_wp_mail_from($email) | |
{ | |
//Make sure the email is from the same domain | |
//as your website to avoid being marked as spam. | |
return strip_tags(get_option('admin_email')); | |
} | |
public function wp_mail_from_name($name) | |
{ | |
return get_bloginfo('name'); | |
} | |
} | |
/* Usage */ | |
/* Set mail parameters */ | |
$to = '[email protected]'; | |
$subject = 'Inline Img'; | |
$body = '<h1>Image:</h1> <img src="cid:my_img_cid"/>'; | |
$headers = ""; | |
$my_attachments = [ | |
[ | |
"cid" => "my_img_cid", /* used in email body */ | |
"path" => plugin_dir_path(__FILE__) . '/my_img.png', | |
], | |
]; | |
$custom_mailer = new Custom_Mailer(); | |
$custom_mailer->send($to, $subject, $body, $headers, $my_attachments); |
This file contains hidden or 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 | |
global $global_attachments; | |
$global_attachments = []; | |
function add_attachments_to_php_mailer(&$phpmailer) | |
{ | |
/* Required */ | |
$phpmailer->SMTPKeepAlive=true; | |
$phpmailer->ContentType = "text/html"; | |
$phpmailer->From = strip_tags(get_option('admin_email')); | |
$phpmailer->FromName = get_bloginfo('name'); | |
/* Sendgrid */ | |
$phpmailer->IsSMTP(); | |
$phpmailer->Host="smtp.sendgrid.net"; | |
$phpmailer->Port = 587; | |
$phpmailer->SMTPAuth = true; | |
$phpmailer->SMTPSecure = 'tls'; | |
$phpmailer->Username="apikey"; | |
$phpmailer->Password = ''; /* api key from sendgrid */ | |
/* Add attachments to mail */ | |
global $global_attachments; | |
foreach ($global_attachments as $attachment) { | |
if (file_exists($attachment['path'])) { | |
$phpmailer->AddEmbeddedImage($attachment['path'], $attachment['cid']); | |
} | |
} | |
} | |
function send_mail($to, $subject, $body, $headers = "", $attachments = []) | |
{ | |
global $global_attachments; | |
/* Setup before sending email */ | |
$global_attachments = $attachments; | |
add_action('phpmailer_init', 'add_attachments_to_php_mailer'); | |
/* Send Email */ | |
$is_sent = wp_mail($to, $subject, $body, $headers); | |
/* Cleanup after email is sent */ | |
remove_action('phpmailer_init', 'add_attachments_to_php_mailer'); | |
$global_attachments = []; | |
return $is_sent; | |
} | |
/* Usage */ | |
/* Set mail parameters */ | |
$to = '[email protected]'; | |
$subject = 'Inline Img'; | |
$body = '<h1>Image:</h1> <img src="cid:my_img_cid"/>'; | |
$attachments = [ | |
[ | |
"cid" => "my_img_cid", /* used in email body */ | |
"path" => plugin_dir_path(__FILE__) . '/my_img.png', | |
], | |
]; | |
$is_sent = send_mail($to, $subject, $body, $headers, $attachments); | |
var_dump( $is_sent ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment