Skip to content

Commit

Permalink
Fix from header in contact form
Browse files Browse the repository at this point in the history
  • Loading branch information
Chocobozzz committed Feb 14, 2019
1 parent d4804ee commit 7a33818
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
18 changes: 12 additions & 6 deletions server/lib/emailer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
})
}

Expand Down
6 changes: 4 additions & 2 deletions server/lib/job-queue/handlers/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

// ---------------------------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion server/tests/api/server/contact-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ describe('Test contact form', function () {

const email = emails[0]

expect(email['from'][0]['address']).equal('[email protected]')
expect(email['from'][0]['address']).equal('test-admin@localhost')
expect(email['from'][0]['name']).equal('[email protected]')
expect(email['to'][0]['address']).equal('[email protected]')
expect(email['subject']).contains('Contact form')
expect(email['text']).contains('my super message')
Expand Down
7 changes: 7 additions & 0 deletions server/tests/api/server/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('[email protected]')
expect(email['subject']).contains('password')
Expand Down Expand Up @@ -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('[email protected]')
expect(email['subject']).contains('abuse')
Expand All @@ -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('[email protected]')
expect(email['subject']).contains(' blocked')
Expand All @@ -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('[email protected]')
expect(email['subject']).contains(' unblocked')
Expand All @@ -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('[email protected]')
expect(email['subject']).contains(' blacklisted')
Expand All @@ -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('[email protected]')
expect(email['subject']).contains(' unblacklisted')
Expand All @@ -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('[email protected]')
expect(email['subject']).contains('Verify')
Expand Down

0 comments on commit 7a33818

Please sign in to comment.