4

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: 'test@test.com' I want to send this email from 'test@test.com' 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: josh22@domain.com)

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: 'josh22@domain.com' }

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: "mymail@gmail.com", // 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 josh22@domain.com 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.

antzshrek
  • 9,276
  • 5
  • 26
  • 43
Freestyle09
  • 4,894
  • 8
  • 52
  • 83
  • So you have already extracted the mail, from input field? – Abhishek Mani Oct 26 '18 at 14:18
  • Yes i have all but don't know how to pass this req.body.email to 'from' value – Freestyle09 Oct 26 '18 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 Nov 04 '18 at 14:18

4 Answers4

3

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: 'another.your.email@company.com',
    to: 'mymail@gmail.com',
    cc:`${req.body.firstName} ${req.body.surname} <${req.body.email}>`//josh22@domain.com
    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.

Al Fahad
  • 2,378
  • 5
  • 28
  • 37
  • Hmmmm, It's almost good, One problem, Is there any way to have something like this... Send e-mail from my-email to my-second-email and FROM my second-email when i click on REPLY I want to have req.body.email in to field in my email provider, In your solution I'm getting 2 same emails on 2 different email addresses but when i click reply, in TO field I have my e-mail but I want req.body.email there... Do you know how to set this ?? – Freestyle09 Nov 05 '18 at 15:33
  • This is great !!!! I can now reply to email that message was come from :) 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 to but I can reply to client and have all conversation history :) You can edit answer and I will accept. Many many thanks!!!! – Freestyle09 Nov 05 '18 at 16:10
  • 1
    Edited the answer with your tip, @Freestyle09, I am very much glad to know that your problem is solved :) – Al Fahad Nov 05 '18 at 16:20
  • 1
    Damn one mistake :) please change last sentence. ' I'm getting message, client too and I can reply to the client and have all conversation history ' – Freestyle09 Nov 05 '18 at 16:30
  • 1
    Rectified your tip. – Al Fahad Nov 05 '18 at 16:41
2

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: "myadress@gmail.com",
        pass: "mypassword"
    }
});
let mailOptions = {
    from: `${req.body.firstName} ${req.body.surname} <${req.body.email}>`, // sender address
    to: "mymail@gmail.com", // 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)
})
Joseph Dykstra
  • 1,416
  • 1
  • 15
  • 17
  • I want to get email from req.body.email but I'm getting email from myadress@gmail.com – Freestyle09 Oct 26 '18 at 14:29
  • And you're sure that req.body.email is the value you expect? – Joseph Dykstra Oct 26 '18 at 14:31
  • Yes, I'm sure... That's simply website form for contact. I pass my name, surname and e-mail. And I want to have this email from this value (req.body.email), later I can click on my gmail email answer and I can write answer to this email which it's come from, anyway I must click on this link and write other email and I don't see this first email which came from website, do you understand ? – Freestyle09 Oct 26 '18 at 14:34
2

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: 'mymail@gmail.com',
        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: 'mymail@gmail.com',
            pass: 'password_you_created'
        }
    });

    let mailOptions = {
        from: 'mymail@gmail.com',
        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.

antzshrek
  • 9,276
  • 5
  • 26
  • 43
1

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.com",
 port: 465,
 secure: true,
 auth: {
   user: global.email || "myadress@gmail.com",
   pass: "mypassword"
 }
});
antzshrek
  • 9,276
  • 5
  • 26
  • 43
Abhishek Mani
  • 502
  • 4
  • 12
  • But I need password for this email ... You think it's correct ?? – Freestyle09 Oct 26 '18 at 14:24
  • you can assign it to `password` key, `global.password = req.body.password` – Abhishek Mani Oct 26 '18 at 14:24
  • You want to send me email from 'abhishek@domain.com' and I want to get this email in my email provider and have this email from your email not from global – Freestyle09 Oct 26 '18 at 14:25
  • But when you want to pass there req.body.password user must write me his password but I don't want this :) I simply want to send email from value that user write me in form – Freestyle09 Oct 26 '18 at 14:27