What is the safest and most elegant way to send a E-Mail from javascript within a domain? We have our own mail server and I'm trying to avoid 3rd party API's as smtpjs or emailjs. Is this possible?
            Asked
            
        
        
            Active
            
        
            Viewed 667 times
        
    0
            
            
        - 
                    https://www.emailjs.com/ – BenM Jun 05 '20 at 13:57
 - 
                    @BenM Question stated: I'm trying to avoid 3rd party API's as smtpjs or emailjs. – Doruk Eren Aktaş Jun 05 '20 at 13:58
 
2 Answers
2
            
            
        You can't send email via JavaScript alone. You can either open the mail client on the users device via window.open('mailto:{{to_address}}'), or by calling an API that's hosted on a server (Using nodejs with mandrill would work for this). For an example on how to do that, there's a pretty exhaustive code sample here.
        Josh Ray
        
- 291
 - 2
 - 7
 
1
            
            
        In nodejs you can use nodemailer to connect your email server and send emails.
Here is a sample code to do that (from Nodemailer's Docs):
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 " <foo@example.com>', // sender address
    to: "bar@example.com, baz@example.com", // 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: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com>
  // 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);
        Tibebes. M
        
- 6,940
 - 5
 - 15
 - 36