Fix from header in contact form

This commit is contained in:
Chocobozzz 2019-02-14 11:56:23 +01:00
parent b7a7e801ac
commit 4759fedc61
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
4 changed files with 25 additions and 9 deletions

View File

@ -361,7 +361,8 @@ class Emailer {
'PeerTube.' 'PeerTube.'
const emailPayload: EmailPayload = { const emailPayload: EmailPayload = {
from: fromEmail, fromDisplayName: fromEmail,
replyTo: fromEmail,
to: [ CONFIG.ADMIN.EMAIL ], to: [ CONFIG.ADMIN.EMAIL ],
subject: '[PeerTube] Contact form submitted', subject: '[PeerTube] Contact form submitted',
text text
@ -370,16 +371,21 @@ class Emailer {
return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
} }
sendMail (to: string[], subject: string, text: string, from?: string) { sendMail (options: EmailPayload) {
if (!Emailer.isEnabled()) { if (!Emailer.isEnabled()) {
throw new Error('Cannot send mail because SMTP is not configured.') throw new Error('Cannot send mail because SMTP is not configured.')
} }
const fromDisplayName = options.fromDisplayName
? options.fromDisplayName
: CONFIG.WEBSERVER.HOST
return this.transporter.sendMail({ return this.transporter.sendMail({
from: from || CONFIG.SMTP.FROM_ADDRESS, from: `"${fromDisplayName}" <${CONFIG.SMTP.FROM_ADDRESS}>`,
to: to.join(','), replyTo: options.replyTo,
subject, to: options.to.join(','),
text subject: options.subject,
text: options.text
}) })
} }

View File

@ -6,14 +6,16 @@ export type EmailPayload = {
to: string[] to: string[]
subject: string subject: string
text: string text: string
from?: string
fromDisplayName?: string
replyTo?: string
} }
async function processEmail (job: Bull.Job) { async function processEmail (job: Bull.Job) {
const payload = job.data as EmailPayload const payload = job.data as EmailPayload
logger.info('Processing email in job %d.', job.id) 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)
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------

View File

@ -45,7 +45,8 @@ describe('Test contact form', function () {
const email = emails[0] 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['to'][0]['address']).equal('admin1@example.com')
expect(email['subject']).contains('Contact form') expect(email['subject']).contains('Contact form')
expect(email['text']).contains('my super message') expect(email['text']).contains('my super message')

View File

@ -89,6 +89,7 @@ describe('Test emails', function () {
const email = emails[0] const email = emails[0]
expect(email['from'][0]['name']).equal('localhost:9001')
expect(email['from'][0]['address']).equal('test-admin@localhost') expect(email['from'][0]['address']).equal('test-admin@localhost')
expect(email['to'][0]['address']).equal('user_1@example.com') expect(email['to'][0]['address']).equal('user_1@example.com')
expect(email['subject']).contains('password') expect(email['subject']).contains('password')
@ -133,6 +134,7 @@ describe('Test emails', function () {
const email = emails[1] const email = emails[1]
expect(email['from'][0]['name']).equal('localhost:9001')
expect(email['from'][0]['address']).equal('test-admin@localhost') expect(email['from'][0]['address']).equal('test-admin@localhost')
expect(email['to'][0]['address']).equal('admin1@example.com') expect(email['to'][0]['address']).equal('admin1@example.com')
expect(email['subject']).contains('abuse') expect(email['subject']).contains('abuse')
@ -152,6 +154,7 @@ describe('Test emails', function () {
const email = emails[2] const email = emails[2]
expect(email['from'][0]['name']).equal('localhost:9001')
expect(email['from'][0]['address']).equal('test-admin@localhost') expect(email['from'][0]['address']).equal('test-admin@localhost')
expect(email['to'][0]['address']).equal('user_1@example.com') expect(email['to'][0]['address']).equal('user_1@example.com')
expect(email['subject']).contains(' blocked') expect(email['subject']).contains(' blocked')
@ -169,6 +172,7 @@ describe('Test emails', function () {
const email = emails[3] const email = emails[3]
expect(email['from'][0]['name']).equal('localhost:9001')
expect(email['from'][0]['address']).equal('test-admin@localhost') expect(email['from'][0]['address']).equal('test-admin@localhost')
expect(email['to'][0]['address']).equal('user_1@example.com') expect(email['to'][0]['address']).equal('user_1@example.com')
expect(email['subject']).contains(' unblocked') expect(email['subject']).contains(' unblocked')
@ -188,6 +192,7 @@ describe('Test emails', function () {
const email = emails[4] const email = emails[4]
expect(email['from'][0]['name']).equal('localhost:9001')
expect(email['from'][0]['address']).equal('test-admin@localhost') expect(email['from'][0]['address']).equal('test-admin@localhost')
expect(email['to'][0]['address']).equal('user_1@example.com') expect(email['to'][0]['address']).equal('user_1@example.com')
expect(email['subject']).contains(' blacklisted') expect(email['subject']).contains(' blacklisted')
@ -205,6 +210,7 @@ describe('Test emails', function () {
const email = emails[5] const email = emails[5]
expect(email['from'][0]['name']).equal('localhost:9001')
expect(email['from'][0]['address']).equal('test-admin@localhost') expect(email['from'][0]['address']).equal('test-admin@localhost')
expect(email['to'][0]['address']).equal('user_1@example.com') expect(email['to'][0]['address']).equal('user_1@example.com')
expect(email['subject']).contains(' unblacklisted') expect(email['subject']).contains(' unblacklisted')
@ -224,6 +230,7 @@ describe('Test emails', function () {
const email = emails[6] const email = emails[6]
expect(email['from'][0]['name']).equal('localhost:9001')
expect(email['from'][0]['address']).equal('test-admin@localhost') expect(email['from'][0]['address']).equal('test-admin@localhost')
expect(email['to'][0]['address']).equal('user_1@example.com') expect(email['to'][0]['address']).equal('user_1@example.com')
expect(email['subject']).contains('Verify') expect(email['subject']).contains('Verify')