Class: Postal::Mailing

Inherits:
Base
  • Object
show all
Defined in:
lib/postal/mailing.rb

Constant Summary collapse

DEFAULT_ATTRIBUTES =
{:additional_headers => nil, 
:attachments => nil, 
:bypass_moderation => nil, 
:campaign => nil, 
:char_set_id => nil, 
:detect_html => nil, 
:dont_attempt_after_date => nil, 
:enable_recovery => nil, 
:from => nil, 
:html_message => nil, 
:html_section_encoding => nil, 
:is_html_section_encoded => nil, 
:is_text_section_encoded => nil,
:list_name => nil,
:recency_number_of_mailings => nil,
:recency_which => nil,
:reply_to => nil,
:resend_after_days => nil,
:sample_size => nil,
:scheduled_mailing_date => nil,
:subject => nil,
:text_message => nil,
:text_section_encoding => nil,
:title => nil,
:to => nil,
:track_opens => nil,
:rewrite_date_when_sent => nil,
:mailing => nil }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

all, create, create!, find

Constructor Details

#initialize(args = {}) ⇒ Mailing

Create a new mailing ready to send



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/postal/mailing.rb', line 73

def initialize(args={})
  args = DEFAULT_ATTRIBUTES.merge(args)
  
  @list_name = args[:list_name] || Postal.options[:list_name]
  @to = args[:to] 
  
  if args[:mailing].nil?
    @additional_headers = args[:additional_headers] 
    @attachments = args[:attachments] 
    @bypass_moderation = args[:bypass_moderation] 
    @campaign = args[:campaign] 
    @char_set_id = args[:char_set_id] 
    @detect_html = args[:detect_html] 
    @dont_attempt_after_date = args[:dont_attempt_after_date] 
    @enable_recovery = args[:enable_recovery] 
    @from = args[:from] 
    @html_message = args[:html_message] 
    @html_section_encoding = args[:html_section_encoding] 
    @is_html_section_encoded = args[:is_html_section_encoded] 
    @is_text_section_encoded = args[:is_text_section_encoded]
    @recency_number_of_mailings = args[:recency_number_of_mailings]
    @recency_which = args[:recency_which]
    @reply_to = args[:reply_to]
    @resend_after_days = args[:resend_after_days]
    @sample_size = args[:sample_size]
    @scheduled_mailing_date = args[:scheduled_mailing_date]
    @subject = args[:subject]
    @text_message = args[:text_message]
    @text_section_encoding = args[:text_section_encoding]
    @title = args[:title]
    @to = args[:to]
    @track_opens = args[:track_opens]
    @rewrite_date_when_sent = args[:rewrite_date_when_sent]
    @mailing = args[:mailing]
  else
    @subject = args[:mailing].subject
    @is_html_section_encoded = args[:mailing].is_html_section_encoded
    @html_section_encoding = args[:mailing].html_section_encoding
    @html_message = args[:mailing].html_message
    @char_set_id = args[:mailing].char_set_id
    @is_text_section_encoded = args[:mailing].is_text_section_encoded
    @text_section_encoding = args[:mailing].text_section_encoding
    @title = args[:mailing].title
    @text_message = args[:mailing].text_message
    @attachments = args[:mailing].attachments
    @from = args[:mailing].from
    @additional_headers = args[:mailing].additional_headers
    @mailing = args[:mailing]
  end
end

Instance Attribute Details

#additional_headersObject

Returns the value of attribute additional_headers.



43
44
45
# File 'lib/postal/mailing.rb', line 43

def additional_headers
  @additional_headers
end

#attachmentsObject

Returns the value of attribute attachments.



43
44
45
# File 'lib/postal/mailing.rb', line 43

def attachments
  @attachments
end

#bypass_moderationObject

Returns the value of attribute bypass_moderation.



43
44
45
# File 'lib/postal/mailing.rb', line 43

def bypass_moderation
  @bypass_moderation
end

#campaignObject

Returns the value of attribute campaign.



43
44
45
# File 'lib/postal/mailing.rb', line 43

def campaign
  @campaign
end

#char_set_idObject

Returns the value of attribute char_set_id.



43
44
45
# File 'lib/postal/mailing.rb', line 43

def char_set_id
  @char_set_id
end

#detect_htmlObject

Returns the value of attribute detect_html.



43
44
45
# File 'lib/postal/mailing.rb', line 43

def detect_html
  @detect_html
end

#dont_attempt_after_dateObject

Returns the value of attribute dont_attempt_after_date.



43
44
45
# File 'lib/postal/mailing.rb', line 43

def dont_attempt_after_date
  @dont_attempt_after_date
end

#enable_recoveryObject

Returns the value of attribute enable_recovery.



43
44
45
# File 'lib/postal/mailing.rb', line 43

def enable_recovery
  @enable_recovery
end

#fromObject

Returns the value of attribute from.



43
44
45
# File 'lib/postal/mailing.rb', line 43

def from
  @from
end

#html_messageObject

Returns the value of attribute html_message.



43
44
45
# File 'lib/postal/mailing.rb', line 43

def html_message
  @html_message
end

#html_section_encodingObject

Returns the value of attribute html_section_encoding.



43
44
45
# File 'lib/postal/mailing.rb', line 43

def html_section_encoding
  @html_section_encoding
end

#is_html_section_encodedObject

Returns the value of attribute is_html_section_encoded.



43
44
45
# File 'lib/postal/mailing.rb', line 43

def is_html_section_encoded
  @is_html_section_encoded
end

#is_text_section_encodedObject

Returns the value of attribute is_text_section_encoded.



43
44
45
# File 'lib/postal/mailing.rb', line 43

def is_text_section_encoded
  @is_text_section_encoded
end

#list_nameObject

Returns the value of attribute list_name.



43
44
45
# File 'lib/postal/mailing.rb', line 43

def list_name
  @list_name
end

#mailingObject

Returns the value of attribute mailing.



43
44
45
# File 'lib/postal/mailing.rb', line 43

def mailing
  @mailing
end

#recency_number_of_mailingsObject

Returns the value of attribute recency_number_of_mailings.



43
44
45
# File 'lib/postal/mailing.rb', line 43

def recency_number_of_mailings
  @recency_number_of_mailings
end

#recency_whichObject

Returns the value of attribute recency_which.



43
44
45
# File 'lib/postal/mailing.rb', line 43

def recency_which
  @recency_which
end

#reply_toObject

Returns the value of attribute reply_to.



43
44
45
# File 'lib/postal/mailing.rb', line 43

def reply_to
  @reply_to
end

#resend_after_daysObject

Returns the value of attribute resend_after_days.



43
44
45
# File 'lib/postal/mailing.rb', line 43

def resend_after_days
  @resend_after_days
end

#rewrite_date_when_sentObject

Returns the value of attribute rewrite_date_when_sent.



43
44
45
# File 'lib/postal/mailing.rb', line 43

def rewrite_date_when_sent
  @rewrite_date_when_sent
end

#sample_sizeObject

Returns the value of attribute sample_size.



43
44
45
# File 'lib/postal/mailing.rb', line 43

def sample_size
  @sample_size
end

#scheduled_mailing_dateObject

Returns the value of attribute scheduled_mailing_date.



43
44
45
# File 'lib/postal/mailing.rb', line 43

def scheduled_mailing_date
  @scheduled_mailing_date
end

#subjectObject

Returns the value of attribute subject.



43
44
45
# File 'lib/postal/mailing.rb', line 43

def subject
  @subject
end

#text_messageObject

Returns the value of attribute text_message.



43
44
45
# File 'lib/postal/mailing.rb', line 43

def text_message
  @text_message
end

#text_section_encodingObject

Returns the value of attribute text_section_encoding.



43
44
45
# File 'lib/postal/mailing.rb', line 43

def text_section_encoding
  @text_section_encoding
end

#titleObject

Returns the value of attribute title.



43
44
45
# File 'lib/postal/mailing.rb', line 43

def title
  @title
end

#toObject

Returns the value of attribute to.



43
44
45
# File 'lib/postal/mailing.rb', line 43

def to
  @to
end

#track_opensObject

Returns the value of attribute track_opens.



43
44
45
# File 'lib/postal/mailing.rb', line 43

def track_opens
  @track_opens
end

Class Method Details

.import(content_id) ⇒ Object



6
7
8
9
# File 'lib/postal/mailing.rb', line 6

def import(content_id)
  mail = Mailing.new(Postal.driver.importContent(content_id))
  return mail
end

Instance Method Details

#sendObject Also known as: save

Send the mailing



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/postal/mailing.rb', line 126

def send
  if valid?
    
    # are we sending to a list of email addresses or member ids
    case [@to].flatten.first
    when ::String
      emails = [@to].flatten
      member_ids = []
    when ::Fixnum
      emails = []
      member_ids = [@to].flatten
    end
    
    if @mailing.nil?
      mail = { 'AdditionalHeaders' => @additional_headers, 
               'Attachments' => @attachments, 
               'BypassModeration' => @bypass_moderation, 
               'Campaign' => @campaign, 
               'CharSetID' => @char_set_id, 
               'DetectHtml' => @detect_html, 
               'DontAttemptAfterDate' => @dont_attempt_after_date, 
               'EnableRecovery' => @enable_recovery, 
               'From' => @from, 
               'HtmlMessage' => @html_message, 
               'HtmlSectionEncoding' => @html_section_encoding, 
               'IsHtmlSectionEncoded' => @is_html_section_encoded, 
               'IsTextSectionEncoded' => @is_text_section_encoded,
               'ListName' => @list_name,
               'RecencyNumberOfMailings' => @recency_number_of_mailings,
               'RecencyWhich' => @recency_which,
               'ReplyTo' => @reply_to,
               'ResendAfterDays' => @resend_after_days,
               'SampleSize' => @sample_size,
               'ScheduledMailingDate' => @scheduled_mailing_date,
               'Subject' => @subject,
               'TextMessage' => @text_message,
               'TextSectionEncoding' => @text_section_encoding,
               'Title' => @title,
               # :to => @to,
               'TrackOpens' => @track_opens,
               'RewriteDateWhenSent' => @rewrite_date_when_sent }
    
      return Postal.driver.sendMailingDirect(emails,member_ids,mail)
    else
      mail = { 'AdditionalHeaders' => @mailing.additional_headers, 
               'Attachments' => @mailing.attachments, 
               'BypassModeration' => @mailing.bypass_moderation, 
               'Campaign' => @mailing.campaign, 
               'CharSetID' => @mailing.char_set_id, 
               'DetectHtml' => @mailing.detect_html, 
               'DontAttemptAfterDate' => @mailing.dont_attempt_after_date, 
               'EnableRecovery' => @mailing.enable_recovery, 
               'From' => @mailing.from, 
               'HtmlMessage' => @mailing.html_message, 
               'HtmlSectionEncoding' => @mailing.html_section_encoding, 
               'IsHtmlSectionEncoded' => @mailing.is_html_section_encoded, 
               'IsTextSectionEncoded' => @mailing.is_text_section_encoded,
               'RecencyNumberOfMailings' => @mailing.recency_number_of_mailings,
               'RecencyWhich' => @mailing.recency_which,
               'ReplyTo' => @mailing.reply_to,
               'ResendAfterDays' => @mailing.resend_after_days,
               'SampleSize' => @mailing.sample_size,
               'ScheduledMailingDate' => @mailing.scheduled_mailing_date,
               'Subject' => @mailing.subject,
               'TextMessage' => @mailing.text_message,
               'TextSectionEncoding' => @mailing.text_section_encoding,
               'Title' => @mailing.title,
               # :to => @to,
               'TrackOpens' => @mailing.track_opens,
               'RewriteDateWhenSent' => @mailing.rewrite_date_when_sent,
               'ListName' => @mailing.list_name || Postal.options[:list_name] }
      return Postal.driver.sendMailingDirect(emails,member_ids,mail)
    end
    
  else  # mail wasn't valid
    return false
  end
end

#send!Object Also known as: save!

same as send() but throws an error instead of just returning false



207
208
209
210
211
212
213
# File 'lib/postal/mailing.rb', line 207

def send!
  if id = send
    return id
  else
    raise Postal::CouldNotSendMailing, 'Your mail was invalid and could not be sent.'
  end
end

#valid?Boolean

Determines whether the email is valid to send

Returns:

  • (Boolean)


220
221
222
# File 'lib/postal/mailing.rb', line 220

def valid?
  return validate
end

#validateObject

Determines whether we have everything we need to send an email



226
227
228
# File 'lib/postal/mailing.rb', line 226

def validate
  return (@list_name && @to && @subject) ? true : false
end