加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 如何创建多部分电子邮件? [滑轨]

发布时间:2020-12-17 02:22:11 所属栏目:百科 来源:网络整理
导读:我想要实现的目标如下: 发送电子邮件至delayed_job包含: 纯文本 html(将由不了解内联的常规客户显示) “内联”,由Outlook和Thunderbird(使用Lightning)识别. “常规”附件(#2) 到目前为止有什么用/什么不做: 我可以通过delayed_job发送包含所有部分的电子
我想要实现的目标如下:

发送电子邮件至delayed_job包含:

>纯文本
> html(将由不了解内联的常规客户显示)
>“内联”,由Outlook和Thunderbird(使用Lightning)识别.

>“常规”附件(#2)

到目前为止有什么用/什么不做:

我可以通过delayed_job发送包含所有部分的电子邮件,但是:

>在Apple的Mail 2附件中显示(而不是一个):

(html显示正常)

>在Thunderbird(Lightning)中我收到邀请,就像我想要的那样.但警报没有显示出来.

>我必须在渲染的iCal上做一些非常恶心的gsubs才能让ATTENDEES显示出来. (见代码片段)

我的想法:

首先要记住的是:in order to send an email with attachments from delayed_job

To fix this,remember to add this line to your mailer: content_type “multipart/mixed”

据我所知,correct MIME-Type hierarchy因此是:

> multipart / mixed

> multipart / alternative

> text / plain
> text / html
> text / calendar(with:method = REQUEST)

>申请/ ics

警告!代码传入.

我目前以下列方式构建此电子邮件:

编辑:我更新了邮件的Rails 4.2(附件必须放在邮件之前)

在我的邮件中.rb

def invitation_email(...)  
  subject = "I suck at email..."
  attachments["invite.ics"] = { mime_type: "application/ics",content: ical_attachment }
  email = mail(from: me,to: you,subject: subject)
  add_ical_part_to(email)
  email
end

def add_ical_part_to(mail)
  outlook_body = ical_attachment
  mail.add_part(Mail::Part.new do
    content_type "text/calendar; method=REQUEST"
    body outlook_body
  end)
end

这就是我构建attach附件的方式:

def ical_attachment
  params_participant = {
        "ROLE"   => "REQ-PARTICIPANT","RSVP" => "FALSE","PARTSTAT" => "ACCEPTED"
  }

  params_invite = {
        "CUTYPE" => 'INDIVIDUAL',"ROLE"   => "REQ-PARTICIPANT","PARTSTAT" => "NEEDS-ACTION","RSVP" => "TRUE"
  }

  cal = Icalendar::Calendar.new

  event = Icalendar::Event.new
  event.dtstart      @party.from.to_datetime,{ "VALUE" => "DATE" }
  event.dtend        @party.to.to_datetime,{ "VALUE" => "DATE" }
  event.summary      @party.title
  event.description  @party.description
  event.klass        "PRIVATE"
  event.organizer    "cn=#{@user.name} #{@user.surname}:mailto:#{@user.email}"

    # THIS DOES NOT WORK
  event.alarm.trigger =  "-PT5M" # 5 Minutes before...

  @party.participations.each do |participation|
        str = "cn=#{participation.user.name} #{participation.user.surname}:mailto:#{participation.user.email}"
        event.add_attendee(str,params_participant)
  end

  @party.invitations.each do |invitee|
        event.add_attendee("mailto:#{invitee.email}",params_invite)
  end
  cal.add_event(event)
  cal.publish

  # I KNOW THIS IS HORRIBLE AND I HATE IT,BUT OTHERWISE THE ATTENDEES DO NOT SHOW UP
  cal.to_ical.gsub("ORGANIZER:","ORGANIZER;").gsub("ACCEPTED:","ACCEPTED;").gsub("TRUE:","TRUE;").gsub("PUBLISH","REQUEST")
end

任何帮助将非常感激!

正在生成的电子邮件:http://pastebin.com/patf05zd

哦,我在:

> Rails 3.2.13
> Icalendar gem I’m using

解决方法

如果遇到其他人碰巧碰到这个,我就是这样做的:

而不是icalendar宝石,我现在使用ri_cal.虽然我持怀疑态度,因为最后一次对该回购的承诺是3年前,google group是一个非常有用的资源.

这是我如何生成ical附件(内联和正常),这似乎工作正常(虽然它显然需要一些重构:))

def to_ical
# this is horrible
klass = self

cal = RiCal.Calendar do
  event = event do
    organizer   "CN=#{klass.user.name} #{klass.user.surname}:mailto:#{klass.user.email}"
    summary     klass.party.title

    description klass.ical_description
    dtstart     klass.party.from.utc.to_datetime
    dtend       klass.party.to.utc.to_datetime
    location    "See url in description"
    security_class klass.security_class

    # this is horrible
    h = self

    klass.party.participations.each do |participation|
      h.add_attendee klass.prepare_participant(participation)
    end

    klass.party.invitations.each do |invitee|
      h.add_attendee klass.prepare_invitee(invitee.email)
    end

    unless klass.party.reminder == 0
      alarm do
        description "Alarm description"
        trigger klass.convert_trigger # -PT1H
        action "DISPLAY"
      end
    end
  end
end

# THE HORROR
cal.to_s.gsub("ATTENDEE:","ATTENDEE")
        .gsub("ORGANIZER:","ORGANIZER;")
        .gsub("CALSCALE:GREGORIAN","CALSCALE:GREGORIANnMETHOD:REQUESTn")

结束

苹果邮件中的2个附件仍然显示,我不认为可以修复.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读