PHP:PHPMailer错误未显示

时间:2012-08-21 06:20:25

标签: php phpmailer rfc5545

我正在使用php邮件程序发送iCal和一些html文本

这是我的代码

test.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:

请告诉我我在哪里做错了

1 个答案:

答案 0 :(得分: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
}