Skip to content

Commit 3d96deb

Browse files
committed
AWS SES - Simple Email Service example
1 parent e29ccdf commit 3d96deb

File tree

3 files changed

+104
-5
lines changed

3 files changed

+104
-5
lines changed

extend/.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Ignore everything in this directory
22
*
33
# Except this file
4-
!.gitignore
4+
!.gitignore
5+
!aws_ses.example.js

extend/aws_ses.example.js

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Nodemailer SES support: https://nodemailer.com/transports/ses/
2+
// Important steps:
3+
// 1) run "npm install aws-sdk" in "echo $(countly dir)"
4+
// 2) make sure you have an IAM policy to allow access to AWS SES
5+
// 3) update the email variable below with one of your email addresses that was added to SES Identity Management
6+
// 4) update region to one of the 3 valid AWS SES regions: eu-west-1, us-west-2 or us-east-1
7+
// 5) rename or copy this file to countly/extend/mail.js
8+
// Optional: You are free to stop the sendmail process as it is no longer required.
9+
10+
var nodemailer = require('nodemailer');
11+
var aws = require('aws-sdk');
12+
13+
//rename company
14+
var company = "Company";
15+
var email = "[email protected]";
16+
17+
aws.config.update({region: 'eu-west-1'});
18+
19+
module.exports = function(mail) {
20+
//define this if you need to send email from some third party service
21+
mail.sesTransport = nodemailer.createTransport({
22+
SES: new aws.SES({
23+
apiVersion: '2010-12-01'
24+
})
25+
});
26+
27+
mail.sendMail = function(message, callback) {
28+
message.from = company + " <" + email + ">";
29+
mail.sesTransport.sendMail(message, function(error) {
30+
if (error) {
31+
console.log('Error sending email');
32+
console.log(error.message);
33+
}
34+
if (callback) {
35+
callback(error);
36+
}
37+
});
38+
};
39+
40+
mail.sendMessage = function(to, subject, message, callback) {
41+
mail.sendMail({
42+
to: to,
43+
from: company + " <" + email + ">",
44+
subject: subject || "",
45+
html: message || ""
46+
}, callback);
47+
};
48+
49+
mail.sendToNewMember = function(member, memberPassword) {
50+
mail.lookup(function(err, host) {
51+
mail.sendMessage(member.email, "Your " + company + " Account",
52+
"Hi " + mail.getUserFirstName(member) + ",<br/><br/>\n" +
53+
"Your " + company + " account on <a href='" + host + "'>" + host + "</a> is created with the following details;<br/><br/>\n" +
54+
"Username: " + member.username + "<br/>Password: " + memberPassword + "<br/><br/>\n" +
55+
"Enjoy,<br/>A fellow " + company + " Admin");
56+
});
57+
};
58+
59+
mail.sendToUpdatedMember = function(member, memberPassword) {
60+
mail.lookup(function(err, host) {
61+
mail.sendMessage(member.email, "" + company + " Account - Password Change", "Hi " + mail.getUserFirstName(member) + ",<br/><br/>\n" +
62+
"Your password for your " + company + " account on <a href='" + host + "'>" + host + "</a> has been changed. Below you can find your updated account details;<br/><br/>\n" +
63+
"Username: " + member.username + "<br/>Password: " + memberPassword + "<br/><br/>\n" +
64+
"Best,<br/>A fellow " + company + " Admin");
65+
});
66+
};
67+
68+
mail.sendPasswordResetInfo = function(member, prid) {
69+
mail.lookup(function(err, host) {
70+
mail.sendMessage(member.email, "" + company + " Account - Password Reset", "Hi " + mail.getUserFirstName(member) + ",<br/><br/>\n" +
71+
"You can reset your " + company + " account password by following <a href='" + host + "/reset/" + prid + "'>this link</a>.<br/><br/>\n" +
72+
"If you did not request to reset your password ignore this email.<br/><br/>\n" +
73+
"Best,<br/>A fellow " + company + " Admin");
74+
});
75+
};
76+
77+
mail.sendAutomatedMessageError = function(member, link) {
78+
mail.lookup(function(err, host) {
79+
link = host + '/' + link;
80+
mail.sendMessage(member.email, company + " Automated Push Problem", "Hi " + mail.getUserFirstName(member) + ",,<br/><br/>\n" +
81+
"Your <a href=\"" + link + "\">automated message</a> cannot be sent due to a repeating error.\n" +
82+
"Please review message status and reactivate the message once the problem is resolved.<br/><br/>\n" +
83+
"Best,<br/>A fellow " + company + " Admin");
84+
});
85+
};
86+
};

