mail.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. "use strict";
  2. const nodemailer = require("nodemailer");
  3. let DEFAULT_DATA = {
  4. //from: '通知<webmaster@ikamaza.com>',
  5. from: 'Monitor<pcoloring@ikamaza.com>',
  6. to: "chengen@jccy-tech.com", // list of receivers
  7. subject: '无标题',
  8. text: "无内容", // plain text body
  9. //html: "<b>Hello world?</b>", // html body
  10. }
  11. async function send(mail) {
  12. let transporter = nodemailer.createTransport({
  13. host: "mail.ikamaza.com",
  14. port: 587,
  15. secure: false, // true for 465, false for other ports
  16. auth: {
  17. user: 'mailer', // generated ethereal user
  18. pass: 'Mailer@Jccy2017.', // generated ethereal password
  19. },
  20. });
  21. let data = JSON.parse(JSON.stringify(DEFAULT_DATA));
  22. data = Object.assign(data, mail);
  23. try {
  24. let info = await transporter.sendMail(data);
  25. console.log('Mail', data);
  26. console.log("Message sent: %s", info.messageId);
  27. // Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com>
  28. // Preview only available when sending through an Ethereal account
  29. console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
  30. return info;
  31. } catch(err) {
  32. console.error(err);
  33. }
  34. }
  35. module.exports = {
  36. send,
  37. }
  38. if (require.main == module) {
  39. send({
  40. subject: 'hello:' + new Date(),
  41. text: 'hello',
  42. //attachments: [{
  43. // path: './mail.js',
  44. //}]
  45. }).then(console.log).catch(console.error);
  46. }