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');
$mail->bodyHTML($bodyHTML);
$mail->attachment($myPage->files->first()->filename);

$count = $mail->send();
  • Like 13

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
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 :)

Share this post


Link to post
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" :)

Share this post


Link to post
Share on other sites

@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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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
 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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()

Share this post


Link to post
Share on other sites

You know that Mandrill did change their pricing policy, so you have to be a paying mailchimp customer to use their service?

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • 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.
    • By Noel Boss
      To create a new gitlab issue, I'd like to send an Email to the following email address using FormBuilder: 
      incoming+account/repository@incoming.gitlab.com
      unfortunately, the / in the email gets striped by the sanitizer:
      wire('sanitizer')->email(incoming+account/repository@incoming.gitlab.com'); // output: incoming+accountrepository@incoming.gitlab.com Is there any way to configure the sanitizer? Any other Ideas how to send the mail to this address?
       
      Ps.: I can not define the email format as it is defined by gitlab. The format is also kind of "common" and known as email-alias in the form of emailaccount+alias@domain.com – gmail uses it to label mails…
      https://help.wodify.com/hc/en-us/articles/234441508-How-Do-I-Create-an-Email-Alias-
×
×
  • Create New...