Skip to content

Instantly share code, notes, and snippets.

@oomathias
Created June 25, 2013 21:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save oomathias/5862778 to your computer and use it in GitHub Desktop.
Save oomathias/5862778 to your computer and use it in GitHub Desktop.
Get your email with your own domain thanks to Mandrill. You need to host the script somewhere, and point Mandrill Inbound webhook to it. Don't forget to include the Mandrill PHP library. If you user composer, you just have to do composer install.
{
"require": {
"mandrill/mandrill": "1.0.34"
}
}
<?php
require 'vendor/mandrill/mandrill/src/Mandrill.php';
define('API_KEY', '123456789QWERTY');
define('TO_EMAIL', 'user@example.com');
define('TO_NAME', 'Foo Bar');
if(!isset($_POST['mandrill_events'])) {
echo 'A mandrill error occurred: Invalid mandrill_events';
exit;
}
$mail = array_pop(json_decode($_POST['mandrill_events']));
$attachments = array();
foreach ($mail->msg->attachments as $attachment) {
$attachments[] = array(
'type' => $attachment->type,
'name' => $attachment->name,
'content' => $attachment->content,
);
}
$headers = array();
// Support only Reply-to header
if(isset($mail->msg->headers->{'Reply-to'})) {
$headers[] = array('Reply-to' => $mail->msg->headers->{'Reply-to'});
}
try {
$mandrill = new Mandrill(API_KEY);
$message = array(
'html' => $mail->msg->html,
'text' => $mail->msg->text,
'subject' => $mail->msg->subject,
'from_email' => $mail->msg->from_email,
'from_name' => $mail->msg->from_name,
'to' => array(
array(
'email' => TO_EMAIL,
'name' => TO_NAME,
)
),
'attachments' => $attachments,
'headers' => $headers,
);
$async = false;
$result = $mandrill->messages->send($message, $async);
print_r($result);
} catch(Mandrill_Error $e) {
// Mandrill errors are thrown as exceptions
echo 'A mandrill error occurred: ' . get_class($e) . ' - ' . $e->getMessage();
// A mandrill error occurred: Mandrill_PaymentRequired - This feature is only available for accounts with a positive balance.
throw $e;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment