Created
July 5, 2013 15:30
-
-
Save leonardreidy/5935308 to your computer and use it in GitHub Desktop.
A simple ruby script that uses pony.rb to mass mail a list of contacts specified in json format.
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
# | |
# Note that this program does not have any error-handling code, so if it fails, | |
# you will just get some error messages at the command line. It won't skip the offending | |
# email and move on with the task; it will fail entirely. | |
# | |
# Keep an eye on the command prompt periodically, if you don't | |
# know how to write error-handling code. If it does fail, as it occassionally will | |
# due to a server rejecting the email, or something like that, then look in your | |
# Gmail 'sent' folder to identify the last email sent. Then, compare to the list | |
# of contacts. It will almost certainly have failed because of the next email in the list. | |
# In that case, delete all emails before the offending email in your contacts list variable, and | |
# run the program again. This is a crude solution, admittedly, but I haven't finished | |
# coding the error-handling part of the script. | |
# | |
# To test this script as is, assign your Gmail username to the username variable, | |
# your Gmail password to the password variable. Then, add your email and name, | |
# a couple of times to the email list (contacts variable) substituting your email for the email | |
# string and your name for the name string. To avoid throwing an error, add | |
# an attachment too by specifying the path to the file. To keep it easy, save | |
# this file (ponymailer.rb) to the same directory as the attachment, and just give the | |
# name of the file and ponymailer will understand that you mean the current directory. | |
# Then, run this file from the command line with the following command: | |
# ruby ponymailer.rb | |
require 'pony' | |
# mail service username (gmail, for now) | |
username = 'someusername' | |
# mail service password (gmail, for now) | |
password = 'somepassword' | |
# smtp address and port number (gmail for now) | |
smtpaddress = 'smtp.gmail.com' | |
# make sure this is a string | |
port = '587' | |
# to...list of contacts, in json format | |
contacts = | |
[ | |
# contact list should take the following form (excluding the # of course): | |
# ['[email protected]', 'Dr. Contact0'], | |
# ['[email protected]', 'Dr. Contact0'], | |
# ['[email protected]', 'Dr. Contact0'], | |
# ['[email protected]', 'Dr. Contact3'], | |
# ['[email protected]', 'Dr. Contactn'] | |
] | |
# name of sender | |
from = "Ms. Some Sender" | |
# subject of email | |
subject = "Some Subject" | |
# body of email specified as a string | |
body = "The body of the email should proceed from the line beneath the salutation,\n | |
and newlines should be indicated using: \n. Other standard ruby formatting\n | |
rules apply. Please send some test emails to accounts you control first, | |
so that you can make sure the formatting looks reasonable. " | |
# name and path of attachment | |
attachment = "path/to/file.format" | |
# mailing loop | |
contacts.each do |contact| | |
# Create new Mersenne Twister based pseudo-random number generator with seed - | |
# this will be used later to generate a random number of seconds for the script | |
# to sleep between emails | |
rander = Random.new(1234) | |
Pony.mail({ | |
:to => contact[0], | |
# make sure the from field matches the email sender - see :user_name and :password below | |
:from => from, | |
:subject => subject, | |
:body => "Dear " + contact[1] + ",\n\n" + body, | |
:attachments => {attachment => File.binread(attachment)}, | |
:via => :smtp, | |
:via_options => { | |
:address => smtpaddress, | |
:port => port, | |
:enable_starttls_auto => true, | |
:user_name => username, | |
:password => password, | |
:authentication => :plain, # :plain, :login, :cram_md5, no auth by default | |
:domain => "localhost.localdomain", # the HELO domain provided by the client to the server | |
} | |
}) | |
# Sleep the program execution for a random number of seconds between 1 and n, | |
# to give some semblance of human-like behavior | |
sleep(rander.rand(1..74)) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This works. I am now finding a way to attach depending on the test running