Last active
November 26, 2020 04:34
-
-
Save jugyo/1f9902d7e4c4dba07b1ab762211ad83c to your computer and use it in GitHub Desktop.
An example to send email with Cloud Pub Sub and Cloud Functions #Rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const nodemailer = require('nodemailer'); | |
const smtpConfig = require('./smtp-config').config; | |
/** | |
* An example of the args | |
* | |
* { to: '[email protected]', | |
* from: 'example <[email protected]>', | |
* 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); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Facing the same issue today. Anyone found the root cause?