【问题标题】:Yii, PHPMailer : how to send an email with HTML and ICS formatYii, PHPMailer : 如何发送 HTML 和 ICS 格式的电子邮件
【发布时间】:2015-07-14 10:49:58
【问题描述】:

我正在尝试将 HTML 和 Ics 格式的电子邮件发送到 gmail、outlook、yahoo、thunderbird、outlook express 2013 和 iphone,但我只能将 ics 格式发送到 gmail、outlook 和 yahoo。

问题我无法在同一邮件中发送 html 格式
我尝试了以下代码

public function createScheduleNotification(){

    $from = 'info@careervita.com';
    $fromName = 'info';
    $to = 'testcareervita@outlook.com';
    $subject = 'Invitation for Test Demo';
    $desc = 'THIS IS TEST DEMO INVITAION';
    $uid =  date('Ymd').'T'.date('His')."-".rand();

    $message =  "BEGIN:VCALENDAR\r\n";
    $message.=  "PRODID:-//Zoho CRM//NONSGML Calendar//EN\r\n";
    $message.=  "VERSION:2.0\r\n";
    $message.=  "X-WR-TIMEZONE:GMT\r\n";
    $message.=  "X-WR-CALNAME:ZCRM\r\n";
    $message.=  "METHOD:REQUEST\r\n";
    $message.=  "CALSCALE:GREGORIAN\r\n";
    $message.=  "BEGIN:VEVENT\r\n";
    $message.=  "UID:".$uid."\r\n";
    $message.=  "DESCRIPTION:".$desc."\r\n";
    $message.=  "ORGANIZER;CN= Manisha:mailto:manisha.g@careervita.com\r\n";
    $message.=  "DTSTART:20150710T100010Z\r\n";
    $message.=  "DTEND:20150710T110000Z\r\n";
    $message.=  "ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;CN=Vinod T:mailto:g_manisha@outlook.com\r\n";
    $message.=  "ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;CN=Vivek J:mailto:manisha.gaidhane8788@gmail.com\r\n";
    $message.=  "LOCATION:CV360\r\n";
    $message.=  "CREATED:20150701T060720Z\r\n";
    $message.=  "LAST-MODIFIED:20150701T060720Z\r\n";
    $message.=  "X-EVENT-OWNER:1563791000000084003\r\n";
    $message.=  "END:VEVENT\r\n";
    $message.=  "END:VCALENDAR";

    Yii::import('application.extensions.phpmailer.JPhpMailer');
        $mailer = new JPhpMailer(true);
        if (Yum::module()->phpmailer['transport'])
            switch (Yum::module()->phpmailer['transport']){
                case 'smtp':
                    $mailer->IsSMTP();
                    break;
                case 'sendmail':
                    $mailer->IsSendmail();
                    break;
                case 'qmail':
                    $mailer->IsQmail();
                    break;
                case 'mail':
                default:
                    $mailer->IsMail();
            }
        else
            $mailer->IsMail();
            $mailer->IsHTML(true);

        $mailerconf=Yum::module()->phpmailer['properties'];
        if(is_array($mailerconf))
            foreach($mailerconf as $key=>$value) {
                if(isset(JPhpMailer::${$key}))
                    JPhpMailer::${$key} = $value;
                else
                    $mailer->$key=$value;
            }

        $mailer->SetFrom($from); 
        $mailer->AddAddress($to); 
        $mailer->Subject = $subject;
        $mailer->Body = $message;
        $mailer->AddCC('testcareervita@gmail.com', 'Person One');
        $mailer->AddCC('testcareervita@yahoo.in', 'Person Two');

        $mailer->ContentType = 'text/calendar';
        $mailer->CharSet = 'UTF-8';
        $mailer->addCustomHeader("MIME-version : 1.0");
        $mailer->addCustomHeader('Content-type : text/calendar; name="testcal.ics"; method=REQUEST;');
        $mailer->addCustomHeader('Content-Transfer-Encoding:7bit'); //to interpret ics file
        $mailer->addCustomHeader("Content-class: urn:content-classes:calendarmessage"); 
        $mailer->addCustomHeader('Content-Disposition : inline; filename="testcal.ics"');
        return $mailer->Send(); 
}
  1. 如何在同一封电子邮件中发送 HTML 数据?
  2. outlook .ics 文件也无法下载
    任何帮助都将不胜感激

【问题讨论】:

    标签: php email yii outlook phpmailer


    【解决方案1】:

    您不应该添加那些自定义标题 - 它只会破坏事情。

    PHPMailer 包含一个名为EasyPeasyICS 的简单ICS 生成类。它只是非常简单,但它做了最重要的事情。使用方法请参考this example in the test suite

    无论您如何生成它,只需将您的 ics 数据粘贴到 $mail->Ical 中,它将作为 text/calendar 部分与通常的 text/htmltext/plain 部分一起添加到 multipart/alternative 中。这在某些客户端中有点棘手且不可靠,如果您尝试同时使用附件,您可能会遇到麻烦。

    不幸的是,PHPMailer 不允许您构建任意 MIME 结构,并且更全面地支持 iCal 部分会变得过于混乱,尤其是在面对需要混乱解决方法的某些客户端的中断支持时,正如this issue 所讨论的那样。

    【讨论】:

    • 您好,感谢您的帮助,在此之前我也尝试使用相同的方法,但它给出了诸如 Notification 之类的错误,并且它的行为没有名为“assertNotEmpty”的方法或闭包。
    • 呃!不要只是从测试中复制和粘贴——这是一个测试,而不是可用的代码!看一下,看看ICS类是怎么用的。
    • 您好,我正在尝试相同的方法,但我只在邮件和 .ics 文件 ICS 文件中获取 html 文本以及我当前工作页面的整个 html
    • $ical = new EasyPeasyICS(); $ical->addEvent('15-07-2015 08:00:00','15-07-2015 17:00:00',"ICS 和 HTML","ICS 和 HTML",""); $ical->setName('schedule'); $mailer->setFrom('info@domain.com', 'info'); $mailer->addAddress('name@outlook.com', 'Name'); $mailer->Body = '这是邮件的 HTML 部分。'; $mailer->AltBody = '这是邮件的正文部分。'; $mailer->Subject .= 'EasyPeasyICS iCal'; $mailer->Ical = $ical->render(); $mailer->send();
    • 所以检查返回值,而不是仅仅假设它们正在工作。
    猜你喜欢
    • 2011-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-26
    • 2014-06-22
    相关资源
    最近更新 更多