Emarsys::Broadcast
Ruby library for Emarsys batch mailing API
Installation
Add this line to your application's Gemfile:
gem 'emarsys-broadcast'
And then execute:
$ bundle
Or install it yourself as:
$ gem install emarsys-broadcast
Usage
Complete sending example:
Minimal configuration is required before usage
# if using rails, place this configuration code into initializer
Emarsys::Broadcast::configure do |c|
c.api_user = your_api_user
c.api_password = your_api_password
c.sftp_user = your_sftp_user
c.sftp_password = your_sftp_password
c.sender_domain = 'mail.your.company.com'
end
# create a batch that you want to send
batch = Emarsys::Broadcast::Batch.new
batch.sender = 'news@company.com'
batch.name = 'newsletter_2013_06_01'
batch.subject = 'June 2013 company news'
batch.body_html = '<h1>Dear 朋友!</h1>'
batch.recipients_path = '/path/to/your/csv/with/emails.csv'
# create API client
api = Emarsys::Broadcast::API.new
# now send your batch
api.send_batch(batch)
This will synchronously send the batch email to all recipients found in CSV file.
Creating batch from hash
If you like, you can construct your batch from a hash, like follows:
batch = Emarsys::Broadcast::Batch.new name: 'name', subject: 'subject', body_html: '<h1>html body</h1>'
Required batch attributes
Batch#name
Batch name must be a valid identifier, i.e. start with a letter and contain letters, digits and underscores. Emarsys requires every batch to have a unique name, but you don't have to maintain the uniqueness, because this library internally appends a timestamp to each batch name before submitting it to Emarsys.
Batch#subject
Batch subject must be a string with a maximum length of 255 characters
Batch#sender
Valid email, registed with Emarsys.
Emarsys maintains a list of allowed sender emails, and restricts sending emails from arbitrary email. If you want to use an email as a sender, refer to working with senders
Can be set once via configuration
Batch#body_html
Batch body html can be any HTML text, no restrictions
Batch#recipients_path
Absolute path to CSV file with recipients data.
Can be set once via configuration
Optional batch attributes
Batch#body_text
It is possible to supply the body contents in plain text, this will broaden compatibility, because some email clients don't support HTML and will download textual version instead.
batch = Emarsys::Broadcast::Batch.new
batch.name = 'newsletter_2013_06_01'
batch.subject = 'June 2013 company news'
batch.body_html = '<h1>Dear 朋友!</h1>'
batch.body_text = 'Dear 朋友'
Batch#send_time
You can set the batch send time using this optional attribute. If you don't set it it will be scheduled for immediate sending.
Batch.import_delay_hours
Time in hours which is allowed for import to be delayed after batch import. Defaults to 1 hour.
Can be set once via configuration
Moving batch properties to configuration
If you find yourself using same batch attributes over and over again, you can move these attributes into configuration:
Emarsys::Broadcast::configure do |c|
c.api_user = your_api_user
c.api_password = your_api_password
c.sftp_user = your_sftp_user
c.sftp_password = your_sftp_password
c.sender = 'sender_id'
c.sender_domain = 'mail.your.company.com'
c.recipients_path = '/path/to/hyour/csv/with/emails'
end
# now you can omit these attributes when constructing a batch:
batch = Emarsys::Broadcast::Batch.new
batch.name = 'newsletter_2013_06_01'
batch.subject = 'June 2013 company news'
batch.body_html = '<h1>Dear 朋友!</h1>'
# send your batch as above, via api
List of configurable attributes
- sender (required attribute)
- sender_domain (required attribute)
- recipients_path (required attribute)
- import_delay_hours (optional attribute) see more here
Batch validation
Emarsys::Broadcast
uses ActiveModel
for validating plain ruby objects, so you have all the methods for
validation you're accustomed to:
batch = Emarsys::Broadcast::Batch.new
batch.name = 'newsletter_2013_06_01'
batch.valid? # false
batch.errors
batch.errors.full_messages
You can always validate your batch before submitting it.
Note that calling API#send_batch
on an invalid batch will throw ValidationException
batch = get_invalid_batch
api = Emarsys::Broadcast::API.new
begin
api.send_batch batch
rescue Emarsys::Broadcast::ValidationException => ex
# get exception message
puts ex.message
# get exception errors (ActiveModel compatible)
puts ex.errors
end
Scheduling batches
By default a new batch is scheduled for immediate sending, but you can set the Batch#send_time
# Assuming using ActiveSupport and want to schedule a batch to be sent in 10 days
batch.send_time = Time.zone.now + 10.days
# .. more attributes
api.send_batch batch
CSV file requirements
The recipients must be placed in a UTF-8
CSV file.
The file must have at least one column with EMAIL
header, for example:
EMAIL
john.doe@gmail.com
sara.parker@yahoo.com
...
...
If you use additional customization columns, add them to your CSV file:
EMAIL FIRST_NAME
john.doe@gmail.com John
sara.parker@yahoo.com Sara
...
...
Having additional columns allows you to customize your body_html, for example:
<h1>Hi, $$FIRST_NAME$$</h1>
Working with senders
Adding a new sender
Adding a sender is required before you can start using its email address as a Batch#sender
# assuming you have API configured already
api = Emarsys::Broadcast::API.new
# sender requires 3 arguments: id, name, email_address
sender = Emarsys::Broadcast::Sender.new('primary_newsletter_sender', 'My company', 'news@company.com')
api.create_sender sender
Once you add a sender, you can use its email in any batch:
batch.sender = 'news@company.com'
# more attributes
Getting a full list of senders
api.get_senders
# returns array of `Emarsys::Broadcast::Sender`
Getting a single sender by email
api.get_sender('news@mycompany.ru')
Finding if a sender exists by email
api.sender_exists? 'news@mycompany.ru'
Compatibility
This gem is tested on
- MRI
2.1.2
- JRuby 1.9 mode
Further plans
This library does not yet cover all Emarsys functionality, so the plans are to cover 100% of Emarsys features, add async support, more scheduling options etc.
If you want to help me with this, pull requests are especially welcome :)
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Run specs (
rspec
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
License
Copyright (c) 2013 Valentin Vasilyev
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.