Skip to content
Snippets Groups Projects
Commit 6bd67b45 authored by Marcelo Schmidt's avatar Marcelo Schmidt
Browse files

RocketMailer and Unsubscription

parent 3da68fdd
No related merge requests found
Showing with 78 additions and 16 deletions
...@@ -8,9 +8,8 @@ FlowRouter.route '/rocket-mailer', ...@@ -8,9 +8,8 @@ FlowRouter.route '/rocket-mailer',
action: -> action: ->
BlazeLayout.render 'main', {center: 'rocketMailer'} BlazeLayout.render 'main', {center: 'rocketMailer'}
FlowRouter.route '/rocket-mailer/unsubscribe/:hash', FlowRouter.route '/rocket-mailer/unsubscribe/:_id/:createdAt',
name: 'rocket-mailer-unsubscribe' name: 'rocket-mailer-unsubscribe'
action: (params) -> action: (params) ->
console.log params.hash Meteor.call 'RocketMailer.unsubscribe', params._id, params.createdAt
Meteor.call 'RocketMailer.unsubscribe', params.hash
BlazeLayout.render 'rocketMailerUnsubscribe' BlazeLayout.render 'rocketMailerUnsubscribe'
Template.rocketMailer.events Template.rocketMailer.events
'click .send': (e) -> 'click .send': (e, t) ->
e.preventDefault() e.preventDefault()
console.log 'Must send e-mail' from = $(t.find('[name=from]')).val()
subject = $(t.find('[name=subject]')).val()
body = $(t.find('[name=body]')).val()
if body.indexOf('[unsubscribe]') is -1
toastr.error TAPi18n.__('You_must_provide_the_unsubscribe_link')
else
Meteor.call 'RocketMailer.sendMail', from, subject, body, (err) ->
return toastr.error err.reason if err
toastr.success TAPi18n.__('The_emails_are_being_sent')
...@@ -16,7 +16,10 @@ ...@@ -16,7 +16,10 @@
<div class="input-line"> <div class="input-line">
<label>{{_ "Email_from"}}</label> <label>{{_ "Email_from"}}</label>
<div> <div>
<input type="text" name="from" value="" />@rocket.chat <input type="text" name="from" value="" />
</div>
<div>
<small class="settings-description">{{_ "Currently_only_rocket_chat_emails"}}</small>
</div> </div>
</div> </div>
<div class="input-line"> <div class="input-line">
...@@ -30,6 +33,9 @@ ...@@ -30,6 +33,9 @@
<div> <div>
<textarea name="body" rows="10" style="height: auto"></textarea> <textarea name="body" rows="10" style="height: auto"></textarea>
</div> </div>
<div>
<small class="settings-description">{{{_ "RocketMailer_body_tags"}}}</small>
</div>
</div> </div>
</fieldset> </fieldset>
<div class="submit"> <div class="submit">
......
{ {
"Currently_only_rocket_chat_emails": "Currently only accepting @rocket.chat e-mails",
"Email_from": "From", "Email_from": "From",
"Email_subject": "Subject", "Email_subject": "Subject",
"Email_body": "E-mail body", "Email_body": "E-mail body",
"Rocket_Mailer": "Rocket Mailer", "Rocket_Mailer": "Rocket Mailer",
"RocketMailer_body_tags": "You <b>must</b> use [unsubscribe] for the unsubscription link.<br />You may use [name], [fname], [lname] for the user's full name, first name or last name, respectively.<br />You may use [email] for the user's e-mail.",
"Send_email": "Send E-mail", "Send_email": "Send E-mail",
"The_emails_are_being_sent": "The e-mails are being sent.",
"You_are_not_authorized_to_view_this_page": "You are not authorized to view this page.", "You_are_not_authorized_to_view_this_page": "You are not authorized to view this page.",
"You_have_successfully_unsubscribed": "You have successfully unsubscribed from our Mailling List." "You_have_successfully_unsubscribed": "You have successfully unsubscribed from our Mailling List.",
"You_informed_an_invalid_FROM_address": "You informed an invalid FROM address.",
"You_must_provide_the_unsubscribe_link": "You must provide the [unsubscribe] link."
} }
...@@ -30,6 +30,7 @@ Package.onUse(function(api) { ...@@ -30,6 +30,7 @@ Package.onUse(function(api) {
'server/models/Users.coffee', 'server/models/Users.coffee',
'server/functions/sendMail.coffee', 'server/functions/sendMail.coffee',
'server/functions/unsubscribe.coffee', 'server/functions/unsubscribe.coffee',
'server/methods/sendMail.coffee',
'server/methods/unsubscribe.coffee' 'server/methods/unsubscribe.coffee'
], 'server'); ], 'server');
......
RocketMailer.sendMail = (from, subject, body) -> RocketMailer.sendMail = (from, subject, body) ->
rocketchatMailPattern = /^(?:.*<)?([a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@rocket.chat)(?:>?)$/
unless rocketchatMailPattern.test from
throw new Meteor.Error 'invalid-from-address', TAPi18n.__('You_informed_an_invalid_FROM_address')
if body.indexOf('[unsubscribe]') is -1
throw new Meteor.Error 'missing-unsubscribe-link', TAPi18n.__('You_must_provide_the_unsubscribe_link')
Meteor.users.find({ "rocketMailer.unsubscribed": { $exists: 0 } }).forEach (user) -> Meteor.users.find({ "rocketMailer.unsubscribed": { $exists: 0 } }).forEach (user) ->
# Meteor.users.find({ "username": /\.rocket\.team/ }).forEach (user) ->
email = user.emails?[0]?.address email = user.emails?[0]?.address
if email
html = body.replace /\[unsubscribe\]/g, Meteor.absoluteUrl(FlowRouter.path('rocket-mailer/unsubscribe/:hash', { hash: "#{user._id}:#{user.createdAt.getTime()}" }))
html = html.replace /\[name\]/g, user.name
fname = _.strLeft user.name, ' '
lname = _.strRightBack user.name, ' '
html = html.replace /\[fname\]/g, fname
html = html.replace /\[lname\]/g, lname
html = html.replace /\[email\]/g, email
html = html.replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + '<br>' + '$2')
# rfcMailPatternWithName = /^(?:.*<)?([a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*)(?:>?)$/
# if rfcMailPatternWithName.test email
rfcMailPattern = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
if rfcMailPattern.test email
Meteor.defer -> Meteor.defer ->
console.log email Email.send
# mailsend to: email
from: from
subject: subject
html: html
console.log 'Sending email to ' + email
RocketMailer.unsubscribe = (hash) -> RocketMailer.unsubscribe = (_id, createdAt) ->
[_id, createdAt] = hash.split ':'
if _id and createdAt if _id and createdAt
return RocketChat.models.Users.RocketMailUnsubscribe(_id, createdAt) == 1 return RocketChat.models.Users.RocketMailUnsubscribe(_id, createdAt) == 1
return false return false
Meteor.methods
'RocketMailer.sendMail': (from, subject, body) ->
console.log '[method] RocketMailer.sendMail', from, subject, body
return RocketMailer.sendMail from, subject, body
# Limit setting username once per minute
# DDPRateLimiter.addRule
# type: 'method'
# name: 'RocketMailer.sendMail'
# connectionId: -> return true
# , 1, 60000
Meteor.methods Meteor.methods
'RocketMailer.unsubscribe': (hash) -> 'RocketMailer.unsubscribe': (_id, createdAt) ->
return RocketMailer.unsubscribe hash return RocketMailer.unsubscribe _id, createdAt
# Limit setting username once per minute # Limit setting username once per minute
DDPRateLimiter.addRule DDPRateLimiter.addRule
......
# Extends model Users # Extends model Users
RocketChat.models.Users.RocketMailUnsubscribe = (_id, createdAt) -> RocketChat.models.Users.RocketMailUnsubscribe = (_id, createdAt) ->
console.log '[RocketMailer.Unsubscribe]', _id, createdAt, new Date(parseInt createdAt)
query = query =
_id: _id _id: _id
...@@ -11,4 +10,8 @@ RocketChat.models.Users.RocketMailUnsubscribe = (_id, createdAt) -> ...@@ -11,4 +10,8 @@ RocketChat.models.Users.RocketMailUnsubscribe = (_id, createdAt) ->
$set: $set:
"rocketMailer.unsubscribed": true "rocketMailer.unsubscribed": true
return @update query, update affectedRows = @update query, update
console.log '[RocketMailer.Unsubscribe]', _id, createdAt, new Date(parseInt createdAt), affectedRows
return affectedRows
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment