26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/maropost_api/transactional_campaigns.rb', line 26
def send_email(
campaign_id:,
content: {},
contact: {},
send_time: {},
ignore_dnm: nil,
bcc: nil,
from_name: nil,
from_email: nil,
subject: nil,
reply_to: nil,
address: nil,
tags: {},
add_ctags: []
)
email_regex = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
raise ArgumentError.new('Content must be a type of Integer (content_id) or a Hash (content field values)') until content.kind_of? Hash or content.kind_of? Integer
if (content.kind_of? Hash)
content = content.slice(:name, :html_part, :text_part)
raise ArgumentError.new('Content field values must have all or some of :name, :html_part and :text_part as keys') until content.empty? || content.count > 0
end
raise ArgumentError.new('Contact must be a type of Integer (contact_id) or a Hash (contact field values)') until contact.kind_of? Hash or contact.kind_of? Integer
if (contact.kind_of? Hash)
contact = contact.slice(:email, :first_name, :last_name, :custom_field)
raise ArgumentError.new('Contact field values must have :email and any or all of :first_name and :last_name :custom_field as keys') if contact.empty? || contact[:email].nil?
raise ArgumentError.new("contact[:custom_field] must be a type of Hash - #{contact[:custom_field].class} given!") until contact[:custom_field].is_a?(Hash) || contact[:custom_field].nil?
end
raise ArgumentError.new('contact[:email] must be a valid email address') until contact.is_a? Integer or contact[:email].match? email_regex
raise ArgumentError.new('bcc must be a valid email address') until bcc.nil? or bcc.match? email_regex
raise ArgumentError.new('from_email must be a valid email address') until from_email.nil? or from_email.match? email_regex
raise ArgumentError.new('reply_to must be a valid email address') until reply_to.nil? or reply_to.match? email_regex
raise ArgumentError.new('add_ctags must be a valid Array of string') until add_ctags.is_a? Array and add_ctags.all?{|t| t.is_a? String }
raise ArgumentError.new('send_time must have two keys viz. :hour, :minute') until send_time.has_key?(:hour) and send_time.has_key?(:minute)
raise ArgumentError.new('send_time[:hour] must be between 1-12 and send_time[:minute] must be between 0-59') until (1..12).to_a.include?(send_time[:hour]) and (0..59).to_a.include?(send_time[:minute])
params = {}
method(__method__).parameters.each{|p| params[p[1]] = eval(p[1].to_s)}
params.reject!{|k,v| v.nil? or (v.respond_to? :empty? and v.empty?) }
params[:content_id] = params.delete(:content) if params[:content].kind_of? Integer
params[:contact_id] = params.delete(:contact) if params[:contact].kind_of? Integer
full_path = full_resource_path('/deliver', 'emails')
MaropostApi.post_result(full_path, :email => params)
end
|