Craig Posted May 13, 2015 Share Posted May 13, 2015 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(); 13 Link to comment Share on other sites More sharing options...
joey102030 Posted May 14, 2015 Share Posted May 14, 2015 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 More sharing options...
Craig Posted May 14, 2015 Author Share Posted May 14, 2015 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 More sharing options...
joey102030 Posted May 14, 2015 Share Posted May 14, 2015 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 More sharing options...
alan Posted May 14, 2015 Share Posted May 14, 2015 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. 4 Link to comment Share on other sites More sharing options...
jacmaes Posted May 14, 2015 Share Posted May 14, 2015 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! 3 Link to comment Share on other sites More sharing options...
cb2004 Posted May 22, 2015 Share Posted May 22, 2015 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 More sharing options...
Craig Posted May 22, 2015 Author Share Posted May 22, 2015 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 More sharing options...
teppo Posted May 23, 2015 Share Posted May 23, 2015 @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 More sharing options...
harmvandeven Posted June 11, 2015 Share Posted June 11, 2015 @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. 3 Link to comment Share on other sites More sharing options...
Pete Posted June 15, 2015 Share Posted June 15, 2015 Submitted a PR to add CC and BCC support. Needs testing because I'm in a hurry until this evening but I don't see why it wouldn't Link to comment Share on other sites More sharing options...
Pierre-Luc Posted June 25, 2015 Share Posted June 25, 2015 This is **amazing**! Implementing Mandrill using the API was actually on my to-do. Can check that off. Link to comment Share on other sites More sharing options...
muzzer Posted June 25, 2015 Share Posted June 25, 2015 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. 1 Link to comment Share on other sites More sharing options...
naldrocks98 Posted September 18, 2015 Share Posted September 18, 2015 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 advance Link to comment Share on other sites More sharing options...
Craig Posted September 18, 2015 Author Share Posted September 18, 2015 Hi naldrocks98, have you added a valid API key into the settings page of the module? The error and line number suggest that the Mandrill API library hasn't been initialised. Link to comment Share on other sites More sharing options...
benbyf Posted December 9, 2016 Share Posted December 9, 2016 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 More sharing options...
LostKobrakai Posted December 10, 2016 Share Posted December 10, 2016 You know that Mandrill did change their pricing policy, so you have to be a paying mailchimp customer to use their service? Link to comment Share on other sites More sharing options...
Jason Huck Posted March 28, 2017 Share Posted March 28, 2017 This seems to work just fine with PW 3.x, by the way. Thank you to the author! 2 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now