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',
action: ->
BlazeLayout.render 'main', {center: 'rocketMailer'}
FlowRouter.route '/rocket-mailer/unsubscribe/:hash',
FlowRouter.route '/rocket-mailer/unsubscribe/:_id/:createdAt',
name: 'rocket-mailer-unsubscribe'
action: (params) ->
console.log params.hash
Meteor.call 'RocketMailer.unsubscribe', params.hash
Meteor.call 'RocketMailer.unsubscribe', params._id, params.createdAt
BlazeLayout.render 'rocketMailerUnsubscribe'
Template.rocketMailer.events
'click .send': (e) ->
'click .send': (e, t) ->
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 @@
<div class="input-line">
<label>{{_ "Email_from"}}</label>
<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 class="input-line">
......@@ -30,6 +33,9 @@
<div>
<textarea name="body" rows="10" style="height: auto"></textarea>
</div>
<div>
<small class="settings-description">{{{_ "RocketMailer_body_tags"}}}</small>
</div>
</div>
</fieldset>
<div class="submit">
......
{
"Currently_only_rocket_chat_emails": "Currently only accepting @rocket.chat e-mails",
"Email_from": "From",
"Email_subject": "Subject",
"Email_body": "E-mail body",
"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",
"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_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) {
'server/models/Users.coffee',
'server/functions/sendMail.coffee',
'server/functions/unsubscribe.coffee',
'server/methods/sendMail.coffee',
'server/methods/unsubscribe.coffee'
], 'server');
......
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({ "username": /\.rocket\.team/ }).forEach (user) ->
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 ->
console.log email
# mailsend
Email.send
to: email
from: from
subject: subject
html: html
console.log 'Sending email to ' + email
RocketMailer.unsubscribe = (hash) ->
[_id, createdAt] = hash.split ':'
RocketMailer.unsubscribe = (_id, createdAt) ->
if _id and createdAt
return RocketChat.models.Users.RocketMailUnsubscribe(_id, createdAt) == 1
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
'RocketMailer.unsubscribe': (hash) ->
return RocketMailer.unsubscribe hash
'RocketMailer.unsubscribe': (_id, createdAt) ->
return RocketMailer.unsubscribe _id, createdAt
# Limit setting username once per minute
DDPRateLimiter.addRule
......
# Extends model Users
RocketChat.models.Users.RocketMailUnsubscribe = (_id, createdAt) ->
console.log '[RocketMailer.Unsubscribe]', _id, createdAt, new Date(parseInt createdAt)
query =
_id: _id
......@@ -11,4 +10,8 @@ RocketChat.models.Users.RocketMailUnsubscribe = (_id, createdAt) ->
$set:
"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