Jump to content

WireMailMandrill


Craig
 Share

Recommended Posts

Want to use Mandrill for sending emails using their HTTP API rather than SMTP? Read on! :)

I've been working on a WireMailMandrill module for a site I'm currently adding some features to. So far, the module has been tested for simple mail sending, with basic options, and attachments. I haven't tested the full range of things that are possible with the official Mandrill PHP library, but I think I've implemented the ability to set most, if not all, of them.

This should be considered beta and not entirely relied upon for sites in production. Testing and feedback welcome. I'm sorry about the lack of code comments at the moment as well, I was just throwing this together as I went along so I could move onto the next part of what I was building...

This was originally mentioned in a discussion about SendGrid and Mandrill options in a thread in the Form Builder support area. Not everyone has access to that, which is why I'm putting this here :)

WireMailMandrill on GitHub

Quick example (taken directly from a site I'm working on):

$mail = wireMail();

$mail->from('info@example.com', 'ExampleCOM Enterprises');
$mail->to($toMail, $toName);
$mail->subject('Entry confirmation');
$mail->bodyHTML($bodyHTML);
$mail->attachment($myPage->files->first()->filename);

$count = $mail->send();
  • Like 13
Link to comment
Share on other sites

Hi Craig

I've actually been thinking about the same thing for a while.

In my mind, one of the main benefits of the API over SMTP is the ability to setup webhooks to track actions against each sent email.

Therefore I had envisaged each email first being saved to the pagetree before being sent.

Then each action against that email (eg, open, click, bounce) to be captured and saved back to the pagetree as children of the sent email.

I hope that makes sense. I've not tried your module yet, but I've had a look through your code and it all looks great.

Link to comment
Share on other sites

Hi

That's an interesting point! I haven't used that feature before but definitely can see how it's worth doing.

In other systems my company has developed, we often keep a log of all messages sent purely for auditing and recording purposes, so there is merit in keeping those purely for that - but if that can also help with things like tracking stuff that's great.

The WireMail ___send function is hookable - perhaps another module could be created which just hooks into this and logs all messages, either in its own custom table or as a page.

Link to comment
Share on other sites

I currently use a bespoke (non PW) system where outgoing emails are queued in a DB table, and a cron job sends the queue through Mandrill every 5 minutes. This method was originally used to eliminate the lag associated with sending through an external SMTP server, but actually having all emails permanently stored in the database offers loads of other unexpected benefits.

I'm not sure how sending in this way could be best done in a PW module. I wouldn't like to leave it up to lazycron, so maybe the choice of immediate sending, or alternatively a cron job would need to be setup manually to fire a PW url every x minutes, which would be responsible for clearing the queue.

I'm not sure the best way to illustrate a pagetree here, but I would actually envisage the top level being the email templates themselves, then the children being the outgoing emails, and the children of those being the actions captured from the webhook.

The benefits from a module like this would be huge. Apart from just being able to see instantly which emails have sent successfully or not, a simple selector could show information like which is the most popular link in an email, or which emails aren't being opened at all.

It's good to know the WireMail __send function is hookable, this should mean the internal emails sent from PW (eg forgotten password) could be intercepted and forced into the new Mandrill system

Link to comment
Share on other sites

This is great Craig, thanks. These guys certainly note the benefits of using their system via API rather than SMTP:

The REST API is the primary API for the service and the SMTP endpoint is considered a migration route. Using SMTP access avoids big changes to your code base.

Not tried their system but it looks good, like another Mandrill.

The benefits of using Mandrill as an intermediary for sending email are many. For me, switching to Mandrill (using Horst's excellent Wire Mail SMTP module) meant that all emails got through—I no longer had occasional false spam fails.

  • Like 4
Link to comment
Share on other sites

The benefits of using Mandrill as an intermediary for sending email are many. For me, switching to Mandrill (using Horst's excellent Wire Mail SMTP module) meant that all emails got through—I no longer had occasional false spam fails.

This is exactly what I'm doing, and I've had the same experience: no more false SPAM. Such a great service. This new module is going to open up new possibilities I'm sure!

  • Like 3
Link to comment
Share on other sites

  • 2 weeks later...

Nice module and works well. Would be nice if I could set email headers, so I could set the From: on my email to always be an email address on my domain, and then a Reply-To: would be the person filling in the form. That way I can setup DKIM and SPF on my domain. I am thinking this may be a restriction of WireMail itself though?

Link to comment
Share on other sites

This looks to be supported by WireMail itself, so it should work with WireMailMandrill. Using the example above, try adding this line:

$mail->header('Reply-To', 'sender@example.com'); 

I haven't tested it but please give it a go :)

Link to comment
Share on other sites

@Craig: just wanted to say that this looks great; going to give it a proper try soon!

How 'bout adding this to the modules directory? There's a "beta" option there, if you're hesitant about calling it "stable" :)

Link to comment
Share on other sites

  • 3 weeks later...

@Craig: first of all thanks for this module! 

I did run into a small issue while using it on a SSL server. Mandrill gave me a "Unable to get local issuer SSL certificate" error and did not want to send any e-mails. It was a relatively easy fix but it does require changes in the Mandrill.php.

I don't know if it should be changed in the module, but I'm posting it here for anybody else who runs headfirst into this error.

1) I downloaded the following file, renamed it to carcert.pem and placed it in the Mandrill directory.

2) I added the following code after line 72 of Mandrill.php

curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, TRUE); 
curl_setopt($this->ch, CURLOPT_CAINFO, __DIR__ . "/cacert.pem");
And boom! It works (well for me at least).
I hope this helps somebody.
  • Like 3
Link to comment
Share on other sites

  • 2 weeks later...

Have to agree Pierre-Luc, mandrill is pretty sharp. I've been using it for about four weeks with this module and it's been 100% reliable (which I can't say for my previous SMTP setup. The API is sweet and the dashboard is pretty bloody good to boot. This is one system I'll use again and again. Cheers Craig for getting me onto this.

  • Like 1
Link to comment
Share on other sites

  • 2 months later...

I get this error when i used the sample code for mail

$mail = wireMail();
$mail->from('naldrocks98@gmail.com', 'Raynald Jan Umali');
$mail->to('enald@localhost.com');
$mail->subject('The park is open');
$mail->bodyHTML("sample");
// Add an attachment
//$mail->attachment($somePage->files->first()->filename);
$count = $mail->send();

what is the possible way to get fix this?

thanks in advancepost-3301-0-63312600-1442539921_thumb.pn
 

Link to comment
Share on other sites

  • 1 year later...

this module still working with Mandril? I'm trying it on the mailchimp trial. cannt seem to get an emali sent after setting up their DNS settings and using the example wireMail() example im getting 0 for $count = $mail->send()

Link to comment
Share on other sites

  • 3 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...