使用 codeigniter 电子邮件库通过电子邮件发送 ical 邀请

Posted

技术标签:

【中文标题】使用 codeigniter 电子邮件库通过电子邮件发送 ical 邀请【英文标题】:send ical invitation on email using codeigniter email library 【发布时间】:2018-03-18 01:40:24 【问题描述】:

我想将日历邀请发送到电子邮件中。我尝试使用以下代码发送 ical 邀请,但它不起作用。

我正在使用 codeigniter Email Libraray 发送电子邮件。

创建如下所示的 ical 请求

$ical = "BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
BEGIN:VEVENT
UID:" . md5(uniqid(mt_rand(), true)) . "@test.com
DTSTAMP:" . gmdate('Ymd').'T'. gmdate('His') . "Z
DTSTART:19970714T170000Z
DTEND:19970715T035959Z
SUMMARY:New event
END:VEVENT
END:VCALENDAR";

然后我将此内容添加到:

$this->email->set_header('Content-type', 'text/calendar');
$this->email->attach($ical);

但它不起作用。我错过了什么或做错了什么,请建议我。

我试图在电子邮件中得到以下结果

【问题讨论】:

您是否验证了您的 ical?你可以这样做,例如icalendar.org/validator.html 和许多其他人 我验证它是否正确.....在发送 ical 下载选项之后,但我如何显示我在问题中提到的内容(如上图) 【参考方案1】:

这是我的 emails_model 中发送日历事件的 sn-p:

/**
 * @param $event
 *
 * @throws Exception
 */
public function calendar_event($event)

  $this->load->library('email');
  if (empty($event->end_dt))
  
    $event->end_dt = clone $event->start_dt;
    $event->end_dt->modify('+30 minutes');
  
  $location        = $event->location ?? '';
  $organizer       = SITE_NAME;
  $organizer_email = Emails_dto::send_from;
  $cr              = "\n";

  /** @var DateTimeZone $tz */
  $tz      = $event->start_dt->getTimeZone();
  $dt      = new \DateTime (NULL, new DateTimeZone('UTC'));
  $subject = html_entity_decode($event->subject, ENT_QUOTES, 'UTF-8');
  $headers = 'From: ' . SITE_NAME . ' <' . Emails_dto::send_from . '>' . $cr;
  $headers .= "MIME-Version: 1.0$cr";
  $headers .= "Content-Type: text/calendar; method=REQUEST;$cr";
  $headers .= '        charset="UTF-8"' . $cr;
  $headers .= 'Content-Transfer-Encoding: 7bit' . $cr;

  $message    = 'BEGIN:VCALENDAR' . $cr;
  $message    .= 'PRODID:-//i3SoftWebsite//cal_events/NONSGML v1.0//EN' . $cr;
  $message    .= 'VERSION:2.0' . $cr;
  $message    .= 'CALSCALE:GREGORIAN' . $cr;
  $message    .= 'METHOD:REQUEST' . $cr;
  $message    .= 'BEGIN:VEVENT' . $cr;
  $message    .= 'UID:' . md5(uniqid(mt_rand(), TRUE)) . '@' . SITE_DOMAIN_NAME . $cr;
  $message    .= 'CREATED:' . $dt->format("Ymd\THis") . 'Z' . $cr;
  $message    .= 'DTSTAMP:' . gmdate('Ymd') . 'T' . gmdate('His') . 'Z' . $cr;
  $message    .= 'DTSTART;TZID=' . $tz->getName() . ':' . $event->start_dt->format("Ymd\THis") . $cr;
  $message    .= 'DTEND;TZID=' . $tz->getName() . ':' . $event->end_dt->format("Ymd\THis") . $cr;
  $message    .= "SUMMARY:$subject$cr";
  $message    .= "ORGANIZER;CN=$organizer:mailto:$organizer_email$cr";
  $message    .= "LOCATION:$location$cr";
  $message    .= "SEQUENCE:0$cr";
  $message    .= 'DESCRIPTION:' . html_entity_decode($event->description, ENT_QUOTES, 'UTF-8') . $cr;
  $recipients = [$event->email_to];
  if (empty($event->recipients) === FALSE
    && is_array($event->recipients))
  
    foreach ($event->recipients as $recipient)
    
      $message      .= 'ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN=' . $recipient['name'] . ';X-NUM-GUESTS=0:MAILTO:' . $recipient['email'] . $cr;
      $recipients[] = $recipient['email'];

    
  
  $message .= 'END:VEVENT' . $cr;
  $message .= 'END:VCALENDAR' . $cr;
  // @codeCoverageIgnoreStart
  $email_status = ENVIRONMENT === 'production'
    ? (int)mail($event->email_to, $subject, $message, $headers)
    : 0;
  // @codeCoverageIgnoreEnd
  $email                 = new stdClass();
  $email->ema_created_dt = $dt->format(DATETIME_mysql);
  $email->ema_from       = $organizer_email;
  $email->ema_to         = implode(',', $recipients);
  $email->ema_subject    = $subject;
  $email->ema_body       = $message;
  $email->ema_status     = $email_status;
  $email->ema_body       = substr($headers . $message, 0, 1431655760); // trim this to longtext.
  $this->add($email);

【讨论】:

以上是关于使用 codeigniter 电子邮件库通过电子邮件发送 ical 邀请的主要内容,如果未能解决你的问题,请参考以下文章

使用 codeigniter 电子邮件库的主题 > 75 个字符时格式错误的电子邮件主题标头

如何使用 Codeigniter 通过 cc 发送多封电子邮件?

使用 codeigniter 通过 gmail smtp 服务器发送电子邮件

自过去几天以来通过gmail发送电子邮件的问题php codeigniter

将表单文件附加到电子邮件,Codeigniter

通过 Codeigniter 发送带附件的电子邮件时出错