PHP:使用php邮件发送工具发送邮件 [英] PHP : send ical using php mailer

查看:94
本文介绍了PHP:使用php邮件发送工具发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用php邮件发送邮件

I am trying to send the ical using the php mailer

此处是我的功能

function sendIcalEmail($firstname,$lastname,$email,$meeting_date,$meeting_name,$meeting_duration) {



    //Convert MYSQL datetime and construct iCal start, end and issue dates
    $meetingstamp = strtotime($meeting_date . " UTC");    
    $dtstart= gmdate("Ymd\THis\Z",$meetingstamp);
    $dtend= gmdate("Ymd\THis\Z",$meetingstamp+$meeting_duration);
    $todaystamp = gmdate("Ymd\THis\Z");

    //Create unique identifier
    $cal_uid = date('Ymd').'T'.date('His')."-".rand()."@mydomain.com";

    //Create Mime Boundry
    $mime_boundary = "----Meeting Booking----".md5(time());

    //Create Email Headers
    $headers = "From: ".$from_name." <".$from_address.">\n";
    $headers .= "Reply-To: ".$from_name." <".$from_address.">\n";

    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-Type: multipart/alternative; boundary=\"$mime_boundary\"\n";
    $headers .= "Content-class: urn:content-classes:calendarmessage\n";

    //Create Email Body (HTML)
        $message = '';
    $message .= "--$mime_boundary\n";
    $message .= "Content-Type: text/html; charset=UTF-8\n";
    $message .= "Content-Transfer-Encoding: 8bit\n\n";

    $message .= "<html>\n";
    $message .= "<body>\n";
    $message .= '<p>Dear '.$firstname.' '.$lastname.',</p>';
    $message .= '<p>Here is my HTML Email / Used for Meeting Description</p>';    
    $message .= "</body>\n";
    $message .= "</html>\n";
    $message .= "--$mime_boundary\n";

    //Create ICAL Content (Google rfc 2445 for details and examples of usage) 
    $ical =    'BEGIN:VCALENDAR
PRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//EN
VERSION:2.0
METHOD:PUBLISH
BEGIN:VEVENT
ORGANIZER:MAILTO:'.$from_address.'
DTSTART:'.$dtstart.'
DTEND:'.$dtend.'
LOCATION:'.$meeting_location.'
TRANSP:OPAQUE
SEQUENCE:0
UID:'.$cal_uid.'
DTSTAMP:'.$todaystamp.'
DESCRIPTION:'.$meeting_description.'
SUMMARY:'.$subject.'
PRIORITY:5
CLASS:PUBLIC
END:VEVENT
END:VCALENDAR';   

    $message .= 'Content-Type: text/calendar;name="meeting.ics";method=REQUEST;charset=utf-8\n';
    $message .= 'Content-Type: text/calendar;name="meeting.ics";method=REQUEST\n';
    $message .= "Content-Transfer-Encoding: 8bit\n\n";
    $message .= $ical;            

    //SEND MAIL


    $mail->AddAddress($email);

    $mail->Subject =  $subject;
    $mail->IsHTML(true);
    //$mail->AddAttachment($message);
    $mail->Body =$message;
    if(!$mail->Send())
    {
        $message= "Error sending: " . $mail->ErrorInfo;

    }


}

我正在使用php mailer,但是不幸的是,邮件无法以ical格式发送.

I am using php mailer but unfortunately mail is not coming yp in ical format .

请告诉我我做错了.

我也无法通过此邮件发送标头.

I am also not getting a way to send the header along this mail.

推荐答案

尝试添加内容类型为"multipart/alternative"的内容.从有效的我的功能(使用PEAR的Mail)中复制:

Try adding a content-type of "multipart/alternative". Copying from a function of mine that has worked (it uses PEAR's Mail):

protected function eF_mail_multipart($sender, $recipient, $subject, $textbody, $calendarbody, $onlyText = false, $bcc = false) {

        $hdrs = array('From'    => $sender,
                'Subject' => $subject,
                //'To'      => $recipient,
                'Date' => date("r"));
        if ($bcc) {
            //$hdrs['To'] = '';
        }

        $params = array("text_charset" => "UTF-8",
                "html_charset" => "UTF-8",
                "head_charset" => "UTF-8",
                "head_encoding" => "base64");


        $textparams = array(
                'charset'       => 'utf-8',
                'content_type'  => 'text/plain',
                'encoding'      => 'base64',
        );

        $calendarparams = array(
                'charset'       => 'utf-8',
                'content_type'  => 'text/calendar;method=REQUEST',
                'encoding'      => 'base64',
        );


        $email = new Mail_mimePart('', array('content_type' => 'multipart/alternative'));

        $textmime = $email->addSubPart($textbody, $textparams);
        $htmlmime = $email->addSubPart($calendarbody, $calendarparams);


        $final = $email->encode();
        $final['headers'] = array_merge($final['headers'], $hdrs);

        $smtp = Mail::factory('smtp', array('auth'      => $GLOBALS['configuration']['smtp_auth'] ? true : false,
                'host'      => $GLOBALS['configuration']['smtp_host'],
                'password'  => $GLOBALS['configuration']['smtp_pass'],
                'port'      => $GLOBALS['configuration']['smtp_port'],
                'username'  => $GLOBALS['configuration']['smtp_user'],
                'timeout'   => $GLOBALS['configuration']['smtp_timeout'],
                'localhost' => $_SERVER["HTTP_HOST"]));

        $result = $smtp -> send($recipient, $final['headers'], $final['body']);

        return $result;
    }

这篇关于PHP:使用php邮件发送工具发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