Skip to content

Instantly share code, notes, and snippets.

@tobiasschutter
Last active October 8, 2021 16:50
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobiasschutter/6b190f26a8e2cd240276 to your computer and use it in GitHub Desktop.
Save tobiasschutter/6b190f26a8e2cd240276 to your computer and use it in GitHub Desktop.
send-mail-with-mandrill-template
<?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