gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 02:18:59 30 4
gpt4 key购买 nike

我想要实现的是以下内容:
发送电子邮件至 delayed_job包含:

  • 纯文本
  • html(将由不理解内联 ical 的普通客户显示)
  • Outlook 和 Thunderbird(使用 Lightning)识别的“内联” ical。
    Thunderbird Accept/Decline/Tentative Buttons
  • “常规”附件(用于#2)

  • 到目前为止什么有效/什么无效:
    我可以通过 delay_job 发送包含所有部分的电子邮件,但是:
  • 在 Apple 的 Mail 2 附件中显示(而不是一个):

  • Apple Mail
    (html显示正常)
  • 在 Thunderbird (Lightning) 中,我确实收到了邀请,就像我想要的那样。但是报警没有出现。

  • Lightning
  • 我必须在渲染的 iCal 上做一些非常恶心的 gsub,以便参加者出现。 (见代码片段)

  • 我的想法:
    首先要记住的是: 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因此将是:
  • 多部分/混合
  • 多部分/替代
  • 文字/普通
  • 文本/html
  • 文本/日历(带有:method=REQUEST)

  • 应用程序/ics


  • 警告!代码传入。
    我目前按以下方式构建此电子邮件:
    编辑:我更新了 Rails 4.2 的邮件程序( attachments 必须放在 mail 之前)
    在我的mailer.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
    这就是我构建 ical 附件的方式:
    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我现在使用的 gem ri_cal .尽管我对此表示怀疑,因为对该 repo 的最后一次提交是在 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:GREGORIAN\nMETHOD:REQUEST\n")

    结尾

    Apples Mail 中的 2 个附件仍然显示,我认为无法修复。

    关于ruby-on-rails - 如何创建多部分电子邮件? [ rails ],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22074917/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com