Allow Email node to send html, and add attachments.

(outbound only at present)
This commit is contained in:
dceejay 2015-02-08 11:33:04 +00:00
parent 1513dbfcdd
commit d4a27f88a1
2 changed files with 24 additions and 10 deletions

View File

@ -70,9 +70,13 @@
<script type="text/x-red" data-help-name="e-mail">
<p>Sends the <b>msg.payload</b> as an email, with a subject of <b>msg.topic</b>.</p>
<p>The default message recipient can be configured in the node, if it is left blank it should be set in an incoming message on <b>msg.to</b>.</p>
<!-- <p>It sends the message to the configured recipient <i>only</i>.</p> -->
<!-- <p><b>msg.topic</b> is used to set the subject of the email, and <b>msg.payload</b> is the body text.</p> -->
<p>The default message recipient can be configured in the node, if it is left
blank it should be set using the <b>msg.to</b> property of the incoming message.</p>
<p>The payload can be html format.</p>
<p>If the payload is a binary buffer then it will be converted to an attachment.
The filename should be set using <b>msg.filename</b>.</p>
<p>Alternatively you may provide <b>msg.attachments</b> which should contain an array of one or
more attachments in <a href="https://www.npmjs.com/package/nodemailer#attachments" target="_new">nodemailer</a> format.</p>
</script>
<script type="text/javascript">

View File

@ -71,16 +71,26 @@ module.exports = function(RED) {
this.on("input", function(msg) {
if (smtpTransport) {
node.status({fill:"blue",shape:"dot",text:"sending"});
var payload = RED.util.ensureString(msg.payload);
if (msg.to && node.name && (msg.to !== node.name)) {
node.warn("Deprecated: msg properties should not override set node properties. See bit.ly/nr-override-msg-props");
}
smtpTransport.sendMail({
from: node.userid, // sender address
to: msg.to || node.name, // comma separated list of addressees
subject: msg.topic, // subject line
text: payload // plaintext body
}, function(error, info) {
var sendopts = { from: node.userid }; // sender address
sendopts.to = msg.to || node.name; // comma separated list of addressees
sendopts.subject = msg.topic || "Message from Node-RED"; // subject line
if (Buffer.isBuffer(msg.payload)) { // if it's a buffer in the payload then auto create an attachment instead
sendopts.attachments = [ { content: msg.payload, filename:(msg.filename || "file.bin") } ];
if (msg.hasOwnProperty("headers") && msg.headers.hasOwnProperty("content-type")) {
sendopts.attachments[0].contentType = msg.headers["content-type"];
}
sendopts.text = "Your file from Node-RED is attached : "+(msg.filename || "file.bin"); // holding body
}
else {
var payload = RED.util.ensureString(msg.payload);
sendopts.text = payload; // plaintext body
if (/<[a-z][\s\S]*>/i.test(payload)) { sendopts.html = payload; } // html body
if (msg.attachments) { sendopts.attachments = msg.attachments; } // add attachments
}
smtpTransport.sendMail(sendopts, function(error, info) {
if (error) {
node.error(error);
node.status({fill:"red",shape:"ring",text:"send failed"});