Просмотр исходного кода

Merge pull request #54 from cwoac/nodemailer-passthrough

Nodemailer passthrough
Henry Oswald 12 лет назад
Родитель
Сommit
fc2b42400f

+ 21 - 15
services/web/app/coffee/Features/Email/EmailSender.coffee

@@ -2,37 +2,43 @@ 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()
 
-module.exports =
+if Settings.email?
+	if Settings.email.transport? and Settings.email.parameters?
+		nm_client = nodemailer.createTransport( Settings.email.transport, Settings.email.parameters )
+		if nm_client
+			client = nm_client
+		else
+			logger.warn "Failed to create email transport. Please check your settings. No email will be sent."
+	else
+		logger.warn "Email transport and/or parameters not defined. No emails will be sent."
 
+module.exports =
 	sendEmail : (options, callback = (error) ->)->
 		logger.log receiver:options.to, subject:options.subject, "sending email"
 		metrics.inc "email"
-		options = 
+		options =
 			to: options.to
 			from: defaultFromAddress
 			subject: options.subject
-			message: options.html
+			html: 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)
-	
+

+ 16 - 9
services/web/config/settings.development.coffee

@@ -134,6 +134,22 @@ module.exports =
 		{name: "English", code: "en"}
 	]
 
+	# Email support
+	# -------------
+	#
+	#	ShareLaTeX uses nodemailer (http://www.nodemailer.com/) to send transactional emails.
+	#	To see the range of transport and options they support, see http://www.nodemailer.com/docs/transports
+	#email:
+	#	fromAddress: ""
+	#	replyTo: ""
+	#	lifecycle: false
+	## Example transport and parameter settings for Amazon SES
+	#	transport: "SES"
+	#	parameters:
+	#		AWSAccessKeyID: ""
+	#		AWSSecretKey: ""
+
+
 	# Third party services
 	# --------------------
 	#
@@ -153,15 +169,6 @@ module.exports =
 	# tenderUrl: ""
 	#
 
-	# 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
 	# -------------------

+ 1 - 1
services/web/package.json

@@ -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",

+ 21 - 20
services/web/test/UnitTests/coffee/Email/EmailSenderTests.coffee

@@ -12,20 +12,21 @@ describe "Email", ->
 
 		@settings =
 			email:
-				ses:
-					key: "key"
-					secret: "secret"
+				transport: "ses"
+				parameters:
+					AWSAccessKeyID: "key"
+					AWSSecretKey: "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.message.should.equal @opts.html
+				args = @sesClient.sendMail.args[0][0]
+				args.html.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()