nodemailer / smtp-server

Create custom SMTP servers on the fly
Other
846 stars 145 forks source link

Email gets sent but not received #121

Closed firstdorsal closed 3 years ago

firstdorsal commented 5 years ago
const nodemailer = require('nodemailer');

const SMTPServer = require("smtp-server").SMTPServer;

const server = new SMTPServer({
    onAuth(auth, session, callback) {
        if (auth.username !== "test" || auth.password !== "password") {
            return callback(new Error("Invalid username or password"));
        }
        console.log(mailOptions.text);
        callback(null, {
            user: "test"

        }); // where 123 is the user id or similar property
    }
});

server.on("error", err => {
    console.log("Error %s", err.message);
});

server.listen(26);

var transporter = nodemailer.createTransport({
    host: "MYDOMAINNAME/IP",
    port: 26,
    secure: false,
    auth: {
        user: "test",
        pass: "password"
    },
    tls: {
        rejectUnauthorized: false
    }
});

var mailOptions = {
    from: '"MYSITENAME"<info@MYDOMAIN.com>',
    to: 'ADRESS@TLD.com',
    subject: 'Sending Email using Node.js',
    text: 'That was easy!'
};

transporter.sendMail(mailOptions, function (error, info) {
    if (error) {
        console.log("sendmail" + error);
    } else {
        console.log('Email sent: ' + info.response);
    }
});

Output is: That was easy! Email sent: 250 OK: message queued

i just want to send a mail with an input variable as text from my domainname to an normal gmail address; the mail doesent get received by the gmail/ or any other adress; checked the spam folder;

MrXyfir commented 5 years ago

You've probably solved this by now but you're just sending the mail to your own server and then doing nothing with it.

firstdorsal commented 5 years ago

And how do I do something with it? xD

Mr. Xyfir notifications@github.com schrieb am Mi., 13. März 2019, 03:05:

You've probably solved this by now but you're just sending the mail to your own server and then doing nothing with it.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/nodemailer/smtp-server/issues/121#issuecomment-472250548, or mute the thread https://github.com/notifications/unsubscribe-auth/AKEvfLcdbp4tpvMIGKIB19mtKxTwrsXbks5vWFzQgaJpZM4bKJ3G .

MrXyfir commented 5 years ago

You want to send mail from your server (and your domain) to Gmail and/or other third-party mailboxes correct? If so, smtp-server isn't really what you want. It's only for receiving incoming mail. nodemailer itself should be enough to send mail wherever you need it.

The simplest option for you would be to use a sendmail transporter:

nodemailer.createTransport({ sendmail: true })

Note that your Linux server will need to have sendmail installed and outgoing port 25 will need to be open, which most hosts block by default.

Now this kind of mail might be seen as spam unless you setup DKIM, SPF, and DMARC for your domain. To avoid all that, don't send from your domain. Send from your Gmail and use their SMTP information for your transporter.

firstdorsal commented 5 years ago

thank you!

Am Mi., 13. März 2019 um 17:14 Uhr schrieb Mr. Xyfir < notifications@github.com>:

You want to send mail from your server (and your domain) to Gmail correct? If so, smtp-server isn't really what you want. It's only for receiving incoming mail. nodemailer itself should be enough to send mail wherever you need it.

The simplest option for you would be to use a sendmail transporter:

nodemailer.createTransport({ sendmail: true })

Now this kind of mail might be seen as spam unless you setup DKIM, SPF, and DMARC for your domain. To avoid all that, don't send from your domain. Send from your Gmail and use their SMTP information for your transporter.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/nodemailer/smtp-server/issues/121#issuecomment-472493068, or mute the thread https://github.com/notifications/unsubscribe-auth/AKEvfMN0a4HO5mq61XVi8L03kCDrcdZ9ks5vWSPTgaJpZM4bKJ3G .

sLiMFly commented 4 years ago

Hi, I'm new, and I don't understand where to get emails to gmail ... I've seen them say "nodemailer.createTransport ({sendmail: true})" but is it referred to in the smtp.js? I use the smtp.js of the example and to send the mail I use this: `var nodemailer = require('nodemailer');

console.log("Creating transport...");
var transporter = nodemailer.createTransport({
  host: '127.0.0.1',
  port: 25,
  secure: false, // true for 465, false for other ports
});

let mailOptions = {
  from: '"yeahhh" <my@mail.com>',
  to: 'xxxxx@gmail.com',
  subject: 'subject',
  text: 'Ok, text...'
};

console.log("sending email", mailOptions);

transporter.sendMail(mailOptions, function (error, info) {
  console.log("senMail returned!");
  if (error) {
    console.log("ERROR!!!!!!", error);
  } else {
    console.log('Email sent: ' + info.response);

  }
});

