-
Notifications
You must be signed in to change notification settings - Fork 543
/
Copy pathemailer.ls
executable file
·58 lines (48 loc) · 1.92 KB
/
emailer.ls
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
@__emailer__ = null
@include = ->
return @__emailer__ if @__emailer__
emailer = {}
emailer.log = -> console.log "email tester"
nodemailer = try require \nodemailer
return unless nodemailer
generator = require \xoauth2 .createXOAuth2Generator do
#// **********************************
#// Please add OAuth2 values
#// from OAuth playground: https://developers.google.com/oauthplayground/
#// to node environment vars (process.env.)
#// **********************************
user: process.env.gmail_user #// Your gmail address.
clientId: process.env.gmail_clientId
clientSecret: process.env.gmail_clientSecret
refreshToken: process.env.gmail_refreshToken
#// listen for token updates
#// you probably want to store these to a db
generator.on \token (token) ->
#console.log "New token for #{token.user}: #{token.accessToken}"
#// login
smtpTransport = nodemailer.createTransport do
service: 'gmail'
auth:
xoauth2: generator
emailer.sendemail_ignore = (emailTo, emailSubject, emailBody, callback) ->
callback " [E-mail Sent]"
#debug return before sending
emailer.sendemail = (emailTo, emailSubject, emailBody, callback) !->
mailOptions =
from: process.env.gmail_user
to: emailTo #// to address
subject: emailSubject #// Subject line
text: emailBody #// plaintext body
html: emailBody #// html: '<b>Hello world </b>' #// html body
smtpTransport.sendMail mailOptions, (error, info) !->
if error
console.log error
console.dir info
callback " EMAIL ERROR - "+error
else
#console.log 'Message sent to:'+(info.accepted)
#//console.dir(info);
callback " [E-mail Sent]"
return info.accepted
smtpTransport.close();
@__emailer__ = emailer