0

我正在使用 php mailer 发送 iCal 和一些 html 文本

这是我的代码

测试.php

<?php
include('/opt/lampp/htdocs/UI/ical/iCal.php');

$sendermail = "info@mysite.com";
$receivermail="myemail@gmail.com";
$subjectline="beep beeep testing 123 ical and html mail";



$msg="Some description of the event or meeting here.";
$icalarray = array("isCal"=>"true", "start"=>"20110326T160000", "end"=>"20110326T180000", "summary"=>"this will be a great meeting blabla", "location"=>"Next to the parking lot");

$hi = sendHtmlandIcal($receivermail,$subjectline, $msg,$sendermail, $icalarray );

echo $hi;


?>

这是 iCal.php

<?php 
include('/opt/lampp/htdocs/UI/user/SMTPconfig.php');

/**
 * Send mail using phpmailer, for details make sure to read phpmailer http://www.tig12.net/downloads/apidocs/wp/wp-includes/PHPMailer.class.html and icalendar RFC  http://tools.ietf.org/html/rfc5545
 *
 * @param string $to Receiver of the mail
 * @param string $subject Subject of the email to be sent
 * @param string $message Message to be sent
 * @param string $sender Sender of the mail
 * @param array $ical for icalendar parameters, includes isCal, start, end, summary, location
 * @access public
 * @return string Returns success message if the mail was successfully accepted for delivery, error message otherwise
 */
function sendHtmlandIcal($to, $subject, $message, $sender, $ical)
{
    $mail = new PHPMailer();

    $body = $message; //just in case as this can cause errors
    $mail->AddAddress($to);
    $mail->Subject = $subject;  

    //send out icalendar if you request it
    if ($ical['isCal']=="true"):
        //icalendar format, alot more can be added
        $body="BEGIN:VCALENDAR
            VERSION:2.0
            METHOD:REQUEST
            BEGIN:VEVENT
            CREATED:".date('Ymd')."
            DTSTAMP:".date('Ymd//This')."
            UID:".date('Ymdhis')."
            SUMMARY:".$ical['summary']."
            ORGANIZER;RSVP=TRUE;CN=".$sender.";PARTSTAT=ACCEPTED;ROLE=CHAIR:mailto
             :".$sender."
            DTSTART;".$ical['start']."
            DTEND;".$ical['end']."
            LOCATION:".$ical['location']."
            DESCRIPTION:".$message."
            END:VEVENT
            END:VCALENDAR
            ";

        //manually setup headers so phpmailer does not break format and so that icalendar works
        $headers = "From: ".$sender."\n";
        $headers .= "Reply-To: ".$sender."\n";
        $headers .= "MIME-Version: 1.0\n";
        $headers .= "Content-Type: text/calendar; method=REQUEST; charset=utf-8\n";
        $headers .= "Content-Transfer-Encoding: 8bit\n";
        $headers .= "Content-class: urn:content-classes:calendarmessage\n";

        $mailerror="Mailer Error: " . $mail->ErrorInfo;
        $mailsuccess="Sent!";

       if(!$mail->Send($headers,$body)):
            return $mailerror;
       else:
            return $mailsuccess;
       endif;
    else:
        $mail->MsgHTML($body);
        if(!$mail->Send()):
            return $mailerror;
        else:
            return $mailsuccess;
        endif;
    endif;
}
?>

排队$mailerror="Mailer Error: " . $mail->ErrorInfo;

我为错误信息定义了上面的行,但我没有收到任何错误信息

echo$hi仅打印Mailer Error:

请告诉我哪里做错了

4

1 回答 1

0

那是因为你打电话ErrorInfo太早了。你应该使用PHPMailer Exception. 此外,您应该包装一个try catch块以获得更好的方法。

try {
    $mail->Send($headers,$body);
    return $mailsuccess;
} catch (phpmailerException $e) {
    return $e->errorMessage(); // error from PHPMailer
} catch (Exception $e) {
    return $e->getMessage(); // Other erros
}
于 2012-08-21T06:24:55.510 回答