EmailSender is an easy to use library to send email based on Net::SMTP. It supports the well-known encryption and authentication methods, and you can use it very easily with Gmail account.
require 'email_sender' mailer = EmailSender.new(server: "smtp.mail.com", from: "sender@mail.com") mailer.send(to: "receiver@mail.com", subject: "Test mail", content: "This is a test mail.")
You can specify the connection parameters to SMTP server at initialization:
# simple connection mailer = EmailSender.new(server: "smtp.mail.com", from: "Sender Name <sender@mail.com>") # encripted and authenticated connection mailer = EmailSender.new(server: "smtp.mail.com", from: "Sender Name <sender@mail.com>", enctype: :ssl_tls, authtype: :plain, username: "user.name", password: "secret")
Easier way to using Gmail account:
# minimal configuration mailer = EmailSender.new_gmail(username: "user.name", password: "secret") mailer = EmailSender.new_gmail(from: "user.name", password: "secret") # with full sender name mailer = EmailSender.new_gmail(from: "Sender Name <user.name@gmail.com>", password: "secret") # with another sender email address and another domain (not gmail.com) account mailer = EmailSender.new_gmail(from: "Sender Name <sender@othermail.com>", username: "user.name@company.com", password: "secret")
You can modify easily the parameters of mailer object:
mailer = EmailSender.new(server: "smtp.mail.com", from: "sender@mail.com") # modify parameters mailer.renew(server: "smtp.mail.com", from: "Sender Name <sender@mail.com>") # modify parameters to a Gmail account mailer.renew_gmail(from: "Sender Name <user.name@gmail.com>", password: "secret")
Send the mail to the :to
, :cc
and :bcc
addresses with attachment:
mailer.send(to: "Receiver Name <receiver@mail.com>", subject: "Test mail", content: "This is a test mail.", attachment: "/path/to/file")
The :to
, :cc
and :bcc
keys accept an email address array so you can send the message to many receivers. And the :attachment
key accepts also file path array so you can attach more file:
mailer.send(to: ["Receiver Name 1 <receiver1@mail.com>", "Receiver Name 2 <receiver2@mail.com>"], cc: ["Receiver Name 3 <receiver3@mail.com>", "Receiver Name 4 <receiver4@mail.com>"], bcc: [receiver5@mail.com, "receiver6@mail.com", "receiver7@mail.com"], subject: "Test mail", content: "This is a test mail.", attachment: ["/path/to/file1", "/path/to/file2", "/path/to/file3"])
If there are not specified the :to
, :cc
and :bcc
addresses on sending use the initialized default addresses:
# create a mailer with default addresses mailer = EmailSender.new(server: "smtp.mail.com", from: "Sender Name <user.name@gmail.com>", to: "Receiver Name 1 <receiver1@mail.com>", cc: ["Receiver Name 2 <receiver2@mail.com>", "Receiver Name 3 <receiver3@mail.com>"]) # send email to the default addresses mailer.send(subject: "Test mail", content: "This is a test mail.")
You can specify the content type which is +‘text/plain’+ by default:
mailer.send(to: "Receiver Name <receiver@mail.com>", subject: "The Ruby programming language", content: "<img src='http://upload.wikimedia.org/wikipedia/commons/7/73/Ruby_logo.svg'/>", conttype: "text/html")
The library has implemented threadsafe and support the character encoded messages.
gem install email_sender
ruby1.9.1 or greater
Copyright © 2012 Peter Bakonyi
EmailSender is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License (LGPL) version 3.0 as published by the Free Software Foundation.