Search code examples
nodemailergoogle-workspace

send a group email address with Nodemailer with G Suite OAuth2


I hope someone can help me.

I am using nodejs and Nodemailer to connect to a company g-suite account from which I want to send emails from the address info@domain.com automatically.

Following the instructions en https://medium.com/@imre_7961/nodemailer-with-g-suite-oauth2-4c86049f778a

I have the following code inside an azure function.


module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');

    const YOUR_EMAIL_ADDRESS = 'cesarcas@delfometrics.com';
    const YOUR_EMAIL_ADDRESS_AUTOMATIC_SENDER = 'info@delfometrics.com';
    // Change this to the receiver to the mail
    const SEND_TO = (req.query.toemail || (req.body && req.body.toemail));

    const transporter = nodemailer.createTransport({
            host: 'smtp.gmail.com',
            port: 465,
            secure: true,
            auth: {
            type: 'OAuth2',
            user: YOUR_EMAIL_ADDRESS,
            serviceClient: "bla bla bla private",
            privateKey: "bla blab bla private",
        },
    });
    try {
        await transporter.verify();
        await transporter.sendMail({
            from: YOUR_EMAIL_ADDRESS_AUTOMATIC_SENDER ,
            to: SEND_TO,
            subject: 'John Doe opens new farm YOU GOTTA SEE IT',
            text: 'It is beautiful 2.',
        });

        context.res = {
            // status: 200, /* Defaults to 200 */
            body: "already send email"
        };

    } catch (err) {
        context.log(err);
    }

} 

The problem I have is that:

Only user accounts are working with service accounts and not group email.

I already configured a group within google workspace following the instructions:

1- Create a group with that address e.g.: ‘info@domain.com’

2- Make sure that group has no members (if you don’t want to receive emails sent to that address)

3- Make sure the group can be used as an email address. Please google this, there is a lot of info.

4- Set ‘cesarcas@domain.com’ as the auth.user when creating the transport.

5- Now you can set the from field to ‘info@domain.com’ when you send the email. 🎉

even so, after configuring the group,

every time I run the code, the emails are sent correctly, but not from the correct address

that is, although I have the address info@domain.com indicated in YOUR_EMAIL_ADDRESS_AUTOMATIC_SENDER , it sends the emails from YOUR_EMAIL_ADDRESS cesarcas@domain.com (which is a correct user account within the organization), it seems like an error from the nodemailer library or a problem with the group configuration in google workspace.

I would greatly appreciate the help


Solution

  • It is not a nodemailer error

    I fix my problem adding a missing configuration in my gmail account YOUR_EMAIL_ADDRESS "Gmail Send Mail As"

    You could review how to add "Gmail Send Mail As" in:

    https://www.youtube.com/watch?v=8Esd85pyH8I&feature=emb_logo&ab_channel=GoldyArora

    Following the instructions in https://medium.com/@imre_7961/nodemailer-with-g-suite-oauth2-4c86049f778a

    I confirm that the writing by Bogdan Bota works and I was able to send emails from a group email in my organization info@domain.com validating from my account cesarcas@domain.com.

    Also note that you need to have access to group email, in mi case, info@domain.com to be able to validate that you can use that address to send emails.