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 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-
    • By louisstephens
      I have been using the wiresmtpmail module in a few installs for sending out automated emails etc etc, and it works flawlessly. However, I now need to send an email when a user submits a form, and for the life of me I can not figure out how to get the form to submit.
      if(isset($_POST['Submit'])) { $mail = wireMail(); // calling an empty wireMail() returns a wireMail object $toName = $sanitizer->text($input->post->fullname); $toEmail = $sanitizer->email($input->post->email); $phone = $sanitizer->text($input->post->phone); $comments = $sanitizer->text($input->post->comments); $subject = $page->store_name . $page->title; $numSent = wireMail($toName, '', $subject, $textBody, $options); } If I remove the if statement, the form submits just fine, however you can just refresh the page (without adding anything to the fields), and it will submit a blank email every refresh. Has anyone gotten this working with a submit button?
    • By ihathome
      Hi,
      I'm an abolute newbie in processwire an it's modules. But I have read huge amounts of posts in this forum and as many docs and i learnt a lot in the last days. For a contact-form i used this great module (many thanks) and i hooked it my first time. Everything is alright. Now i search for a possibility to format or render the output in the email. Is there any posibility?
      Thank you for your replies
    • By Kiwi Chris
      Forms are an essential part of most websites, and it's no surprise that there's an excellent premium module Form Builder but what if you're on a zero budget for whatever reason?
      It is possible to build forms quickly and easily by making use of a couple of free modules and the admin UI to give you a great deal of flexibility and speed of development, particularly if you need multiple forms on a website with different fields.
      1. First you're going to need to install a couple of modules:
      Form Template Processor
      Fieldtype Select External Option
      2. For each form that you want to display, create a template without a template file and add fields to it as you normally would. (eg I have formContact, formRegister etc)
      Tip: under the Advanced tab in the setup for each template, I add a tag Forms so that all my forms templates are nicely grouped together in admin.
      3. Create a new field of type Select External Option and call it formTemplate
      In the section Create options from any database table select
      templates as the source table id as the Option Value name as the Option Label 4. Create a new template file and call it renderForm.php (or whatever else you like)
      Add an email field to this form - This will be the email address that forms get submitted to. Add the formTemplate field you previously created to this form. This will allow you to select which of the templates you previously created such as formContact, formRegister etc you want to render. Add any other fields as usual that you want to render on the page. Add the following PHP code to the template file. $recipient = $page->email; $form = $modules->get('FormTemplateProcessor'); $form->template = $templates->get($page->formTemplate->label); // required $form->requiredFields = array('contactName', 'contactEmail', 'contactMesssage'); //Optional: This can be improved by having a field in the page template with a CSV list of required fields eg $form->requiredFields = explode(',', $page->requiredFields) $form->email = $recipient; // optional, sends form as email. FormTemplateProcessor can also save forms to the database. $content .= $form->render(); //generate the form to display. Note: this doesn't actually render the form at this point, but you have it in the $content variable ready to output wherever you want in your template.
      Add any template HTML or other PHP code and echo $content; wherever you want to render the form.
      5. Create a page using the renderForm template, and provide an email address, and select a form that you want to display.
      6. Use CSS to style the form as required.
      7. View your new page, and check that the form renders correctly.
      8. You can modify the templates you created at step 2 or create new ones as required if your requirements for what fields forms display changes.
      Note: The Form Template Processor module can also save form input as pages, and the FieldType Select External Option can be set up with filtering, so this solution can probably be refined further.
    • By henri
      Hi Guys,
      i am a newbe to processwire. Have to maintain a website, but never worked with processwire. I have got an form that is submitted ( i do not have the form thing), to two email addresses. The last week the mails to the second address aren't delivered so it seems. Where do i find the email addresses where the form is submitted to?? I cant find them, though i can find the include of the form. Who knows?????
×
×
  • Create New...