Jump to content



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');

$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


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->subject('The park is open');
// Add an attachment
$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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Similar Content

    • By fruid
      What's wrong with my code? It just won't send emails. It worked before when not using wiremailSMTP but now it stops. This is used in a contact form.
      It has to be the code because when I run a test in the module, it works fine and the email reaches me.
      Even the Promailer-Newsletters using WireMail SMTP work fine now.
      Even when I put some debug code on the template to be send to me via email with every email, that email reaches me. 
      But somehow the email in question, send with contact form does not.
      BTW if-condition works fine cause redirection works as well.
      Here's my code:
      if (isset($_POST['sendform']) && $captcha->verifyResponse() === true) :     $givenname = $input->post->text('givenname');      $familyname = $input->post->text('familyname');      $fullname = $givenname.' '.$familyname;     $fullname = ucwords($fullname);     $email = $input->post->email('emailaddress');      $subject = $input->post->text('subject');      $message = $input->post->text('message');      $m = wireMail();     $m->to('myemail@mydomain.com', 'my Name');     $m->from($email, $fullname);     $m->subject($subject);     $m->body($message);     $m->send();     $session->redirect($pages->get("/kontakt/danke/")->url); endif; I recently installed a SSL certificate on the webserver, but that should be irrelevant, right?
      Further settings:
      port: 587
      Allow Connection without Authentication: no
      SMTP User and SMTP Password provided
      Use Start-TLS: no
      TLS Crypto method: 1.2
      Use SSL: no
      Allow self signed certificated: no
      Thanks for help!
    • By ngrmm
      i'm trying to attach a .ics-file to a mail
      The Page send sends out the email but only download the ics-file. Email has no attachments.
      // event ID $eventID = $input->get('eventID','int'); $event = $pages->get($eventID); // config $testEmail = $event->event_mail_test_adress; $fromEmail = $event->event_mail_from; $fromName = $event->event_mail_from_name; $emailSubject = $event->event_subject; // .ics $filename = $event->name; header('Content-Encoding: UTF-8'); header('Content-type: text/calendar; charset=utf-8'); header("Content-Disposition: attachment; filename={$filename}.ics"); // fetch start date $event_start_ts = $event->getUnformatted("date_start"); // build the .ics data $ical_data = 'BEGIN:VCALENDAR'; $ical_data .= "\r\n"; $ical_data .= 'VERSION:2.0'; $ical_data .= "\r\n"; $ical_data .= 'CALSCALE:GREGORIAN'; $ical_data .= "\r\n"; $ical_data .= 'METHOD:PUBLISH'; $ical_data .= "\r\n"; $ical_data .= 'BEGIN:VEVENT'; $ical_data .= "\r\n"; $ical_data .= 'SUMMARY:'.$event->title; $ical_data .= "\r\n"; $ical_data .= 'UID:' . md5(uniqid(mt_rand(), true)) . '@'.$config->httpHost; $ical_data .= "\r\n"; $ical_data .= 'CLASS:PUBLIC'; $ical_data .= "\r\n"; $ical_data .= 'STATUS:CONFIRMED'; $ical_data .= "\r\n"; $ical_data .= 'DTSTART:'.date('Ymd', $event_start_ts).'T'.date("His", $event_start_ts); $ical_data .= "\r\n"; $ical_data .= 'DTSTAMP:'.date('Ymd').'T'.date('His'); $ical_data .= "\r\n"; $ical_data .= 'LOCATION:'.$event->venue; $ical_data .= "\r\n"; $ical_data .= 'URL:'.$event->httpUrl; $ical_data .= "\r\n"; $ical_data .= 'END:VEVENT'; $ical_data .= "\r\n"; $ical_data .= 'END:VCALENDAR'; // HTML BODY ob_start(); include('./_inc/emailbody.inc'); $emailBody = ob_get_clean(); // send email $m = new WireMail(); $m->to($testEmail); $m->from($fromEmail, $fromName); $m->subject($emailSubject); $m->bodyHTML($emailBody); $m->attachment($ical_data); $m->send();  
    • By ngrmm
      I have a page which sends out an email based on queries in the url
      <?php // event ID $eventID = $input->get('eventID','int'); $event = $pages->get($eventID); // config $adminEmail = "events@test.com"; $fromEmail = "noreply@test.com"; $fromName = "test"; $emailSubject = "Test Email"; // HTML BODY $emailBody = ""; // HOW TO ??? … // send mail $m = new WireMail(); $m->to($adminEmail); $m->from($fromEmail, $fromName); $m->subject($emailSubject); $m->bodyHTML($emailBody); $m->send(); ?> The email body is a bit complex: standard html/css tables and some php (pw variables).
      I put my emailbody (html) in a seperate file emailbody.inc but don't know how to include it?
    • By Mithlesh
      Hi there,
      My form is not getting submitted, it is showing:
      Unable to verify successful email delivery of this form submission.
      Attaching for your reference as well: 

      In the Backend, it is showing Connection timed out with smtp.gmail.com
      Pl guide me how to resolve that
    • By abdulqayyum
      hello processwire community,
      i am using LoginRegister module for front-end user registration. i am facing an issue verification email not receiving while WireMailSmtp is working perfectly and user receives message
      "Thank you, a confirmation code has been emailed to you. When you receive the email, click the link it contains, or paste the confirmation code below."
      received this message in front-end but does not receive any email relevant it.
      please help me in this case how i can solve it.
      Please check screen shots for SMTP test and message which receives user.
      Thanks AbdulQayyum,

      First screenshot SMTP test message, second when user receives message after clicking register button.
  • Create New...