Sending email from local host with Nodemailer

前端 未结 5 1237
小蘑菇
小蘑菇 2020-12-20 13:38

I\'d like to send mails on my local server but it seems not working with Nodemailer and NodeJS.

Is there any solutions to send mails from local?

             


        
相关标签:
5条回答
  • 2020-12-20 14:20
      const transporter = nodemailer.createTransport({
        port: 25,
        host: 'localhost',
        tls: {
          rejectUnauthorized: false
        },
      });
    
      var message = {
        from: 'noreply@domain.com',
        to: 'whatever@otherdomain.com',
        subject: 'Confirm Email',
        text: 'Please confirm your email',
        html: '<p>Please confirm your email</p>'
      };
    
      transporter.sendMail(message, (error, info) => {
        if (error) {
            return console.log(error);
        }
        console.log('Message sent: %s', info.messageId);
      });
    

    I'm current triggering this through an restful api on express.js

    this should work, i used this to set up my postfix on digitalocean: https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-postfix-as-a-send-only-smtp-server-on-ubuntu-16-04

    0 讨论(0)
  • 2020-12-20 14:29
    var nodemailer = require('nodemailer');
    
    // Create a SMTP transport object
    var transport = nodemailer.createTransport("SMTP", {
        service: 'Hotmail',
        auth: {
            user: "username",
            pass: "password"
        }
    });
    
    // Message object
    var message = {
    
    // sender info
    from: 'name@hotmail.com',
    
    // Comma separated list of recipients
    to: req.query.to,
    
    // Subject of the message
    subject:req.query.subject, //'Nodemailer is unicode friendly ✔', 
    
    // plaintext body
    text: req.query.text //'Hello to myself!',
    
     // HTML body
      /*  html:'<p><b>Hello</b> to myself <img src="cid:note@node"/></p>'+
         '<p>Here\'s a nyan cat for you as an embedded attachment:<br/></p>'*/
    };
    
    console.log('Sending Mail');
    transport.sendMail(message, function(error){
    if(error){
      console.log('Error occured');
      console.log(error.message);
      return;
    }
    console.log('Message sent successfully!');
    
     // if you don't want to use this transport object anymore, uncomment    
     //transport.close(); // close the connection pool
    });
    

    You must specify the transport protocol like that of SMTP or create youy own transport.You have not specified this in you code.  

    0 讨论(0)
  • 2020-12-20 14:30

    In the localhost its not working because a secure and safetly connection is required for sending an email but using gmail[smtp(simple mail transfer protocol)] we can achieve it functionality.

    Don't forget to first do the setting - Allow less secure apps to access account.

    its given permission for access gmail account.

    by default this settings is off and you simply turn it on. Now move on coding part.

    ****************************--------------------------------------------------------------------*********************

    const nodemailer = require('nodemailer');
    
    let transporter = nodemailer.createTransport({
    host: 'smtp.gmail.com',
    port: 587,
    secure: false,
    requireTLS: true,
    auth: {
        user: 'your.gmail.account@gmail.com', // like : abc@gmail.com
        pass: 'your.gmailpassword'           // like : pass@123
    }
    });
    
    let mailOptions = {
     from: 'your.gmail.account@gmail.com',
     to: 'receivers.email@domain.com',
     subject: 'Check Mail',
     text: 'Its working node mailer'
    };
    
    transporter.sendMail(mailOptions, (error, info) => {
      if (error) {
         return console.log(error.message);
      }
    console.log('success');
    }); 
    
    0 讨论(0)
  • 2020-12-20 14:30

    Shouldn't be an issue with running from a local server.

    It looks like you are using direct transport, which is described in the docs as:

    ...not reliable as outgoing port 25 used is often blocked by default. Additionally mail sent from dynamic addresses is often flagged as spam. You should really consider using a SMTP provider.

    One solution is to define a SMTP transport:

    var transporter = nodemailer.createTransport({
        service: 'gmail',
        auth: {
            user: 'sender@gmail.com',
            pass: 'password'
        }
    });
    

    There are other solutions here, such as using a Transport Plugin.

    Regardless, it is tough to diagnose your issue when you are not adding callbacks to your sendMail function. When you send mail, do it like this, and then you can check the console to see what the error may be:

    transporter.sendMail({
      from: contact.email,
      to: to,
      subject: contact.subject,
      text: contact.message
    }, function(error, info){
        if(error){
            console.log(error);
        }else{
            console.log('Message sent: ' + info.response);
        }
    });
    
    0 讨论(0)
  • 2020-12-20 14:39

    Additional to what @cheniel said: Make sure the "from" email is the same user email as the email in the transporter object. And the "to" must be a valid email and exist (check if the value is correctly set (not undefined and not null)).

    0 讨论(0)
提交回复
热议问题