extend/mail.example.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -44,26 +44,38 @@ module.exports = function(mail) {
4444
mail.sendToNewMember = function(member, memberPassword) {
4545
mail.lookup(function(err, host) {
4646
mail.sendMessage(member.email, "Your " + company + " Account",
47-
"Hi " + mail.getUserFirstName(member) + ",<br/><br/>Your " + company + " account on <a href='" + host + "'>" + host + "</a> is created with the following details;<br/><br/>Username: " + member.username + "<br/>Password: " + memberPassword + "<br/><br/>Enjoy,<br/>A fellow " + company + " Admin");
47+
"Hi " + mail.getUserFirstName(member) + ",<br/><br/>\n" +
48+
"Your " + company + " account on <a href='" + host + "'>" + host + "</a> is created with the following details;<br/><br/>\n" +
49+
"Username: " + member.username + "<br/>Password: " + memberPassword + "<br/><br/>\n" +
50+
"Enjoy,<br/>A fellow " + company + " Admin");
4851
});
4952
};
5053

5154
mail.sendToUpdatedMember = function(member, memberPassword) {
5255
mail.lookup(function(err, host) {
53-
mail.sendMessage(member.email, "" + company + " Account - Password Change", "Hi " + mail.getUserFirstName(member) + ",<br/><br/>Your password for your " + company + " account on <a href='" + host + "'>" + host + "</a> has been changed. Below you can find your updated account details;<br/><br/>Username: " + member.username + "<br/>Password: " + memberPassword + "<br/><br/>Best,<br/>A fellow " + company + " Admin");
56+
mail.sendMessage(member.email, "" + company + " Account - Password Change", "Hi " + mail.getUserFirstName(member) + ",<br/><br/>\n" +
57+
"Your password for your " + company + " account on <a href='" + host + "'>" + host + "</a> has been changed. Below you can find your updated account details;<br/><br/>\n" +
58+
"Username: " + member.username + "<br/>Password: " + memberPassword + "<br/><br/>\n" +
59+
"Best,<br/>A fellow " + company + " Admin");
5460
});
5561
};
5662

5763
mail.sendPasswordResetInfo = function(member, prid) {
5864
mail.lookup(function(err, host) {
59-
mail.sendMessage(member.email, "" + company + " Account - Password Reset", "Hi " + mail.getUserFirstName(member) + ",<br/><br/>You can reset your " + company + " account password by following <a href='" + host + "/reset/" + prid + "'>this link</a>.<br/><br/>If you did not request to reset your password ignore this email.<br/><br/>Best,<br/>A fellow " + company + " Admin");
65+
mail.sendMessage(member.email, "" + company + " Account - Password Reset", "Hi " + mail.getUserFirstName(member) + ",<br/><br/>\n" +
66+
"You can reset your " + company + " account password by following <a href='" + host + "/reset/" + prid + "'>this link</a>.<br/><br/>\n" +
67+
"If you did not request to reset your password ignore this email.<br/><br/>\n" +
68+
"Best,<br/>A fellow " + company + " Admin");
6069
});
6170
};
6271

6372
mail.sendAutomatedMessageError = function(member, link) {
6473
mail.lookup(function(err, host) {
6574
link = host + '/' + link;
66-
mail.sendMessage(member.email, company + " Automated Push Problem", "Hi " + mail.getUserFirstName(member) + ",,<br/><br/>Your <a href=\"" + link + "\">automated message</a> cannot be sent due to a repeating error. Please review message status and reactivate the message once the problem is resolved.<br/><br/>Best,<br/>A fellow " + company + " Admin");
75+
mail.sendMessage(member.email, company + " Automated Push Problem", "Hi " + mail.getUserFirstName(member) + ",,<br/><br/>\n" +
76+
"Your <a href=\"" + link + "\">automated message</a> cannot be sent due to a repeating error.\n" +
77+
"Please review message status and reactivate the message once the problem is resolved.<br/><br/>\n" +
78+
"Best,<br/>A fellow " + company + " Admin");
6779
});
6880
};
6981
};

0 commit comments

Comments
 (0)