最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Nodemailer - dynamic transporter - Stack Overflow

programmeradmin1浏览0评论

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 pany (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",
    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.

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 pany (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.",
    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.

Share Improve this question edited Oct 31, 2018 at 13:01 antzshrek 9,9636 gold badges27 silver badges44 bronze badges asked Oct 26, 2018 at 14:06 Freestyle09Freestyle09 5,52810 gold badges58 silver badges92 bronze badges 3
  • So you have already extracted the mail, from input field? – Abhishek Mani Commented Oct 26, 2018 at 14:18
  • Yes i have all but don't know how to pass this req.body.email to 'from' value – Freestyle09 Commented Oct 26, 2018 at 14:18
  • So, 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. Is this what you are asking? – Al Fahad Commented Nov 4, 2018 at 14:18
Add a ment  | 

4 Answers 4

Reset to default 3 +50

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 remend 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.

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.",
    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)
})

In response to your question on

how to implement this in Node/express

It might be difficult for user who is registering on your platform to get an email from himself even if your have his password passed to req.body.password because he/she must have to turn on the Less secure app access which is for a gmail account, I don't even know how other emailing services do theirs.

But I will advice you setup an email where you want registered users to get an email from you or whoever the admin(owner of the email created), then you can setup the account and provide the login credentials (email and password) in your

auth: {
        user: '[email protected]',
        pass: 'password_you_created'
    }

after which you will have to turn on your Less secure app access, else your emails will not go through.

You can try the example below:

{
    let transporter = nodemailer.createTransport({
        service: 'gmail',
        auth: {
            user: '[email protected]',
            pass: 'password_you_created'
        }
    });

    let mailOptions = {
        from: '[email protected]',
        to: req.body.email,
        subject: "hi", // Subject line
        text: "Hello world", // plain text body

        html: "<b>NodeJS Email Tutorial</b>" // html body
    };

        transporter.sendMail(mailOptions, (error, info)=>{
            if (error) {
                console.log(error);
            } else {
                console.log('Email sent');
            }
        }); 
    res.json({success: 'whatever message you plan on writing'});
}

I hope this is good enough to help.

So you can simply assign the input field value to global object.

eg, global.email = req.body.email,

and can use in your nodemailer config.

let transporter = nodeMailer.createTransport({
 host: "smtp.gmail.",
 port: 465,
 secure: true,
 auth: {
   user: global.email || "[email protected]",
   pass: "mypassword"
 }
});
发布评论

评论列表(0)

  1. 暂无评论