diff --git a/services/web/app/coffee/Features/Email/EmailHandler.coffee b/services/web/app/coffee/Features/Email/EmailHandler.coffee index 5430db7473..6316c21511 100644 --- a/services/web/app/coffee/Features/Email/EmailHandler.coffee +++ b/services/web/app/coffee/Features/Email/EmailHandler.coffee @@ -1,14 +1,14 @@ -EmailTemplator = require "./EmailTemplator" +settings = require("settings-sharelatex") +EmailBuilder = require "./EmailBuilder" EmailSender = require "./EmailSender" module.exports = sendEmail : (emailType, opts, callback)-> - email = EmailTemplator.buildEmail emailType, opts + email = EmailBuilder.buildEmail emailType, opts + if email.type == "lifecycle" and !settings.email.lifecycle + return callback() opts.html = email.html opts.subject = email.subject EmailSender.sendEmail opts, (err)-> - callback(err) - -# module.exports.sendEmail "welcome", {first_name:"henry", to:"henry.oswald@gmail.com"}, -> - + callback(err) \ No newline at end of file diff --git a/services/web/app/coffee/Features/Email/EmailSender.coffee b/services/web/app/coffee/Features/Email/EmailSender.coffee index 9e9de012a1..ff34d9d60e 100644 --- a/services/web/app/coffee/Features/Email/EmailSender.coffee +++ b/services/web/app/coffee/Features/Email/EmailSender.coffee @@ -4,8 +4,8 @@ Settings = require('settings-sharelatex') metrics = require("../../infrastructure/Metrics") ses = require('node-ses') -if Settings.ses?.key? and Settings.ses?.key != "" and Settings.ses?.secret? and Settings.ses?.secret != "" - client = ses.createClient({ key: Settings.ses.key, secret: Settings.ses.secret }); +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 = diff --git a/services/web/config/settings.development.coffee b/services/web/config/settings.development.coffee index 385fc72646..5d10678a19 100644 --- a/services/web/config/settings.development.coffee +++ b/services/web/config/settings.development.coffee @@ -26,6 +26,7 @@ module.exports = secret: "" bucketName : "" + # Databases # --------- mongo: @@ -155,11 +156,16 @@ module.exports = # ShareLaTeX's help desk is provided by tenderapp.com # tenderUrl: "" # - # ShareLaTeX uses Amazon's SES api to send transactional emails. - # Uncomment these lines and provide your credentials to be able to send emails. - # ses: - # "key":"" - # "secret":"" + + # email: + # fromAddress: "" + # replyTo: "" + # lifecycle: false + # ShareLaTeX uses Amazon's SES api to send transactional emails. + # Uncomment these lines and provide your credentials to be able to send emails. + # ses: + # "key":"" + # "secret":"" # Production Settings # ------------------- diff --git a/services/web/test/UnitTests/coffee/Email/EmailHandlerTests.coffee b/services/web/test/UnitTests/coffee/Email/EmailHandlerTests.coffee index 7e668415a1..21a764f597 100644 --- a/services/web/test/UnitTests/coffee/Email/EmailHandlerTests.coffee +++ b/services/web/test/UnitTests/coffee/Email/EmailHandlerTests.coffee @@ -10,13 +10,14 @@ describe "EmailHandler", -> beforeEach -> - @settings = {} - @EmailTemplator = + @settings = + email:{} + @EmailBuilder = buildEmail:sinon.stub() @EmailSender = sendEmail:sinon.stub() @EmailHandler = SandboxedModule.require modulePath, requires: - "./EmailTemplator":@EmailTemplator + "./EmailBuilder":@EmailBuilder "./EmailSender":@EmailSender "settings-sharelatex":@settings "logger-sharelatex": log:-> @@ -26,7 +27,7 @@ describe "EmailHandler", -> describe "send email", -> it "should use the correct options", (done)-> - @EmailTemplator.buildEmail.returns({html:@html}) + @EmailBuilder.buildEmail.returns({html:@html}) @EmailSender.sendEmail.callsArgWith(1) opts = @@ -36,10 +37,8 @@ describe "EmailHandler", -> args.html.should.equal @html done() - - it "should return the erroor", (done)-> - @EmailTemplator.buildEmail.returns({html:@html}) + @EmailBuilder.buildEmail.returns({html:@html}) @EmailSender.sendEmail.callsArgWith(1, "error") opts = @@ -48,3 +47,30 @@ describe "EmailHandler", -> @EmailHandler.sendEmail "welcome", opts, (err)=> err.should.equal "error" done() + + it "should not send an email if lifecycle is not enabled", (done)-> + @settings.email.lifecycle = false + @EmailBuilder.buildEmail.returns({type:"lifecycle"}) + @EmailHandler.sendEmail "welcome", {}, => + @EmailSender.sendEmail.called.should.equal false + done() + + it "should send an email if lifecycle is not enabled but the type is notification", (done)-> + @settings.email.lifecycle = false + @EmailBuilder.buildEmail.returns({type:"notification"}) + @EmailSender.sendEmail.callsArgWith(1) + opts = + to: "bob@bob.com" + @EmailHandler.sendEmail "welcome", opts, => + @EmailSender.sendEmail.called.should.equal true + done() + + it "should send lifecycle email if it is enabled", (done)-> + @settings.email.lifecycle = true + @EmailBuilder.buildEmail.returns({type:"lifecycle"}) + @EmailSender.sendEmail.callsArgWith(1) + opts = + to: "bob@bob.com" + @EmailHandler.sendEmail "welcome", opts, => + @EmailSender.sendEmail.called.should.equal true + done()