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".
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.
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)
I expected that each verification email was sent only once, and each contact email (via CloudCode) was sent only once.
Each email is sent usually twice but sometimes 3, 4, 5 or 6 times.
Server
Database
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'
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:
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.
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 orresponse.error('error message here')if an error occurs.