0

I have a problem setting the author of a mail in the "from" field of the RFC2822 mail (here the detail). The problem is that the author does not appear in the received mail. The "from" field I've set looks like this:

MyName: name.surname@gmail.com

But I also tried:

MyName <name.surname@gmail.com>

None of these work, in the received mail the name is still missing looking at the original mail.

This should work because using nodemailer (with Gmail) and the same value for "from" it works. Can someone explain me what's happening? How can I solve?

EDIT: I report the code I am using as asked in one comment.

I separated the call to the API from the part that generates the mail body, so the call is:

function gmailSend(auth, mail){
    const gmail = google.gmail({version: 'v1', auth});
    const b64mex=mail.b64enc();
    return gmail.users.messages.send(
        {auth: auth,
            userId: 'me',
            resource:
                {raw: b64mex}
        }
    );

}

While the parameter "mail" is generated in this way:

function genMail(candidate, clerk_email, supervisor_email){
return new Mail({from: `MyName: name.surname@gmail.com`, to: candidate.email,
    subject: "Test Mail", bcc: supervisor_email,
    "reply-to": clerk_email}, genMessage(candidate));

}

Mail simply compose an object that has the properties given in its first parameter, while b64enc() puts all in a string respecting the RFC2822 and encodes it base64.

EDIT2: code for Mail class.

class Mail{
constructor(headers, body) {
    if(!headers.to)
        throw Error("Recipient missing");
    if(headers.subject){
        const b64subject=new Buffer(headers.subject).toString("base64")
            .replace(/\+/g, '-').
            replace(/\//g, '_');
        headers.subject='=?utf-8?B?'+b64subject+"?=";
    }
    Object.assign(this, headers);
    this.body = body;
}
b64enc(){
    const fields = ["Content-Type: text/html; charset=\"UTF-8\"\n",
        "MIME-Version: 1.0\n",
        "Content-Transfer-Encoding: 7bit\n"];
    for(const prop of Object.keys(this)){
        if(prop!=="body")
            fields.push(`${prop}: ${this[prop]}\n`);
    }
    fields.push("\n");
    fields.push(this.body);
    const str=fields.join('');
    const encodedMail = new Buffer(str).toString("base64")
        .replace(/\+/g, '-').
        replace(/\//g, '_');
    return encodedMail;
}

}

EDIT3: I add screenshots of desired and actual behaviour:

Desired behaviour, the hidden info are the sender's email and the receiver's one: desired behaviour, the hidden info are the sender's email and the receiver's one

Actual behaviour. The difference is that here there is not "MyName". enter image description here

What is shown in my email client of course is based on the content of "From:" in the plain email. In the first case it is "My name <email address>", in the second it is just "email address".

Community
  • 1
  • 1
Zack
  • 117
  • 1
  • 11
  • Can you provide the code related to the request you are making? – Iamblichus Jan 11 '21 at 09:50
  • 1
    @lamblichus I reported the code, hope it helps undestanding where is the mistake – Zack Jan 11 '21 at 13:34
  • Can you provide a reference to the documentation regarding `Mail` from `new Mail`? – Iamblichus Jan 11 '21 at 15:13
  • @labblichus Ok, I put the complete code for Mail – Zack Jan 11 '21 at 15:24
  • Thank you. Is it only the `from` field that is not being set? There are no problems with `to`, `subject`, etc.? – Iamblichus Jan 12 '21 at 10:31
  • @lamblichus Yes, no problems with all the rest, more specifically the problem is that in the "from" field it doesn't appear "MyName". Setting "from: MyName name.surname@gmail.com": using nodemailer I see in the mail: "From: MyName " using gmail API I see in the mail "From: name.surname@gmail.com" – Zack Jan 12 '21 at 10:52
  • Can you provide screenshots showing the desired and the actual behaviour? – Iamblichus Jan 12 '21 at 11:40
  • @lamblichus I added the screenshots, hope it helps – Zack Jan 12 '21 at 12:34
  • 1
    I assume that using `from: '"Name" '`, as shown in [this example](https://nodemailer.com/about/), is not working either? – Iamblichus Jan 13 '21 at 10:54
  • @lamblichus No, it doesn't work... Do you have any example of mail sending using gmail API? What I found does not use the name in the "from" field. I really don't understand why all the rest work well and this does not. – Zack Jan 13 '21 at 12:26
  • 1
    I cannot reproduce this. If I set the `from` header to `'"Name" '`, I'm getting the desired behaviour (I'm not using your code, but one based on [this answer](https://stackoverflow.com/a/34563593). Can you try the referenced code and see if it works? – Iamblichus Jan 13 '21 at 14:06
  • @lamblichus my mistake... When I tried the last solution I looked at my email client and not the plain mail, and it was hiding the "Name" because the email was self-sent (to experiment I've sent mails from my email account to myself). I confirm that the only correct way is the one you mentioned, any other doesn't work. – Zack Jan 13 '21 at 16:17
  • The only problem I am now noticing is that in my Gmail (both Android and WebApp) when I open the email there is a security warning (that was no present when I used nodemailer). This happens only if I send the mail to myself, so probably this is the reason. – Zack Jan 13 '21 at 16:32
  • Hi, I posted this solution as an answer, can be useful for others with your same problem. – Iamblichus Jan 14 '21 at 11:32

1 Answers1

1

In order to show a name, and not just the email address, you can format the from field the following way:

from: '"MyName" <name.surname@gmail.com>'
Iamblichus
  • 18,540
  • 2
  • 11
  • 27