Mandrill inbound emails through Laravel / PHP

2019-04-13 03:35发布

问题:

I was wondering if someone could help me with a problem I have been having some trouble researching related to Laravel and inbound email processing through Mandrill. Basically I wish to be able to receive emails through Mandrill and store them within my Laravel database. Now i'm not sure if i'm reading through the documentation with the wrong kind of eyes, but Mandrill says it deals with inbound email as well as outbound, however i'm starting to think that Mandrill deals with inbound email details as opposed to the actual inbound email, such as if the message is sent etc.

I've created a new Mandrill account, created an API key, created an inbound domain and corresponding subdomain of my site (e.g. inboundmail.myproject.co.uk), set the MX record and the MX record is showing as valid. From there i have set up a route (e.g. queries@inboundmail.myproject.co.uk), and a corresponding webhook (myproject.co.uk/inboundmail.php) and within this webhook tried a variety of the examples given in the API (https://mandrillapp.com/api/docs/inbound.php.html), such as adding a new route, checking the route and attempting to add a new domain. All of them worked and produced the correct results, so my authentication with Mandrill is not in question, but my real question is is there a specific webhook for dealing with accepting incoming mail messages?

I cant help but feel like an absolute idiot asking this question as i'm sure the answer is either staring me in the face or just not possible through Mandrill.

Thanks in advance.

回答1:

Thanks to duellsy and debest for their help, in the end i found a script and expanded on it to add the mail to my own database and style / display it accordingly. Hope this helps someone who may have the same trouble:

<?php

require 'mandrill.php';

define('API_KEY', 'Your API Key');
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;
}
?>


回答2:

Like using webhooks from other mail parsing services, you'll need to make use of

file_get_contents("php://input")

This will give you the raw data from the webhook, which you can then json_decode and work with the results.