0

I have similar problem as here: PHP mail function doesn't complete sending of e-mail But after several tries I don't think it's my solution...

Objective: Create an action which can send email. Code:

function main(params) {
const params = {
    "payload": {
       "id": "sender.address@gmail.com",
       "password": "CodeForTheSenderAccount",
       "receiver": "another.mail.address@gmail.com",
       "subject": "Test Wikit",
       "body": "<html>HELLO WORLD</html>"
    }
}
const nodemailer = require('nodemailer');
//Creation of a SMTP transporter
var transporter = nodemailer.createTransport({
    service: 'Gmail',
    auth: {
        user: params.payload.id,
        pass: params.payload.password
    }
});
//Creation of data to send
const mail = {
    from: '"Wikitest" <' + params.payload.id + '>',
    to: params.payload.receiver,
    subject: params.payload.subject,
    text: 'Hello World',
    html: params.payload.body
}
//Sending the mail
return(transporter.sendMail(mail, function (err, info) {
    if (err) {
        const ret = {status: 'OK'};
    } else {
        const ret = {status: 'KO'};
    }
    transporter.close();
    return (ret);
}));
}

This code works locally and I receive the email. But not when running the function in the IBM Cloud console.

I think it's due to SMTP servers but I'm not sure...

Some of you will see the "payload" param. It's because this action is in a sequence and the action before send the params.

  • Hi, could you give us a bit more details on the way you deploy into Cloud Foundry? Meanwhile, I strongly advise you use VCAP services to handle your credentials, right now it seems that they're accessible through the CF file system, aside being accessible directly from the code. – Amesys Oct 02 '18 at 15:32
  • When working with asynchronous JavaScript, you need to return and resolve the promise. Here is relevant documentation for your example https://github.com/apache/incubator-openwhisk/blob/master/docs/actions-node.md#creating-asynchronous-actions. – user6062970 Oct 02 '18 at 15:37
  • Hello. To do this, I log on the IBM cloud console and then I go into the space reserved for openwhisk functions (menu, functions). Then I create an action, a console opens and I write my code. I simulate a sequence by sending the parameters in input then I click on save then Invoke – Johann Monnerie Oct 02 '18 at 15:40
  • But locally i'm not in asynchrone? – Johann Monnerie Oct 02 '18 at 15:41
  • The fact that you're running your code locally or in the Cloud doesn't affect your code's behavior here. You're immediately returning an asynchronous function that hence, doesn't have enough time to call the callback function in which u're doing processing. Take a look at the answer given by @user6062970. – Amesys Oct 02 '18 at 15:48
  • Thanks everyone i'll work harder on this space notion who is asynchrone – Johann Monnerie Oct 02 '18 at 15:59

1 Answers1

2

When working with asynchronous JavaScript in serverless functions, you need to return and resolve a Promise. Here is relevant documentation for your example https://github.com/apache/incubator-openwhisk/blob/master/docs/actions-node.md#creating-asynchronous-actions.

return(new Promise(function(resolve, reject) {
  transporter.sendMail(mail, function (err, info) {
    if (err) {
        const ret = {status: 'OK'};
    } else {
        const ret = {status: 'KO'};
    }
  transporter.close();
  resolve(ret);
}}));
user6062970
  • 831
  • 4
  • 5