Skip to content

Instantly share code, notes, and snippets.

@woodie
Created January 18, 2010 07:03
Show Gist options
  • Save woodie/279845 to your computer and use it in GitHub Desktop.
Save woodie/279845 to your computer and use it in GitHub Desktop.
Send Mail from AppEngine for JRuby

Send Mail from AppEngine for JRuby

We have a very simple API to send mail from JRuby on AppEngine

appengine-jruby.googlecode.com/hg/rdoc/appengine-apis/classes/AppEngine/Mail.html

I’d like to construct messages with the ‘mail’ gem.

require 'mail'

mail = Mail.new do
      from 'mando.woodie@gmail.com'
        to 'woodie@netpress.com'
   subject 'Here it a test message'
      body 'some text here for testing'
end

mail.delivery_method AppEngine, {:options => :hash}
mail.deliver!

I’d like to be able to plugin a delivery_method for ‘mail’ that uses appengine

github.com/mikel/mail/tree/905fe0d0a3844141ea95a77e216cc8d46d9109e9/lib/mail/network/delivery_methods

The delivery_methods can be similar to SMTP

module Mail
ERROR_MSG = [
'A sender (Return-Path, Sender or From) required to send a message',
'At least one recipient (To, Cc or Bcc) is required to send a message',
'A encoded content is required to send a message'] unless defined? ERROR_MSG
class AppEngine
def initialize(values)
@settings = {}
end
attr_accessor :settings
# AppEngine applications can send email messages on behalf of the
# app’s administrators, and on behalf of users with Google Accounts.
# Apps use the Mail service to send email messages.
def deliver!(mail)
# Set the envelope from to be either the return-path,
# the sender or the first from address
from = mail.return_path || mail.sender || mail.from_addrs.first
raise ArgumentError.new(ERROR_MSG[0]) if from.blank?
message ||= mail.encoded if mail.respond_to?(:encoded)
raise ArgumentError.new(ERROR_MSG[2]) if message.blank?
destinations ||= mail.destinations if
mail.respond_to?(:destinations) && mail.destinations
raise ArgumentError.new(ERROR_MSG[1]) if destinations.blank?
subject = mail.subject
optons = {}
options[:atttachments] = mail.attachments if mail.attachments
options[:bcc] = mail.bcc unless mail.bcc.blank?
options[:cc] = mail.cc unless mail.cc.blank?
options[:html] = mail.parts.first.parts[1].body if mail.parts.size > 1
options[:reply_to] = mail.reply_to
require 'appengine-apis/mail'
if false # TODO: we need some flag to identify mail to admin
AppEngine::Mail.send_to_admins(from, subject, message, options)
else
AppEngine::Mail.send(from, destinations, subject, message, options)
end
self
end
end
end
# Critical default settings:
disable_system_gems
disable_rubygems
bundle_path ".gems/bundler_gems"
# List gems to bundle here:
gem "appengine-rack"
gem "appengine-apis"
gem "activesupport", "3.0.pre"
gem "builder"
gem "i18n"
gem "mail"
# ...
message = Mail.new do
bcc 'mikel@bcc.lindsaar.net'
cc 'mikel@cc.lindsaar.net'
comments 'this is a comment'
date '12 Aug 2009 00:00:01 GMT'
from 'mikel@from.lindsaar.net'
in_reply_to '<1234@in_reply_to.lindsaar.net>'
keywords 'test, "of the new mail", system'
message_id '<1234@message_id.lindsaar.net>'
received 'from machine.example by x.y.test; 12 Aug 2009 00:00:02 GMT'
references '<1234@references.lindsaar.net>'
reply_to 'mikel@reply-to.lindsaar.net'
resent_bcc 'mikel@resent-bcc.lindsaar.net'
resent_cc 'mikel@resent-cc.lindsaar.net'
resent_date '12 Aug 2009 00:00:03 GMT'
resent_from 'mikel@resent-from.lindsaar.net'
resent_message_id '<1234@resent_message_id.lindsaar.net>'
resent_sender 'mikel@resent-sender.lindsaar.net'
resent_to 'mikel@resent-to.lindsaar.net'
sender 'mikel@sender.lindsaar.net'
subject 'Hello there Mikel'
to 'mikel@to.lindsaar.net'
content_type 'text/plain; charset=UTF-8'
content_transfer_encoding '7bit'
content_description 'This is a test'
content_disposition 'attachment; filename=File'
content_id '<1234@message_id.lindsaar.net>'
mime_version '1.0'
body 'This is a body of text'
end
# ...
# example using AppEngine::Mail
class SignupController < Merb::Controller
def confirm(self):
user_address = params[:email_address]
confirmation_url = create_new_user_confirmation
sender_address = "support@example.com"
subject = "Confirm your registration"
body = <<EOM
Thank you for creating an account! Please confirm your email address by
clicking on the link below:
#{confirmation_url}
EOM
AppEngine::Mail.send(sender_address, user_address, subject, body)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment