Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodemailer - dynamic transporter

I'm creating a nodemailer on my app, and I want to send email dynamically from input field that user will fill up. E.g. If the user writes on form input email value: '[email protected]' I want to send this email from '[email protected]' so later I can answer to this person and have all history of emails.

Edit

Step 1:

Fill up form on website with your personal data. E.g. I want to send email to company (name: josh, surname: murp, email: [email protected])

Step 2:

Send this json object to '/contactus/' with fetch and log this in console with req.body I'm getting this obj: { name: 'josh', surname: 'murp', email: '[email protected]' }

Step 3:

I want to setup Nodemailer in that way:

let mailOptions = {
        from: `${req.body.firstName} ${req.body.surname} <${req.body.email}>`, // sender address
        to: "[email protected]", // list of receivers
        subject: "hi", // Subject line
        text: "Hello world", // plain text body
        html: "<b>NodeJS Email Tutorial</b>" // html body
      };

That means. I want to get email FROM req.body.email, in real life I want to get EMAIL from [email protected] but Nodemailer use TRANSPORTER to send email. It means I'm getting email from email configured in transporter but I want to get EMAIL from JOSH

This result brings me an issue.

When I got email from my account (transporter) I'm not able to answer this mail I need to find who is writing to me and in CONVERSATION HISTORY I don't have FIRST email of this conversation what was send via my website.

One big question here:

HOW TO SET DYNAMIC TRANSPORTER? or how to implement this in Node/express

let transporter = nodemailer.createTransport({
    host: "smtp.gmail.com",
    port: 465,
    secure: true, // true for 465, false for other ports
    auth: {
      user: keys.username, // generated ethereal user
      pass: keys.password // generated ethereal password
    }
  });

I'm looking all day to find this answer in web, but no result.

like image 465
Freestyle09 Avatar asked Mar 06 '23 04:03

Freestyle09


2 Answers

What I have understood by reading your question and all responses is, you want to send an email to your account from a users's email address by asking the user, his username only and you don't want to ask for his/her password, using nodemailer and the purpose for sending email from users account is, you want to have FIRST email of the conversation what was send via your website.

So, @Freestyle09, you cannot send email from any email account using nodemailer unless you have provided username and password of that email account and you will not be having user's password as you said you don't want to ask for user's password.

So, alternate solution I will recommend you is to solve your problem in this way

var mailData = {
    from: '[email protected]',
    to: '[email protected]',
    cc:`${req.body.firstName} ${req.body.surname} <${req.body.email}>`//[email protected]
    subject: 'Message title',
    text: 'This request has been generated on behalf of josh ...... all other 
    text here',
    html: 'HTML version of the message'
};

Once you have added user name in cc list you will have that user included in conversation otherwise its not possible to send email from an email address with having the password using nodemailer. you need to look for some alternative solutions

TIP: (by author of question)

One tip, I have tested this with gmail provider but when I have two different providers (from: my-email, cc: other-email) reply message goes to my-email, but when I have from and cc exacly the same provider It works! I'm getting message, client too and I can reply to the client and have all conversation history.

like image 159
Al Fahad Avatar answered Mar 15 '23 04:03

Al Fahad


Are you wondering how to send the email?

You won't be able to create a transporter with their email address since you won't be able to log in as them.

let transporter = nodeMailer.createTransport({
    host: "smtp.gmail.com",
    port: 465,
    secure: true,
    auth: {
        user: "[email protected]",
        pass: "mypassword"
    }
});
let mailOptions = {
    from: `${req.body.firstName} ${req.body.surname} <${req.body.email}>`, // sender address
    to: "[email protected]", // list of receivers
    subject: "hi", // Subject line
    text: "Hello world", // plain text body
    html: "<b>NodeJS Email Tutorial</b>" // html body
};

transporter.sendMail(mailOptions, function (err, info) {
    if (err) {
        throw err
    }
    console.log(info)
})
like image 35
Joseph Dykstra Avatar answered Mar 15 '23 04:03

Joseph Dykstra