Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending email from own server to the internets

I have set up an email server on my host. It's basically a SMTP server that listens on port 25.

const recvServer = new SMTPServer({
  requireTLS: true,
  authOptional: true,
  logger: true,      
  onConnect(session, callback) {
    return callback();
  },

  onMailFrom(address, session, callback) {
    console.log('from', address, session);
    return callback();
  },    

  onData(stream, session, callback) {
    console.log('new msg');
    let message = '';
    stream.on('data', chunk => {
      message += chunk;
    });

    stream.on('end', () => {

      callback(null, 'Message queued');
      simpleParser(message)
        .then(parsed => {
          console.log(parsed);
          // here I wish to forward the message to outside gmail addresses
        })
        .catch(err => {
          console.log(ee)
        });

    });
  }    
});

recvServer.listen(25);

recvServer.on('error', err => {
  console.log(err.message);
});

It works fine for receiving emails from outside, like gmail etc.

But I want to be able to send emails outside also, or forward emails that I receive to some gmail addresses.

I know that I can do that using Gmail SMTP servers, but then I need an gmail account and password.

I want to be able to send email with my own server, just like yahoo sends mail to gmail using their own server not gmail accounts :)

like image 771
Alex Avatar asked May 23 '19 20:05

Alex


People also ask

Can I build my own email server?

As is evident, setting up your own email server is not that difficult. In fact, it should take you less than an hour to get it up and running, if you don't run into any unexpected issues. However, in case you're looking for more advanced features, it is advisable to hire an IT professional to set it up for you.

Can a web server send email?

In order to send emails from your website, localhost has to be assigned as SMTP host name and port 25 should be used. Authentication or secure connection (SSL/TLS) is not required.

How can I send email through Internet?

To send Internet e-mail, requires an Internet connection and access to a mail server. The standard protocol used for sending Internet e-mail is called SMTP (Simple Mail Transfer Protocol). The SMTP protocol is used to both send and receive email messages over the Internet.


2 Answers

You need a MTA (Mail Transfer Agent) in order to send an email.

So the popular options is: Postfix, here a guid how to setup postfix on ubuntu: https://help.ubuntu.com/community/Postfix

Or you can spin up a docker container like: https://hub.docker.com/r/bytemark/smtp/

Then you can use nodemailer to send emails through postfix or docker instance.

And if you want there is a full stack docker image all batteries included: https://github.com/tomav/docker-mailserver

like image 178
Rami Jarrar Avatar answered Oct 03 '22 01:10

Rami Jarrar


Technically you can use NodeMailer for sending emails.

"use strict";
const nodemailer = require("nodemailer");

// async..await is not allowed in global scope, must use a wrapper
async function main(){

  // Generate test SMTP service account from ethereal.email
  // Only needed if you don't have a real mail account for testing
  let testAccount = await nodemailer.createTestAccount();

  // create reusable transporter object using the default SMTP transport
  let transporter = nodemailer.createTransport({
    host: "smtp.ethereal.email",
    port: 587,
    secure: false, // true for 465, false for other ports
    auth: {
      user: testAccount.user, // generated ethereal user
      pass: testAccount.pass // generated ethereal password
    }
  });

  // send mail with defined transport object
  let info = await transporter.sendMail({
    from: '"Fred Foo 👻" <[email protected]>', // sender address
    to: "[email protected], [email protected]", // list of receivers
    subject: "Hello ✔", // Subject line
    text: "Hello world?", // plain text body
    html: "<b>Hello world?</b>" // html body
  });

  console.log("Message sent: %s", info.messageId);
  // Message sent: <[email protected]>

  // Preview only available when sending through an Ethereal account
  console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
  // Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
}

main().catch(console.error);
like image 20
Sohail Avatar answered Oct 03 '22 00:10

Sohail