summaryrefslogtreecommitdiff
path: root/server/lib/auth/mail.js
blob: 0ba6d5d899570dc7c587d6d62106a1e3b853ae66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
var email = require("emailjs"),
	ejs = require("ejs"),
	fs = require("fs"),
	util = require("../util");

var mail = {
	
	from: 'Vvalls <info@vvalls.com>',
	templates: {},
	
	init: function(){
		var names = ["welcome","password","collaborator"].forEach(function(name){
			mail.templates[name] = {};
			var types = ["text","html"].forEach(function(type){
				fs.readFile("views/mail/" + name + "." + type + ".ejs", function(err, data){
					mail.templates[name][type] = ejs.compile(data.toString())
				})
			})
		})
	},
	
	connect: function(){
		var server = email.server.connect({
			user: process.env.OKFOCUS_EMAIL_USERNAME, 
			password: process.env.OKFOCUS_EMAIL_PASSWORD, 
			host: "smtp.sendgrid.net", 
			ssl: true
		})
		return server
	},
	
	send: function(msg, cb){
		var server = mail.connect()
		server.send(msg, cb)
	},
	
	welcome: function(user, cb){
		var message = {
			 text:    mail.templates.welcome.text(user),
			 from:    mail.from,
			 to:      user.email,
			 subject: "Welcome to Vvalls",
			 attachment: [
					{ data: mail.templates.welcome.html(user), alternative: true },
			 ]
		};
		mail.send(message, cb)
		console.log("sent welcome email to", user.email)
	},
	
	forgotPassword: function(user, cb){
		var message = {
			 text:    mail.templates.password.text(user),
			 from:    mail.from, 
			 to:      user.email,
			 subject: "Recover your password",
			 attachment: [
					{ data: mail.templates.password.html(user), alternative: true },
			 ]
		}
		mail.send(message, cb)
		console.log("sent password email to", user.email)
	},

	collaborator: function(project, user, collaborator, cb){
    var data = {
      projectSlug: project.slug,
      projectName: project.name,
      nonce: collaborator.nonce,
      username: user.username,
    }

		var message = {
			 text:    mail.templates.collaborator.text(data),
			 from:    mail.from, 
			 to:      collaborator.email,
			 subject: "Join " + data.username + " on " + data.projectName,
			 attachment: [
					{ data: mail.templates.collaborator.html(data), alternative: true },
			 ]
		}
		mail.send(message, cb)
		console.log("sent collaborator email to", collaborator.email)
	},

}

module.exports = mail