Using wpmandrill to send subscription notification from bbPress

The support forums for BulkWP runs on bbPress and I use wpMandrill to send all emails from BulkWP through the Mandrill App email service from MailChimp. Recently in bbPress v2.5.4 the logic to send topic subscription emails was changed in a major way which broke it for people who were using bbPress with wpMandrill. After spending almost a day debugging I finally got everything to work and thought of documenting it here so that it will be useful for others as well.

What changed in bbPress?

In bbPress till v2.5.4 whenever a new reply was posed to a topic, individual emails were sent to all people who have subscribed to that topic. The major downside to this approach is that if a topic has a lot of subscribers then it resulted in a major bottleneck and in most cases the script used to timeout or the mail server used to crash.

To fix this, in v2.5.4 all subscriber email addresses were moved to bcc and only a single email was triggered for all the subscribers. Also in the “to” a generic do not reply email address was used that was calculated using the domain name.

How this broke wpMandrill?

Unfortunately the above code change broke emails sent through wpMandrill in more than one ways.

Bugs in generating the “do not reply” email address

The following were the different bugs in the code that generated the “do not reply” email address.

  • The generated “do not reply” email address which was specified in the “to” filed was enclosed in angle bracket, which Mandrill API didn’t like.
  • If the domain name started with s, then it was stripped. eg: if the domain name is https://ssssean.com/, then the email address was noreply@ean.com
  • If the domain name has www, then the generated “do not reply” address also had ‘www’ in the email domain name. eg: noreply@www.example.com

All of these are documented in this post in bbPress support forum.

No BCC support in Mandrill API

Mandrill API doesn’t support more than one bcc email address and the wpMandrill plugin just used the first bcc address and silently ignored the others. Since the new change added all the subscribed email address to bcc, the email was sent only to the first email address and the rest were silently ignored.

How to fix these issues?

All the issues in generating the “do not reply” email address were fixed in this ticket. In addition to fixing the bugs, two more filters are introduced that allows you to change the default email address to something else if needed. The following are the filter names

  • bbp_get_do_not_reply_address
  • bbp_subscription_from_email
  • bbp_subscription_to_email

The changes are already merged into trunk and hopefully should be available as part of the next release. Till then you might have to manually edit the core file to change the default “do not reply” email address. The relevant code is in bbpress/includes/common/function.php file in the bbp_notify_subscribers function (around line number 1065)

Update: The above changes are now available from bbPress 2.5.6. If you are using bbPress 2.5.6 or above then you can change the “do not reply” address using the filters instead of manually editing the core files.

Regarding the issue with bcc support in Mandrill API, I found a way to trigger individual emails for a given list of email address after going over the Mandrill API documentation. Basically you have to sent the different email addresses as array in the “to” file with type set to “bcc” and then set the option preserve_recipients option to false. This will make Mandrill to trigger individual emails for each of the email address.

After I found about this, I looked into wpMandrill plugin source code to see if this can be done by hooking into the mandrill_payload filter. But unfortunately that was not possible. After some more debugging I finally found that it would be possible to this by hooking the wp_mail filter.

I quickly wrote a small plugin which hooks into wp_mail filter and converts all “bcc” address into the correct format that Mandrill expects. You can checkout the plugin in github. One word of caution. Even though this plugin works in my server, I have not fully tested all the corner cases. So do test this first in your dev server before installing it in a live site.

Limitations

The above plugin only works for the bbp_notify_subscribers function. But there is a similar function called bbp_notify_forum_subscribers which sends emails to all people who have subscribed to a forum when a new reply is posted.

Try it out and let me know if you face any issues and hopefully it will be useful for other people who also use bbPress with wpMandrill like me 🙂

