Last active
October 8, 2021 16:50
-
-
Save tobiasschutter/6b190f26a8e2cd240276 to your computer and use it in GitHub Desktop.
send-mail-with-mandrill-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 | |
/** | |
* Send Mail through Mandrill with Template | |
* | |
* See https://mandrillapp.com/api/docs/index.php.html | |
* | |
* @param string $to_email Email Reciever | |
* @param string $to_name Name Receiver | |
* @param string $subject Mail subject | |
* @param string $message HTML message ( allowed ul, p, h1 ) | |
* @param string $tag Keyword to tag type of email ( such as lost-pasword, new-account etc. ) Optionel. | |
*/ | |
function codepress_send_with_mandrill_template( $to_email, $subject, $message, $tag = '' ) { | |
if ( ! class_exists( 'Mandrill' ) ) { | |
echo 'Could not find Mandrill class'; | |
return; | |
} | |
// Email vars | |
$from_email = 'info@example.com'; | |
$from_name = 'Example Sender'; | |
$args = array( | |
'subject' => $subject, | |
'from_email' => $from_email, | |
'from_name' => $from_name, | |
'to' => array( | |
array( | |
'email' => $to_email, | |
'name' => '', | |
'type' => 'to' | |
) | |
), | |
'headers' => array(), | |
'important' => false, | |
'track_opens' => true, | |
'track_clicks' => true, | |
'auto_text' => true, | |
'auto_html' => false, | |
'inline_css' => true, | |
'tracking_domain' => null, | |
'signing_domain' => null, | |
'return_path_domain' => null, | |
'merge' => true, | |
'merge_language' => 'mailchimp', | |
'tags' => $tag ? array( $tag ) : null, | |
); | |
$mandrill = new Mandrill( '12345678901234567890' ); | |
$template_name = 'template-slug'; | |
$template_content = array( | |
array( | |
'name' => 'main', // mc:edit="main" | |
'content' => $message | |
) | |
); | |
// doc: https://mandrillapp.com/api/docs/messages.php.html#method=send-template | |
$result = $mandrill->messages->sendTemplate( $template_name, $template_content, $args ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment