Fix from header in contact form
authorChocobozzz <me@florianbigard.com>
Thu, 14 Feb 2019 10:56:23 +0000 (11:56 +0100)
committerChocobozzz <me@florianbigard.com>
Fri, 15 Feb 2019 08:43:38 +0000 (09:43 +0100)
server/lib/emailer.ts
server/lib/job-queue/handlers/email.ts
server/tests/api/server/contact-form.ts
server/tests/api/server/email.ts

index 672414cc0c95ac94ca18d5fb64cd3f6621cd6ac4..cbe384061355d8626029c89f27330b6e5cca80c2 100644 (file)
@@ -361,7 +361,8 @@ class Emailer {
       'PeerTube.'
 
     const emailPayload: EmailPayload = {
-      from: fromEmail,
+      fromDisplayName: fromEmail,
+      replyTo: fromEmail,
       to: [ CONFIG.ADMIN.EMAIL ],
       subject: '[PeerTube] Contact form submitted',
       text
@@ -370,16 +371,21 @@ class Emailer {
     return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
   }
 
-  sendMail (to: string[], subject: string, text: string, from?: string) {
+  sendMail (options: EmailPayload) {
     if (!Emailer.isEnabled()) {
       throw new Error('Cannot send mail because SMTP is not configured.')
     }
 
+    const fromDisplayName = options.fromDisplayName
+      ? options.fromDisplayName
+      : CONFIG.WEBSERVER.HOST
+
     return this.transporter.sendMail({
-      from: from || CONFIG.SMTP.FROM_ADDRESS,
-      to: to.join(','),
-      subject,
-      text
+      from: `"${fromDisplayName}" <${CONFIG.SMTP.FROM_ADDRESS}>`,
+      replyTo: options.replyTo,
+      to: options.to.join(','),
+      subject: options.subject,
+      text: options.text
     })
   }
 
index 220d0af32e80ecd62b61f7f5e8da58e234b40439..2ba39a156d805e8e0c1af2c6925410613c80f70b 100644 (file)
@@ -6,14 +6,16 @@ export type EmailPayload = {
   to: string[]
   subject: string
   text: string
-  from?: string
+
+  fromDisplayName?: string
+  replyTo?: string
 }
 
 async function processEmail (job: Bull.Job) {
   const payload = job.data as EmailPayload
   logger.info('Processing email in job %d.', job.id)
 
-  return Emailer.Instance.sendMail(payload.to, payload.subject, payload.text, payload.from)
+  return Emailer.Instance.sendMail(payload)
 }
 
 // ---------------------------------------------------------------------------
index 93221d0a3c6d17e677f43d6dbe69bc11a4ddf3bc..06a2f89b0b97154f68ebd1d1a1171ee7554dc2b1 100644 (file)
@@ -45,7 +45,8 @@ describe('Test contact form', function () {
 
     const email = emails[0]
 
-    expect(email['from'][0]['address']).equal('toto@example.com')
+    expect(email['from'][0]['address']).equal('test-admin@localhost')
+    expect(email['from'][0]['name']).equal('toto@example.com')
     expect(email['to'][0]['address']).equal('admin1@example.com')
     expect(email['subject']).contains('Contact form')
     expect(email['text']).contains('my super message')
index f96c57b6636721360a23e2d1145d7fe2602c413b..f8f16f54fce93cfed4afa543b6c97cde3213eb0c 100644 (file)
@@ -89,6 +89,7 @@ describe('Test emails', function () {
 
       const email = emails[0]
 
+      expect(email['from'][0]['name']).equal('localhost:9001')
       expect(email['from'][0]['address']).equal('test-admin@localhost')
       expect(email['to'][0]['address']).equal('user_1@example.com')
       expect(email['subject']).contains('password')
@@ -133,6 +134,7 @@ describe('Test emails', function () {
 
       const email = emails[1]
 
+      expect(email['from'][0]['name']).equal('localhost:9001')
       expect(email['from'][0]['address']).equal('test-admin@localhost')
       expect(email['to'][0]['address']).equal('admin1@example.com')
       expect(email['subject']).contains('abuse')
@@ -152,6 +154,7 @@ describe('Test emails', function () {
 
       const email = emails[2]
 
+      expect(email['from'][0]['name']).equal('localhost:9001')
       expect(email['from'][0]['address']).equal('test-admin@localhost')
       expect(email['to'][0]['address']).equal('user_1@example.com')
       expect(email['subject']).contains(' blocked')
@@ -169,6 +172,7 @@ describe('Test emails', function () {
 
       const email = emails[3]
 
+      expect(email['from'][0]['name']).equal('localhost:9001')
       expect(email['from'][0]['address']).equal('test-admin@localhost')
       expect(email['to'][0]['address']).equal('user_1@example.com')
       expect(email['subject']).contains(' unblocked')
@@ -188,6 +192,7 @@ describe('Test emails', function () {
 
       const email = emails[4]
 
+      expect(email['from'][0]['name']).equal('localhost:9001')
       expect(email['from'][0]['address']).equal('test-admin@localhost')
       expect(email['to'][0]['address']).equal('user_1@example.com')
       expect(email['subject']).contains(' blacklisted')
@@ -205,6 +210,7 @@ describe('Test emails', function () {
 
       const email = emails[5]
 
+      expect(email['from'][0]['name']).equal('localhost:9001')
       expect(email['from'][0]['address']).equal('test-admin@localhost')
       expect(email['to'][0]['address']).equal('user_1@example.com')
       expect(email['subject']).contains(' unblacklisted')
@@ -224,6 +230,7 @@ describe('Test emails', function () {
 
       const email = emails[6]
 
+      expect(email['from'][0]['name']).equal('localhost:9001')
       expect(email['from'][0]['address']).equal('test-admin@localhost')
       expect(email['to'][0]['address']).equal('user_1@example.com')
       expect(email['subject']).contains('Verify')