Parse-server: Mailgun send multiple emails instead of 1

Created on 19 Mar 2017  Â·  4Comments  Â·  Source: parse-community/parse-server

Hello!
I have a problem I've asked in Stackoverflow and ServerFault several weeks ago with no answer, so this is my last chance to find the solution. I'll try to follow the steps to describe this "bug".

Issue Description

I migrated from Parse to Parse Server hosted in Digital Ocean (details below) and everything seemed to be working ok until I realized there was a problem sending emails (from the app through Cloud code and from Parse Server for email verification after the registration): the emails are sent multiple times, usually twice but sometimes 3, 4 even 5 or 6 times.

Steps to reproduce

I'm not sure what are the steps, but this is my code and configuration:

Parse Server configuration > myparseuser/cloud/main.js

var api_key = 'key-myMailgunkey';
var domain = 'mg.mydomain.com';
var mailgun = require('mailgun-js')({apiKey: api_key, domain: domain});
 Parse.Cloud.define("functiontosendemail", function(request, response) {
                   var name = request.params.username;
                   var email = request.params.email;
                   var body = request.params.body;
            var data = {
              from: email,
              to: '[email protected]',
              subject: "Contact Form",
              text: body
               };
        mailgun.messages().send(data, function (error, body) {
              console.log(body);
        });
});

Parse Server configuration > myparseuser/index.js

...
var api = new ParseServer({  
  ...,
  emailAdapter: {
    module: 'parse-server-simple-mailgun-adapter',
    options: {
      fromAddress: '[email protected]',
      domain: 'mg.mydomain.com',
      apiKey: 'key-myMailgunkey'
    }
  }
});
...

Mailgun configuration:

Domain: mg.mydomain.com
State: Active
IPAddress: someipaddress
SMTP Hostname: smtp.mailgun.org
Default SMTP Login: [email protected]
Default password: defaultpassword #Note: I don't remember to use this password anywhere.
API Base URL: https://api.mailgun.net/v3/mg.mydomain.com
API Key: key-myMailgunkey
Inbound smap filter: Disabled
Wildcard domain: Off
DKIM selector: pic
Tracking hostname: email
Click tracking: Off
Open tracking: Off
Unsubscribes: Off
TLS Connection: Opportunistic
Certificate verification: Required
TXT record mg.mydomain.com (ok)
TXT record pic._domainkey.mg.mydomain.com (ok)
CNAME record email.mg.mydomain.com (checked)
MX records mxa.mailgun.org (checked)
MX records mxb.mailgun.org (checked)

Expected Results

I expected that each verification email was sent only once, and each contact email (via CloudCode) was sent only once.

Actual Outcome

Each email is sent usually twice but sometimes 3, 4, 5 or 6 times.

Environment Setup

  • Server

    • parse-server version 2.3.1
    • Operating System: Ubuntu 16.041
    • Hardware: 512MB RAM, 2GB Disk
    • Localhost or remote server? Digital Ocean
  • Database

    • MongoDB version: 3.0.14
    • Storage engine: Linux
    • Hardware: 512MB RAM, 2GB Disk
    • Localhost or remote server? Digital Ocean

Logs/Trace

If I send and email from the app calling "functiontosendemail" in rare occasions the email is sent and received only once. Usually the email is sent and received between 3 and 8 times In these cases, after more or less 1 minute my app receives from Parse Server this error:

com.parse.ParseRequest$ParseRequestException: i/o failure

Mailgun Log shows this two actions between 3 and 8 times:

Accepted: [email protected] → [email protected] 'Contact Form'
Delivered: [email protected] → [email protected] 'Contact Form'

Most helpful comment

I'm not sure if it's related to the cause of the issue or not, but there's an issue in your Cloud Code function where you do not return a success or error, which means the client doesn't get any response to the web request. In that case, some clients may retry until it timeouts, which could result in multiple emails sent.

You should always return a response.success() if sending the email succeeds or response.error('error message here') if an error occurs.

All 4 comments

I'm not sure if it's related to the cause of the issue or not, but there's an issue in your Cloud Code function where you do not return a success or error, which means the client doesn't get any response to the web request. In that case, some clients may retry until it timeouts, which could result in multiple emails sent.

You should always return a response.success() if sending the email succeeds or response.error('error message here') if an error occurs.

Hello wabirached!

Thank you very much for your suggestion but I'm not sure if this is the cause of my problem because 2 reasons:

  • I have access to the client code and it doesn't resend the email even if there was an error.
  • I have the same problem sending verification emails, and verification emails don't use my Cloud code.
    Kind regards!

Hello wabirached!

After a long time without being able to work on this I was finally able to check that you were completely right! So Thank you very much!!
Despite I had access to the client code it seemed that Parse library was resending the emails because it didn't receive the response.
Once I've added response.success() and response.error('error message here') this is working well.

Anyway I'm having now a similar problem sending push notifications from CloudCode because some times they are resent. This time I've added response.success and response.error so I can't imagine the cause.
I've seen in the Parse logs that when sending push notifications from cloud code, response is "undefined", could it be possible that with this response, success() and error() functions are never called?

Probably, because the client never received the response of functiontosendemail, it got a timeout, and it did a retry automatically.

Was this page helpful?
0 / 5 - 0 ratings