Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Message text is not required if at least 1 attachment is inlined html #61

Merged
merged 1 commit into from 12 months ago

2 participants

Harry eleith
Harry

Currently it is required to specify the message text even though the message content is inlined html. This pull request eliminates this restriction by allowing message text to be undeclared if at least 1 attachment is inlined html.

eleith
Owner
eleith commented

thank you very much. and a test, perfect PR

eleith eleith merged commit 8f78dea into from
eleith eleith closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Feb 15, 2013
ruavang Email's text content is not required if email body is inlined html f3f7854
This page is out of date. Refresh to see the latest.

Showing 2 changed files with 48 additions and 10 deletions. Show diff stats Hide diff stats

  1. +19 1 smtp/client.js
  2. +29 9 test/message.js
20 smtp/client.js
@@ -75,7 +75,7 @@ Client.prototype =
75 75 if(!(msg instanceof message.Message)
76 76 && msg.from
77 77 && (msg.to || msg.cc || msg.bcc)
78   - && msg.text)
  78 + && (msg.text || this._containsInlinedHtml(msg.attachment)))
79 79 msg = message.create(msg);
80 80
81 81 if(msg instanceof message.Message)
@@ -109,6 +109,24 @@ Client.prototype =
109 109 callback(new Error("message is not a valid Message instance"), msg);
110 110 },
111 111
  112 + _containsInlinedHtml: function(attachment) {
  113 + if (Array.isArray(attachment)) {
  114 + return attachment.some((function(ctx) {
  115 + return function(att) {
  116 + return ctx._isAttachmentInlinedHtml(att);
  117 + };
  118 + })(this));
  119 + } else {
  120 + return this._isAttachmentInlinedHtml(attachment);
  121 + }
  122 + },
  123 +
  124 + _isAttachmentInlinedHtml: function(attachment) {
  125 + return attachment &&
  126 + (attachment.data || attachment.path) &&
  127 + attachment.alternative === true;
  128 + },
  129 +
112 130 _sendsmtp: function(stack, next)
113 131 {
114 132 var self = this;
38 test/message.js
@@ -22,10 +22,10 @@ describe("messages", function()
22 22 });
23 23 });
24 24
25   - server.send(message, function(err)
  25 + server.send(message, function(err, message)
26 26 {
27 27 if(err)
28   - throw err;
  28 + throw err;
29 29 });
30 30 };
31 31
@@ -128,15 +128,14 @@ describe("messages", function()
128 128 {
129 129 subject: "this is a test TEXT+HTML+DATA message from emailjs",
130 130 from: "obama@gmail.com",
131   - to: "mitt@gmail.com",
132   - text: "hello friend if you are seeing this, you can not view html emails. it is attached inline.",
  131 + to: "mitt@gmail.com",
133 132 attachment: {data:html, alternative:true}
134 133 };
135 134
136 135 send(message, function(mail)
137 136 {
138 137 expect(mail.html).to.equal(html);
139   - expect(mail.text).to.equal(message.text + "\n");
  138 + expect(mail.text).to.not.equal(message.text + "\n");
140 139 expect(mail.headers.subject).to.equal(message.subject);
141 140 expect(mail.headers.from).to.equal(message.from);
142 141 expect(mail.headers.to).to.equal(message.to);
@@ -152,14 +151,13 @@ describe("messages", function()
152 151 subject: "this is a test TEXT+HTML+FILE message from emailjs",
153 152 from: "thomas@gmail.com",
154 153 to: "nikolas@gmail.com",
155   - text: "hello friend if you are seeing this, you can not view html emails. it is attached inline.",
156 154 attachment: {path:path.join(__dirname, "attachments/smtp.html"), alternative:true}
157 155 };
158 156
159 157 send(headers, function(mail)
160 158 {
161 159 expect(mail.html).to.equal(html);
162   - expect(mail.text).to.equal(headers.text + "\n");
  160 + expect(mail.text).to.not.equal(headers.text + "\n");
163 161 expect(mail.headers.subject).to.equal(headers.subject);
164 162 expect(mail.headers.from).to.equal(headers.from);
165 163 expect(mail.headers.to).to.equal(headers.to);
@@ -176,7 +174,6 @@ describe("messages", function()
176 174 subject: "this is a test TEXT+HTML+IMAGE message from emailjs",
177 175 from: "ninja@gmail.com",
178 176 to: "pirate@gmail.com",
179   - text: "hello friend if you are seeing this, you can not view html emails. it is attached inline.",
180 177 attachment:
181 178 {
182 179 path: path.join(__dirname, "attachments/smtp2.html"),
@@ -197,7 +194,7 @@ describe("messages", function()
197 194 {
198 195 expect(mail.attachments[0].content.toString("base64")).to.equal(image.toString("base64"));
199 196 expect(mail.html).to.equal(html);
200   - expect(mail.text).to.equal(headers.text + "\n");
  197 + expect(mail.text).to.not.equal(headers.text + "\n");
201 198 expect(mail.headers.subject).to.equal(headers.subject);
202 199 expect(mail.headers.from).to.equal(headers.from);
203 200 expect(mail.headers.to).to.equal(headers.to);
@@ -205,6 +202,29 @@ describe("messages", function()
205 202 });
206 203 });
207 204
  205 + it("html data and attachment", function(done) {
  206 + var html = fs.readFileSync(path.join(__dirname, "attachments/smtp.html"), "utf-8");
  207 + var headers =
  208 + {
  209 + subject: "this is a test TEXT+HTML+FILE message from emailjs",
  210 + from: "thomas@gmail.com",
  211 + to: "nikolas@gmail.com",
  212 + attachment: [
  213 + {path:path.join(__dirname, "attachments/smtp.html"), alternative:true},
  214 + {path:path.join(__dirname, "attachments/smtp.gif")}
  215 + ]
  216 + };
  217 +
  218 + send(headers, function(mail) {
  219 + expect(mail.html).to.equal(html);
  220 + expect(mail.text).to.not.equal(headers.text + "\n");
  221 + expect(mail.headers.subject).to.equal(headers.subject);
  222 + expect(mail.headers.from).to.equal(headers.from);
  223 + expect(mail.headers.to).to.equal(headers.to);
  224 + done();
  225 + });
  226 + });
  227 +
208 228 it("attachment", function(done)
209 229 {
210 230 var pdf = fs.readFileSync(path.join(__dirname, "attachments/smtp.pdf"));

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.