Last active
August 27, 2022 12:17
-
-
Save RainerRoss/3e501669aab8dec03b03d6dcef4c67aa to your computer and use it in GitHub Desktop.
Send_Mail_with_Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//------------------------------------------------------------------------------// | |
// Node.js Modules // // | |
//------------------------------------------------------------------------------// | |
const nodemailer = require('nodemailer'); | |
const http = require('http'); | |
const port = 8080; | |
//------------------------------------------------------------------------------// | |
// Antwort des HTTP-Servers // // | |
//------------------------------------------------------------------------------// | |
let response = { | |
success: true, | |
error: '' | |
}; | |
//------------------------------------------------------------------------------// | |
// HTTP-Server // // | |
//------------------------------------------------------------------------------// | |
http.createServer(function(req, res) { | |
console.log('URL: ' + req.url); | |
console.log('Method: ' + req.method); | |
console.log('Content-Type: ' + req.headers['content-type']); | |
console.log('Content-Length: ' + req.headers['content-length']); | |
if (req.method === 'POST') { | |
let body = new Array(); | |
req.on('data', function(data) { | |
body.push(data); | |
}) | |
req.on('end', function() { | |
let data = JSON.parse(Buffer.concat(body).toString()); | |
sendMail(data).catch(console.error); | |
res.writeHead(200, {'Content-Type': 'application/json'}); | |
res.end(JSON.stringify(response)); | |
}) | |
} | |
}).listen(port); | |
console.log('Server running at Port: ' + port); | |
//------------------------------------------------------------------------------// | |
// NodeMailer // // | |
//------------------------------------------------------------------------------// | |
async function sendMail(data) { | |
let transporter = nodemailer.createTransport({ | |
host: 'smtp.web.de', | |
port: 587, | |
secure: false, // true for 465, false for other ports | |
debug: false, | |
logger: false, // Debug logging | |
auth: { | |
user: 'rainer', | |
pass: 'MyPassword', | |
} | |
}); | |
let info = await transporter.sendMail({ | |
from: data.from, | |
to: data.to, | |
subject: data.subject, | |
text: data.text, | |
html: data.html | |
}); | |
console.log('Message sent: %s', info.messageId); // Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com> | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
values QSYS2.HTTP_POST( | |
'http://172.16.0.120:8080/sendmail', | |
json_object( | |
'from': 'Rainer_Ross@web.de', | |
'to': 'Rainer_Ross@web.de', | |
'subject': 'Hallo vom NodeMailer!', | |
'text': '', | |
'html': '<h2>Hallo vom NodeMailer!</h2><p>Das ist der Text der Mail</p>' | |
), | |
json_object( | |
'header': 'Content-Type,application/json' | |
) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment