Skip to content

Instantly share code, notes, and snippets.

@jackyq2015
Forked from jugyo/index.js
Created August 19, 2018 15:22
Show Gist options
  • Save jackyq2015/0f42fbf6b4b99808e6f694e5cabbd45e to your computer and use it in GitHub Desktop.
Save jackyq2015/0f42fbf6b4b99808e6f694e5cabbd45e to your computer and use it in GitHub Desktop.
An example to send email with Cloud Pub Sub and Cloud Functions #Rails
const sendEmail = require('./sendEmail').sendEmail;
/**
* Deplooyment:
*
* $ gcloud beta functions deploy sendEmail --trigger-topic sendEmail
*
*/
/**
* Triggered from a message on a Cloud Pub/Sub topic.
*
* @param {!Object} event The Cloud Functions event.
* @param {!Function} The callback function.
*/
exports.sendEmail = (event, callback) => {
// The Cloud Pub/Sub Message object.
const pubsubMessage = event.data;
// We're just going to log the message to prove that
// it worked.
const decoded = Buffer.from(pubsubMessage.data, 'base64').toString();
console.log(decoded);
const data = JSON.parse(decoded);
sendEmail(data);
// Don't forget to call the callback.
callback();
};
require "google/cloud/pubsub"
#
# Configuration:
#
# environments/production.rb:
#
# Rails.application.configure do
# ...
# config.action_mailer.delivery_method = :pub_sub_mailer
# end
#
# initializers/pub_sub_mailer.rb:
#
# PubSubMailer.config = {
# project_id: ENV['GCLOUD_PROJECT_ID'],
# credentials: Rails.root.join('service-account-credentials.json')
# }
#
class PubSubMailer
class << self
attr_accessor :config
end
def initialize(options = {})
@pubsub = Google::Cloud::Pubsub.new(
project_id: self.class.config[:project_id],
credentials: self.class.config[:credentials]
)
end
def deliver!(mail)
data = {
to: Array(mail['to']).join(', '),
from: Array(mail['from']).join(', '),
cc: Array(mail['cc']).join(', '),
bcc: Array(mail['bcc']).join(', '),
reply_to: Array(mail['reply_to']).join(', '),
subject: mail.subject,
html: mail.decoded.to_s, # NOTE: Now only html is supported
}
@pubsub.topic('sendEmail').publish(data.to_json)
Rails.logger.debug(data)
rescue => e
# TODO: Notify the error
Rails.logger.error(e.message)
end
end
ActiveSupport.on_load :action_mailer do
ActionMailer::Base.add_delivery_method(
:pub_sub_mailer,
PubSubMailer
)
end
const nodemailer = require('nodemailer');
const smtpConfig = require('./smtp-config').config;
/**
* An example of the args
*
* { to: 'bob@example.com',
* from: 'example <noreply@example.com>',
* cc: '',
* bcc: '',
* reply_to: '',
* subject: 'Alice has sent a message to you!',
* text: 'text...',
* html: '<!DOCTYPE html>...' }
*/
exports.sendEmail = (mailOptions) => {
console.log('mailOptions', mailOptions);
var transporter = nodemailer.createTransport(smtpConfig);
transporter.sendMail(mailOptions, function(error, info){
if(error){
return console.log(error);
}
console.log('Message sent: ' + info.response);
});
}
exports.config = {
host: '',
port: 587,
secure: false,
requireTLS: true,
auth: {
user: 'user',
pass: 'pass'
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment