move from node-ses to nodemailer

This commit is contained in:
Oliver Matthews 2014-03-06 15:37:25 +00:00
parent a0b02d85f5
commit 8989284e10
3 changed files with 44 additions and 31 deletions

View file

@ -2,37 +2,49 @@ logger = require('logger-sharelatex')
metrics = require('../../infrastructure/Metrics')
Settings = require('settings-sharelatex')
metrics = require("../../infrastructure/Metrics")
ses = require('node-ses')
nodemailer = require("nodemailer")
if Settings.email? and Settings.email.fromAddress?
defaultFromAddress = Settings.email.fromAddress
else
else
defaultFromAddress = ""
if Settings.email?.ses? and Settings.email.ses?.key? and Settings.email.ses?.key != "" and Settings.email.ses?.secret? and Settings.email.ses?.secret != ""
client = ses.createClient({ key: Settings.email.ses.key, secret: Settings.email.ses.secret });
else
logger.warn "AWS SES credentials are not configured. No emails will be sent."
client =
sendemail: (options, callback = (err, data, res) ->) ->
logger.log options: options, "would send email if SES credentials enabled"
callback()
# provide dummy mailer unless we have a better one configured.
client =
sendMail: (options, callback = (err,status) ->) ->
logger.log options:options, "Would send email if enabled."
callback()
createSesClient = (settings) ->
if settings? and settings.key? and settings.key != "" and settings.secret? and settings.secret != ""
client = nodemailer.createTransport("SES", {AWSAccessKeyID: settings.key, AWSSecretKey: settings.secret} )
else
logger.warn "AWS SES credentials are not configured. No emails will be sent."
if Settings.email?
switch Settings.email.transport
when "ses"
createSesClient( Settings.email.ses)
# TODO direct, client
when undefined,null,""
logger.warn "No Email transport defined. No emails will be sent."
else
logger.warn "Uknown email transport #{Settings.email.transport}. No emails will be sent."
module.exports =
sendEmail : (options, callback = (error) ->)->
logger.log receiver:options.receiver, subject:options.subject, "sending email"
metrics.inc "email"
options =
options =
to: options.to
from: defaultFromAddress
subject: options.subject
message: options.html
replyTo: options.replyTo || Settings.email.replyToAddress
client.sendemail options, (err, data, res)->
client.sendMail options, (err, res)->
if err?
logger.err err:err, "error sending message"
else
logger.log "Message sent to #{options.to}"
callback(err)

View file

@ -32,7 +32,7 @@
"fairy": "0.0.2",
"node-uuid": "1.4.0",
"mongojs": "0.9.8",
"node-ses": "0.0.3",
"nodemailer": "0.6.1",
"bcrypt": "0.7.5",
"archiver": "0.5.1",
"nodetime": "0.8.15",

View file

@ -12,20 +12,21 @@ describe "Email", ->
@settings =
email:
transport: "ses"
ses:
key: "key"
secret: "secret"
fromAddress: "bob@bob.com"
replyToAddress: "sally@gmail.com"
@sesClient =
sendemail: sinon.stub()
@ses =
createClient: => @sesClient
@sesClient =
sendMail: sinon.stub()
@ses =
createTransport: => @sesClient
@sender = SandboxedModule.require modulePath, requires:
'node-ses': @ses
'nodemailer': @ses
"settings-sharelatex":@settings
"logger-sharelatex":
"logger-sharelatex":
log:->
warn:->
err:->
@ -38,44 +39,44 @@ describe "Email", ->
describe "sendEmail", ->
it "should set the properties on the email to send", (done)->
@sesClient.sendemail.callsArgWith(1)
@sesClient.sendMail.callsArgWith(1)
@sender.sendEmail @opts, =>
args = @sesClient.sendemail.args[0][0]
args = @sesClient.sendMail.args[0][0]
args.message.should.equal @opts.html
args.to.should.equal @opts.to
args.subject.should.equal @opts.subject
done()
it "should return the error", (done)->
@sesClient.sendemail.callsArgWith(1, "error")
it "should return the error", (done)->
@sesClient.sendMail.callsArgWith(1, "error")
@sender.sendEmail {}, (err)=>
err.should.equal "error"
done()
it "should use the from address from settings", (done)->
@sesClient.sendemail.callsArgWith(1)
@sesClient.sendMail.callsArgWith(1)
@sender.sendEmail @opts, =>
args = @sesClient.sendemail.args[0][0]
args = @sesClient.sendMail.args[0][0]
args.from.should.equal @settings.email.fromAddress
done()
it "should use the reply to address from settings", (done)->
@sesClient.sendemail.callsArgWith(1)
@sesClient.sendMail.callsArgWith(1)
@sender.sendEmail @opts, =>
args = @sesClient.sendemail.args[0][0]
args = @sesClient.sendMail.args[0][0]
args.replyTo.should.equal @settings.email.replyToAddress
done()
it "should use the reply to address in options as an override", (done)->
@sesClient.sendemail.callsArgWith(1)
@sesClient.sendMail.callsArgWith(1)
@opts.replyTo = "someone@else.com"
@sender.sendEmail @opts, =>
args = @sesClient.sendemail.args[0][0]
args = @sesClient.sendMail.args[0][0]
args.replyTo.should.equal @opts.replyTo
done()