console.log("End of Script");`

I know that they are not secure messages, and that the email address from which I send does not put a password or anything. I mean it could be any

vinodhreddygs commented 4 years ago

smtp-server

Hi, Using the smtp-server Iam trying to receive mails. But Authentication is not happening. Please check the below code snippet that iam using. (No consoles are printing expect console.log("starts listening on 465>>>") in the below code snippet)

var SMTPServer = require('smtp-server').SMTPServer;

var server = new SMTPServer({
    tls:true,
    logger: true,
    onAuth(auth, session, callback) {
        console.log("Console in auth function>>>",auth);
        if (auth.username != config.mail_username || auth.password != config.mail_password) {
          return callback(new Error("Invalid username or password"));
        }
        console.log("Authentication successfull>>>")
        return callback();
    },
    onConnect(session, callback) {
        console.log("Console in onConnect function>>>",session);
        if (session.remoteAddress === "127.0.0.1") {
          return callback(new Error("No connections from localhost allowed"));
        }
        return callback(); // Accept the connection
    },
    onMailFrom(address, session, callback) {
        console.log("Received mail from function>>>",address);
        if (address.address !== "vinodhreddy33@gmail.com") {
          return callback(
            new Error("Only vinodhreddy33@gmail.com is allowed to send mail")
          );
        }
        return callback(); // Accept the address
    },
    onData(stream, session, callback) {
        console.log("Console at onData function>>>",onData);
        stream.pipe(process.stdout); // print message to console
        stream.on("end", callback);
      }
});
// mailListener.listen(465) // start listening
server.listen(465, function () {
console.log("starts listning on 465>>>")
})
server.on("error", err => {
    console.log("Error %s", err.message);
  });

server.on('error', err => {
    console.log("Error on server start",err.message);
  });

server.listen(465);

Thanks in advance

varaprasadh commented 4 years ago

i have tried boiler plate code that nodemailer provided and changed receiver mail to main and used default test account created by nodemailer, after running it , it shows as sent and generated a preview url but didnt get mail to my gmail account!, checked in spam too.

ujLion commented 3 years ago

In my case was the issue of name parameter not matching thus being rejected (i think) on the smtp server. So I had to specify the name parameter in the transport configuration as my domain e.g. name=example.com . Likewise, in case of the firebase mail extension: in the smtp connection uri, I appended /?name=example.com

DaggieBlanqx commented 3 years ago

In my case was the issue of name parameter not matching thus being rejected (i think) on the smtp server. So I had to specify the name parameter in the transport configuration as my domain e.g. name=example.com . Likewise, in case of the firebase mail extension: in the smtp connection uri, I appended /?name=example.com

This worked for me!

this.transporter = nodemailer.createTransport( smtps://${process.env.mailUser}:${process.env.mailPass}@${ process.env.mailHost }:465?name=example.com );

ghost commented 3 years ago

In my case, adding name to transporter object the issue is solved!

nodemailer.createTransport( { name: SMTP_HOST, // mail.example.com or smtp.mail.com host: SMTP_HOST, // mail.example.com or smtp.mail.com port: SMTP_PORT, // 465 secure: true, auth: { user: SMTP_USER, // username pass: SMTP_PASS // password }, logger: true, debug: true });

arihantdaga commented 3 years ago

In my case, adding name to transporter object the issue is solved!

Worked for me too.

AndreBLima commented 1 year ago

In my case, adding name to transporter object the issue is solved!

nodemailer.createTransport( { name: SMTP_HOST, // mail.example.com or smtp.mail.com host: SMTP_HOST, // mail.example.com or smtp.mail.com port: SMTP_PORT, // 465 secure: true, auth: { user: SMTP_USER, // username pass: SMTP_PASS // password }, logger: true, debug: true });

<3 try

dosanias commented 8 months ago

Worked for me too.

Thank you.

bored-yuns commented 3 months ago

In my case, adding name to transporter object the issue is solved!

nodemailer.createTransport( { name: SMTP_HOST, // mail.example.com or smtp.mail.com host: SMTP_HOST, // mail.example.com or smtp.mail.com port: SMTP_PORT, // 465 secure: true, auth: { user: SMTP_USER, // username pass: SMTP_PASS // password }, logger: true, debug: true });

worked for me like a charm. great success

gregg-cbs commented 2 months ago

Gmail blocks emails that have localhost urls in them. This stumped me big time!

I was generating my urls based on my environment which was localhost and in the mailer i had links saying "click here to contact us", "view the webpage here" -> these urls were pointing to "http://localhost:5173" and gmail did not like that.

After removing them or changing them to another random https:// url everything was fine.