EmailSender Library
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: "[email protected]")
mailer.send(to: "[email protected]", subject: "Test mail", content: "This is a test mail.")
Usage
You can specify the connection parameters to SMTP server at initialization:
# simple connection
mailer = EmailSender.new(server: "smtp.mail.com", from: "Sender Name <[email protected]>")
# encripted and authenticated connection
mailer = EmailSender.new(server: "smtp.mail.com", from: "Sender Name <[email protected]>",
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 <[email protected]>", password: "secret")
# with another sender email address and another domain (not gmail.com) account
mailer = EmailSender.new_gmail(from: "Sender Name <[email protected]>",
username: "[email protected]", password: "secret")
You can modify easily the parameters of mailer object:
mailer = EmailSender.new(server: "smtp.mail.com", from: "[email protected]")
# modify parameters
mailer.renew(server: "smtp.mail.com", from: "Sender Name <[email protected]>")
# modify parameters to a Gmail account
mailer.renew_gmail(from: "Sender Name <[email protected]>", password: "secret")
Send the mail to the :to
, :cc
and :bcc
addresses with attachment:
mailer.send(to: "Receiver Name <[email protected]>", 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 <[email protected]>", "Receiver Name 2 <[email protected]>"],
cc: ["Receiver Name 3 <[email protected]>", "Receiver Name 4 <[email protected]>"],
bcc: [[email protected], "[email protected]", "[email protected]"],
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 <[email protected]>",
to: "Receiver Name 1 <[email protected]>",
cc: ["Receiver Name 2 <[email protected]>", "Receiver Name 3 <[email protected]>"])
# 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 <[email protected]>", 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.
Installation
gem install email_sender
Requirements
ruby1.9.1 or greater
License
Copyright (C) 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.