0

I am using php mailer to send an iCal and some html text

Here is my code

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;


?>

and here is 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;
}
?>

in line $mailerror="Mailer Error: " . $mail->ErrorInfo;

The above line i defined for error info but i am not getting any error information

echo $hi is printing only Mailer Error:

Please tell me where i am doing wrong

Auberon Vacher
  • 4,655
  • 1
  • 24
  • 36
Nags
  • 157
  • 4
  • 14

1 Answers1

0

That's because you call for ErrorInfo too early. And you should use PHPMailer Exception. Also you should wrap in a try catch block for a better approach.

try {
    $mail->Send($headers,$body);
    return $mailsuccess;
} catch (phpmailerException $e) {
    return $e->errorMessage(); // error from PHPMailer
} catch (Exception $e) {
    return $e->getMessage(); // Other erros
}
Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107