From 7d943c1b9120b9ab47d9816f55675c93e4a571f6 Mon Sep 17 00:00:00 2001 From: Fabrice Daugan Date: Thu, 16 Jan 2020 11:46:39 +0100 Subject: [PATCH 1/5] Fix #114 Sender email wihtout name --- index.js | 20 ++++++-- test/assets/message.simplefrom.processed.txt | 24 ++++++++++ test/assets/message.simplefrom.txt | 35 ++++++++++++++ .../message.simplefrom_custom.processed.txt | 24 ++++++++++ test/processMessage.js | 46 +++++++++++++++++++ 5 files changed, 145 insertions(+), 4 deletions(-) create mode 100644 test/assets/message.simplefrom.processed.txt create mode 100644 test/assets/message.simplefrom.txt create mode 100644 test/assets/message.simplefrom_custom.processed.txt diff --git a/index.js b/index.js index ead0495..b17df87 100644 --- a/index.js +++ b/index.js @@ -206,11 +206,23 @@ exports.processMessage = function(data) { function(match, from) { var fromText; if (data.config.fromEmail) { - fromText = 'From: ' + from.replace(/<(.*)>/, '').trim() + - ' <' + data.config.fromEmail + '>'; + if (from.indexOf('<') >= 0 && from.indexOf('>') >= 0) { + fromText = 'From: ' + from.replace(/<(.*)>/, '').trim() + + ' <' + data.config.fromEmail + '>'; + } else { + // No name format + fromText = 'From: ' + from.replace('@', ' at ') + + ' <' + data.config.fromEmail + '>'; + } } else { - fromText = 'From: ' + from.replace('<', 'at ').replace('>', '') + - ' <' + data.originalRecipient + '>'; + if (from.indexOf('<') >= 0 && from.indexOf('>') >= 0) { + fromText = 'From: ' + from.replace('<', 'at ').replace('>', '') + + ' <' + data.originalRecipient + '>'; + } else { + // No name format + fromText = 'From: ' + from.replace('@', ' at ') + + ' <' + data.originalRecipient + '>'; + } } return fromText; }); diff --git a/test/assets/message.simplefrom.processed.txt b/test/assets/message.simplefrom.processed.txt new file mode 100644 index 0000000..aeedc86 --- /dev/null +++ b/test/assets/message.simplefrom.processed.txt @@ -0,0 +1,24 @@ +Received: from example.com (example.com [127.0.0.1]) + by inbound-smtp.us-west-2.amazonaws.com with SMTP id 81fu1unjk93bm5cb0jlk23fll33spcvf3633l8qg1 + for info@example.com; + Fri, 11 Mar 2016 06:20:55 +0000 (UTC) +X-SES-Spam-Verdict: PASS +X-SES-Virus-Verdict: PASS +Received-SPF: none (spfCheck: 127.0.0.1 is neither permitted nor denied by domain of example.com) client-ip=10.0.0.1; envelope-from=postmaster@example.com; helo=example.com; +From: betsy at example.com +To: info@example.com +Subject: Test message from Betsy +Date: Fri, 11 Mar 2016 01:20:54 -0500 +Reply-To: Betsy + +This is a test message to info@example.com. + +It was sent from betsy@example.com. + +These lines should not be affected: +From: test@example.com +Reply-To: test@example.com +Return-Path: test@example.com +DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/simple; + s=gdwg2y3kokkkomn55z2ilkup5wp5hhxx; d=amazonses.com; t=1457977483; + h=Date:From:Reply-To:To:Message-ID:Subject:MIME-Version; diff --git a/test/assets/message.simplefrom.txt b/test/assets/message.simplefrom.txt new file mode 100644 index 0000000..c4c182a --- /dev/null +++ b/test/assets/message.simplefrom.txt @@ -0,0 +1,35 @@ +Return-Path: +Received: from example.com (example.com [127.0.0.1]) + by inbound-smtp.us-west-2.amazonaws.com with SMTP id 81fu1unjk93bm5cb0jlk23fll33spcvf3633l8qg1 + for info@example.com; + Fri, 11 Mar 2016 06:20:55 +0000 (UTC) +DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/simple; s=gdwg2y3kokomn5a55z2ilkup5wp5hhxx; d=amazonses.com; t=1457977483; h=Date:From:Reply-To:To:Message-ID:Subject:MIME-Version:Content-Type; +DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/simple; + s=gdwg2y3kokkkomn55z2ilkup5wp5hhxx; d=amazonses.com; t=1457977483; + h=Date:From:Reply-To:To:Message-ID:Subject:MIME-Version:Content-Type:References:Feedback-ID; + bh=yu5f22IGBuY/QbF1MYj9KjHbPKMQlS13FY53b5YLEb8=; + b=EX4NWAbgp5EuCQ6WzaNg74GiPRxNV/oPWHbd/QAyZmfutsD2Dzr6HYfA2XJ0aEui + /RRmYfnjvB8lk3MJRhL/TMHDO/LCWLnXV4OLgkZH6IuVFVZnI2p9pAG11E1AWD9l8AW + NTuhRooMHNWMDJgEdo84jnDXIqKSvPR8o0y45M7I= +X-SES-Spam-Verdict: PASS +X-SES-Virus-Verdict: PASS +Message-ID: +Received-SPF: none (spfCheck: 127.0.0.1 is neither permitted nor denied by domain of example.com) client-ip=10.0.0.1; envelope-from=postmaster@example.com; helo=example.com; +DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1457977479; bh=yu5f99IGBuY/QbF1MYj9KjHmHUGQlS13FY53b5YLEb8=; h=Date:From:Reply-To:To:Subject:References:From:Subject; b=Osl8Z/p7lL3v/D60aBh3AJ5coNE6AORIwAEOa66ogh8UI1GLbTo0JgRwN0amg4n8lOU2RJyyNR10+rfx1ciwiP8ypfs0GjllxhgoeXtxCqtsdil5ILvkrxVloOH84tkKDVrvWv0xtZ4S1kOUDVY0EoBnC9xx7dU+WkNA2YmQSQgEji0jb8OeWowvOFxUsIwURewzONCMLm6+ZJqAVVediv6td3U3NRlN3Nfm7IHO8uxvQdDLTbJhqmIx3Ld5x///G9DOkclE+2pHgX0xZwOsbkPsfRRyeDWlrjPWwU2Wm8E481U0CsjmaEbSwk4lkEoFKQH7WfvmULFXftK0YZZMjA== +From: betsy@example.com +To: info@example.com +Subject: Test message from Betsy +Message-Id: +Date: Fri, 11 Mar 2016 01:20:54 -0500 + +This is a test message to info@example.com. + +It was sent from betsy@example.com. + +These lines should not be affected: +From: test@example.com +Reply-To: test@example.com +Return-Path: test@example.com +DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/simple; + s=gdwg2y3kokkkomn55z2ilkup5wp5hhxx; d=amazonses.com; t=1457977483; + h=Date:From:Reply-To:To:Message-ID:Subject:MIME-Version; diff --git a/test/assets/message.simplefrom_custom.processed.txt b/test/assets/message.simplefrom_custom.processed.txt new file mode 100644 index 0000000..976f28f --- /dev/null +++ b/test/assets/message.simplefrom_custom.processed.txt @@ -0,0 +1,24 @@ +Received: from example.com (example.com [127.0.0.1]) + by inbound-smtp.us-west-2.amazonaws.com with SMTP id 81fu1unjk93bm5cb0jlk23fll33spcvf3633l8qg1 + for info@example.com; + Fri, 11 Mar 2016 06:20:55 +0000 (UTC) +X-SES-Spam-Verdict: PASS +X-SES-Virus-Verdict: PASS +Received-SPF: none (spfCheck: 127.0.0.1 is neither permitted nor denied by domain of example.com) client-ip=10.0.0.1; envelope-from=postmaster@example.com; helo=example.com; +From: betsy at example.com +To: info@example.com +Subject: Test message from Betsy +Date: Fri, 11 Mar 2016 01:20:54 -0500 +Reply-To: Betsy + +This is a test message to info@example.com. + +It was sent from betsy@example.com. + +These lines should not be affected: +From: test@example.com +Reply-To: test@example.com +Return-Path: test@example.com +DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/simple; + s=gdwg2y3kokkkomn55z2ilkup5wp5hhxx; d=amazonses.com; t=1457977483; + h=Date:From:Reply-To:To:Message-ID:Subject:MIME-Version; diff --git a/test/processMessage.js b/test/processMessage.js index a6045d3..37f5fa1 100644 --- a/test/processMessage.js +++ b/test/processMessage.js @@ -30,6 +30,52 @@ describe('index.js', function() { }); }); + it('should process email data and handle the simple from format', function(done) { + var data = { + config: {}, + email: { + source: "betsy@example.com" + }, + emailData: fs.readFileSync("test/assets/message.simplefrom.txt").toString(), + log: console.log, + recipients: ["jim@example.com"], + originalRecipient: "info@example.com" + }; + var emailDataProcessed = fs.readFileSync( + "test/assets/message.simplefrom.processed.txt").toString(); + index.processMessage(data) + .then(function(data) { + assert.equal(data.emailData, + emailDataProcessed, + "processEmail updated email data"); + done(); + }); + }); + + it('should process email data and handle the simple from format having a custom fromMail', function(done) { + var data = { + config: { + fromEmail: "noreply@example.com" + }, + email: { + source: "betsy@example.com" + }, + emailData: fs.readFileSync("test/assets/message.simplefrom.txt").toString(), + log: console.log, + recipients: ["jim@example.com"], + originalRecipient: "info@example.com" + }; + var emailDataProcessed = fs.readFileSync( + "test/assets/message.simplefrom_custom.processed.txt").toString(); + index.processMessage(data) + .then(function(data) { + assert.equal(data.emailData, + emailDataProcessed, + "processEmail updated email data"); + done(); + }); + }); + it('should preserve an existing Reply-To header in emails', function(done) { var data = { config: {}, From 03f80c7cd356d22eed4e3328334cc8da30e87cac Mon Sep 17 00:00:00 2001 From: Fabrice Daugan Date: Thu, 16 Jan 2020 12:03:28 +0100 Subject: [PATCH 2/5] Fix #144 Sender email without name --- index.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index b17df87..6c60f7a 100644 --- a/index.js +++ b/index.js @@ -214,15 +214,13 @@ exports.processMessage = function(data) { fromText = 'From: ' + from.replace('@', ' at ') + ' <' + data.config.fromEmail + '>'; } - } else { - if (from.indexOf('<') >= 0 && from.indexOf('>') >= 0) { + } else if (from.indexOf('<') >= 0 && from.indexOf('>') >= 0) { fromText = 'From: ' + from.replace('<', 'at ').replace('>', '') + ' <' + data.originalRecipient + '>'; - } else { - // No name format - fromText = 'From: ' + from.replace('@', ' at ') + - ' <' + data.originalRecipient + '>'; - } + } else { + // No name format + fromText = 'From: ' + from.replace('@', ' at ') + + ' <' + data.originalRecipient + '>'; } return fromText; }); From d5cb05098a79a3ef2877389c4be0b739eb0b33c5 Mon Sep 17 00:00:00 2001 From: Fabrice Daugan Date: Thu, 16 Jan 2020 12:26:25 +0100 Subject: [PATCH 3/5] Fix #114 Sender email without name --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 6c60f7a..5897343 100644 --- a/index.js +++ b/index.js @@ -215,8 +215,8 @@ exports.processMessage = function(data) { ' <' + data.config.fromEmail + '>'; } } else if (from.indexOf('<') >= 0 && from.indexOf('>') >= 0) { - fromText = 'From: ' + from.replace('<', 'at ').replace('>', '') + - ' <' + data.originalRecipient + '>'; + fromText = 'From: ' + from.replace('<', 'at ').replace('>', '') + + ' <' + data.originalRecipient + '>'; } else { // No name format fromText = 'From: ' + from.replace('@', ' at ') + From 33f461ef82349f6fe7a7d8ed0449de4405a189e4 Mon Sep 17 00:00:00 2001 From: Fabrice Daugan Date: Thu, 16 Jan 2020 13:01:36 +0100 Subject: [PATCH 4/5] Fix #114 Sender email without name --- index.js | 8 ++++---- test/processMessage.js | 8 +++++--- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 5897343..eb19764 100644 --- a/index.js +++ b/index.js @@ -210,16 +210,16 @@ exports.processMessage = function(data) { fromText = 'From: ' + from.replace(/<(.*)>/, '').trim() + ' <' + data.config.fromEmail + '>'; } else { - // No name format - fromText = 'From: ' + from.replace('@', ' at ') + + // No display name format + fromText = 'From: ' + from.replace('@', ' at ').trim() + ' <' + data.config.fromEmail + '>'; } } else if (from.indexOf('<') >= 0 && from.indexOf('>') >= 0) { fromText = 'From: ' + from.replace('<', 'at ').replace('>', '') + ' <' + data.originalRecipient + '>'; } else { - // No name format - fromText = 'From: ' + from.replace('@', ' at ') + + // No display name format + fromText = 'From: ' + from.replace('@', ' at ').trim() + ' <' + data.originalRecipient + '>'; } return fromText; diff --git a/test/processMessage.js b/test/processMessage.js index 37f5fa1..15c25e0 100644 --- a/test/processMessage.js +++ b/test/processMessage.js @@ -36,7 +36,8 @@ describe('index.js', function() { email: { source: "betsy@example.com" }, - emailData: fs.readFileSync("test/assets/message.simplefrom.txt").toString(), + emailData: + fs.readFileSync("test/assets/message.simplefrom.txt").toString(), log: console.log, recipients: ["jim@example.com"], originalRecipient: "info@example.com" @@ -52,7 +53,7 @@ describe('index.js', function() { }); }); - it('should process email data and handle the simple from format having a custom fromMail', function(done) { + it('should allow overriding the simple From header in emails', function(done) { var data = { config: { fromEmail: "noreply@example.com" @@ -60,7 +61,8 @@ describe('index.js', function() { email: { source: "betsy@example.com" }, - emailData: fs.readFileSync("test/assets/message.simplefrom.txt").toString(), + emailData: + fs.readFileSync("test/assets/message.simplefrom.txt").toString(), log: console.log, recipients: ["jim@example.com"], originalRecipient: "info@example.com" From 13548a25202ac96deaf8d09856d64dd597d22ecb Mon Sep 17 00:00:00 2001 From: Fabrice Daugan Date: Thu, 16 Jan 2020 15:14:02 +0100 Subject: [PATCH 5/5] Fix #114 Sender email without name --- test/assets/message.simplefrom.processed.txt | 2 +- test/assets/message.simplefrom_custom.processed.txt | 2 +- test/processMessage.js | 11 ++++++----- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/test/assets/message.simplefrom.processed.txt b/test/assets/message.simplefrom.processed.txt index aeedc86..2b78be4 100644 --- a/test/assets/message.simplefrom.processed.txt +++ b/test/assets/message.simplefrom.processed.txt @@ -9,7 +9,7 @@ From: betsy at example.com To: info@example.com Subject: Test message from Betsy Date: Fri, 11 Mar 2016 01:20:54 -0500 -Reply-To: Betsy +Reply-To: betsy@example.com This is a test message to info@example.com. diff --git a/test/assets/message.simplefrom_custom.processed.txt b/test/assets/message.simplefrom_custom.processed.txt index 976f28f..f2d9ffb 100644 --- a/test/assets/message.simplefrom_custom.processed.txt +++ b/test/assets/message.simplefrom_custom.processed.txt @@ -9,7 +9,7 @@ From: betsy at example.com To: info@example.com Subject: Test message from Betsy Date: Fri, 11 Mar 2016 01:20:54 -0500 -Reply-To: Betsy +Reply-To: betsy@example.com This is a test message to info@example.com. diff --git a/test/processMessage.js b/test/processMessage.js index 15c25e0..a0a791b 100644 --- a/test/processMessage.js +++ b/test/processMessage.js @@ -30,13 +30,13 @@ describe('index.js', function() { }); }); - it('should process email data and handle the simple from format', function(done) { + it('should process email data without display name', function(done) { var data = { config: {}, email: { source: "betsy@example.com" }, - emailData: + emailData: fs.readFileSync("test/assets/message.simplefrom.txt").toString(), log: console.log, recipients: ["jim@example.com"], @@ -53,7 +53,7 @@ describe('index.js', function() { }); }); - it('should allow overriding the simple From header in emails', function(done) { + it('should process overridden email without display name', function(done) { var data = { config: { fromEmail: "noreply@example.com" @@ -61,7 +61,7 @@ describe('index.js', function() { email: { source: "betsy@example.com" }, - emailData: + emailData: fs.readFileSync("test/assets/message.simplefrom.txt").toString(), log: console.log, recipients: ["jim@example.com"], @@ -158,7 +158,8 @@ describe('index.js', function() { source: "betsy@example.com" }, emailData: - fs.readFileSync("test/assets/message.from_multiline.source.txt").toString(), + fs.readFileSync( + "test/assets/message.from_multiline.source.txt").toString(), log: console.log, recipients: ["jim@example.com"], originalRecipient: "info@example.com"