22 comments

  1. Stephen White

    This is a handy plug in, Worked fine in test server but on live I do get an error – “trim() expects parameter 1 to be string, array given – ” everything executes and emails send. Just looking to make error page not show. Advice?

    • Sudar

      Can you post the entire error message?

  2. Jeremy Biron

    Thanks for sharing this plugin! Your plugin is working great to send my bbpress subscriptions with one exception.

    Unfortunately the WordPress system is still sending the regular emails to users as well when this function is triggered. So each time a reply email is sent, the user gets one from Mandrill and one from WordPress. Any ideas on what might be causing this?

    It’s probably something stupid I’m doing. Thanks again for putting this plugin together!

    • Sudar

      Hello Jeremy,

      That’s really strange. Is it happening for all emails that are sent through Mandrill in your WordPress installation or only for emails that are sent through bbPress?

      • Jeremy Biron

        Thanks for the reply Sudar. This issue is not happening with any other emails that WordPress is sending. It’s only with the bbpress emails.

        • Sudar

          Were you having this issue before you installed my plugin?

        • Sudar

          I was thinking about it a bit more and now I am thinking whether this might be because of some other plugins. Are you using any other plugins that do anything with email?

  3. Jeremy Biron

    Sorry for the delayed reply on this. Haven’t had time to look at it.

    Here are the only plugins I have enabled currently:
    bbpress
    bbPress – Subscription in Mandrill
    wpMandrill

  4. Jeremy Biron

    So I dug into this a little further. These emails are going to two different addresses.

    Email #1 via Mandrill – going to the correct email.
    Email #2 with no template/format – looks like it is going to noreply@www.blogdomain.com but is being received at the correct email address of the subscriber.

    • Sudar

      Is the second email going through Mandrill as well or is it going through WordPress? I mean if you check your Mandrill logs is the second email appearing there?

    • Sudar

      Also does the domain of your blog includes www or not?

      • Doug Smith

        I can confirm the behavior with receiving multiple e-mails. It only started after I installed your plugin.

        The first e-mail is going through WordPress and my server. It has none of the Mandrill headers in it and does not show up in my Mandrill logs.

        The second e-mail goes through Mandrill. It has the Mandrill headers and the formatting and newlines are stripped out of it.

        My domain does not include a www. I had the problem with the letter “s” being stripped out so I just hardcoded the noreply@domain.com address in bbPress’ includes/common/functions.php in lines 1065 and 1205.

      • Jeremy Biron

        I am still stuck on this. It is killing me.

        The first email goes through Mandrill and the second does not. The second sends through WP and has a “noreply” email address. We use www with the domain/installation.

  5. Jeremy Biron

    I think I have found the issue. The system is trying to send to the noreply address that is used for outgoing emails. See the below API response. It sent to 3 addresses. 2 subscribers and the reply address.

    I think the failed noreply address is causing the system to throw and exception and use wp_mail after it has already sent via Mandrill.

    How do I stop it from sending to noreply@www.my-domain.com?

    [
    {
    “email”: “”,
    “status”: “invalid”,
    “_id”: “e3d513c3dfe142bb892a5392ef563d7a”,
    “reject_reason”: null
    },
    {
    “email”: “subscriber-email-2@gmail.com”,
    “status”: “sent”,
    “_id”: “243535bbf22f4fcf9d2ea0a90e30f7a5”,
    “reject_reason”: null
    },
    {
    “email”: “subscriber-email@gmail.com”,
    “status”: “sent”,
    “_id”: “f7ae7f5eb3fb43c9adfb2208a61a1d89”,
    “reject_reason”: null
    }
    ]

    • Doug Smith

      I can confirm this on my server as well. The log is showing every message being sent to the noreply address.

  6. Chris

    The multiple email issue happens for me too.
    I have been able to get around it by using another plugin – that stops all wp_mail() – but still allows Mandrill emails to get through.
    Plugin: “Stop Emails” (https://wordpress.org/plugins/stop-emails/)
    With that plugin activated, the bbPress notification emails still go out through Mandrill

  7. st

    Is the plug-in still needed after I have updated to bbPress 2.5.6?

    • Sudar

      No. It is not.

      Let me know if you still face any issues with it.

      • Chris

        Interesting, however, it seems that we DO still need the plugin – even after updating to 2.5.7
        Maybe we’re “doing something wrong” — but we’re very thankful to have your plugin!
        We’re still using it – and it is working.

  8. Vanessa

    Hey Sudar,

    Thanks for all your work on this.
    I’ve been unable to figure this out – on bbPress 2.5.9, WordPress 4.5.2
    I tried using your plugin, but that doesn’t seem to route mails through Mandrill, it still seems to send them from WordPress. I don’t get duplicate emails, just the one.

    Buddypress emails have no problem, just bbPress.

    I’d love any ideas you had to fix this – been driving me a little nuts 🙂

Leave a Reply

Your email address will not be published.

*

css.php