Mailing Files Programmatically with GMail

Posted on July 25, 2012. Filed under: Uncategorized |

Recently I had the need to send someone multiple 1-2 MB files. Email was the best option, for reasons that are not very interesting, and outside the scope of this article.

I naturally turned to my favorite language, Ruby.  I did a little research into the various gems and their configuration, and offer this simple example (source code below) in the hope that it will save you a little time if you have the same need.

I’ve tried to trim out anything not relating to the actual sending of the mail so that you can more easily understand it and adapt it for your use.

– Keith

#!/usr/bin/env ruby
# Mails a file using GMail's SMTP Server.
# For illustrative purposes; error checking and testing intentionally omitted for brevity.
#
# Requirements: 
#   1) the 'mail' gem must be installed
#   2) a file named 'pw.txt' containing the Google password must be present
#      in the current directory.
#
# Ruby versions: tested on 1.9.3, 1.8.7, JRuby
#
# Keith Bennett

require 'rubygems' # Need this for Ruby 1.8
require 'mail'


ATTACHMENT_FILESPEC = 'attachment.txt'
File.open(ATTACHMENT_FILESPEC, 'w') do |file|
  file << "Sample attachment text file, sent #{Time.now}\n"
end


# Used for GMail authentication and the 'from' header field.
def my_gmail_address
  # Return your gmail address, e.g.:
  'me@gmail.com'
end


# Used for 'to' header field
def recipients
  # a single address or a comma separated list of email addresses, e.g.:
  'mom@aol.com,fred@people.com'
end


# GMail password for SMTP authentication.
# Create a file named 'pw.txt' and put your password there in plain text.
# Not the best way to do this!
def password
  File.read('pw.txt')
end


def body_text
  "This message contains the following attachment: #{ATTACHMENT_FILESPEC}.\nSent #{Time.now}"
end


def subject_text
  "Gmail-It Attached File: #{ATTACHMENT_FILESPEC}"
end


# This information could probably be put in the Mail.deliver block below,
# but it's here in case multiple mail calls are made.
Mail.defaults do
  delivery_method :smtp, { 
    :address              => "smtp.gmail.com",
    :port                 => 587,
    :domain               => 'gmail.com',
    :user_name            => my_gmail_address,
    :password             => password,
    :enable_starttls_auto => true  }
end
  
puts "Delivering file: #{ATTACHMENT_FILESPEC} to #{recipients}."

Mail.deliver do
  from     my_gmail_address
  to       recipients
  subject  subject_text
  body     body_text
  add_file ATTACHMENT_FILESPEC
end

puts "File #{ATTACHMENT_FILESPEC} sent."
puts "Check recipient email account(s) (#{recipients}) to verify success."  
Advertisements

Make a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Liked it here?
Why not try sites on the blogroll...

%d bloggers like this: