Class | Yapra::Plugin::Publish::Mail |
In: |
lib-plugins/yapra/plugin/publish/mail.rb
|
Parent: | Yapra::Plugin::Base |
# File lib-plugins/yapra/plugin/publish/mail.rb, line 10 10: def run(data) 11: prepare 12: 13: unless config['mail'] 14: config['mail'] = {} 15: end 16: subject_prefix = config['mail']['subject_prefix'] || '' 17: from = config['mail']['from'] || 'yapra@localhost' 18: to = config['mail']['to'] || 'me@localhost' 19: 20: open_session 21: 22: data.each do |item| 23: date = item.date || item.dc_date || Time.now 24: content = item.content_encoded || item.description || 'from Yapra.' 25: content = [content].pack('m') 26: if config['mail']['from_template'] 27: from = apply_template(config['mail']['from_template'], binding) 28: end 29: if config['mail']['to_template'] 30: to = apply_template(config['mail']['to_template'], binding) 31: end 32: subject = (subject_prefix + item.title).gsub(/\n/, '').chomp 33: logger.debug("try append item: #{subject}") 34: boundary = "----_____====#{Time.now.to_i}--BOUDARY" 35: attachments = create_attachments(item, config) 36: send_item(apply_template(mail_template, binding), 37: {'date' => date, 'from' => from, 'to' => to}) 38: 39: sleep config['wait'] 40: end 41: close_session 42: 43: data 44: end
# File lib-plugins/yapra/plugin/publish/mail.rb, line 63 63: def create_attachments item, config 64: mechanize_file_type = defined?(Mechanize) ? Mechanize::File : WWW::Mechanize::File 65: attachments = [] 66: attachment_attributes = config['mail']['attachments'] 67: if attachment_attributes.kind_of?(String) 68: file = item.__send__(attachment_attributes) 69: attachments << file if file.kind_of?(mechanize_file_type) 70: elsif attachment_attributes.kind_of?(Array) 71: attachment_attributes.each do |atc| 72: file = item.__send__(atc) 73: attachments << file if file.kind_of?(mechanize_file_type) 74: end 75: end 76: attachments 77: end
# File lib-plugins/yapra/plugin/publish/mail.rb, line 55 55: def encode_field field 56: field.gsub(/[^\x01-\x7f]*/) {|x| 57: x.scan(/.{1,10}/).map {|y| 58: "=?UTF-8?B?" + y.to_a.pack('m').chomp + "?=" 59: }.join("\n ") 60: } 61: end
# File lib-plugins/yapra/plugin/publish/mail.rb, line 79 79: def mail_template 80: return "From: <%=encode_field(from) %>\nTo: <%=encode_field(to) %>\nDate: <%=date.rfc2822 %>\nMIME-Version: 1.0\nX-Mailer: Yapra <%=Yapra::VERSION::STRING %>\nSubject: <%=encode_field(subject) %>\nContent-Type: multipart/mixed; boundary=\"<%=boundary -%>\"\n\nThis is a multi-part message in MIME format.\n\n--<%=boundary %>\nContent-type: text/html; charset=UTF-8\nContent-transfer-encoding: base64\n\n<%=content %>\n\n--<%=boundary %>\n<% attachments.each do |file| -%>\nContent-Type: <%=file.header['Content-Type'] %>;\n name=\"<%=encode_field(file.filename) %>\"\nContent-Disposition: attachment;\n filename=\"<%=encode_field(file.filename) %>\"\nContent-Transfer-Encoding: base64\n\n<%=[file.body].pack('m') -%>\n\n--<%=boundary %>\n\n<% end -%>\n" 81: end
# File lib-plugins/yapra/plugin/publish/mail.rb, line 47 47: def prepare 48: config['wait'] = config['wait'] || 1 49: end