#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    1
    Rep Power
    0

    How to send PHP email calendar request with HTML body


    Hi there,

    I am trying to send an email using PHP (in Wordpress) that is a calendar request. The calendar request arrives fine however the message body (which should be HTML) is blank.

    Below is the code. Any help would be GREATLY appreciated.

    Tania
    Code:
    -------------------------------------
    //$firstname is the first name of target
    //$lastname is the last name of target
    //$email is the targets email address
    //$meeting_date is straight from a DATETIME mysql field and assumes UTC.
    //$meeting_name is the name of your meeting
    //$meeting_duration is the duration of your meeting in seconds (3600 = 1 hour)
    //$message_body is the body of the message.
     
    function sendIcalEmail($firstname,$lastname,$email,$subject,$meeting_date,$meeting_name,$meeting_duration,$message_body,$attachments = "") {
    
    	$meetingstamp = STRTOTIME($meeting_date . " UTC");    
    	$dtstart= GMDATE("Ymd\THis\Z",$meetingstamp);
    	$dtend= GMDATE("Ymd\THis\Z",$meetingstamp+$meeting_duration);
    	$todaystamp = GMDATE("Ymd\THis\Z");
    	$from_name = "My Company";
    	$from_address = "company@domain.com.au";
     	
    	
    	$meeting_description = $message_body."\n\n";
    	$meeting_location = "My Office"; //Where will your meeting take place
     
     
    	//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()."@quattrocatering.com.au";
     
    	//Create Mime Boundry
    	$mime_boundary = "----Meeting Booking----".MD5(TIME());
    
    /*Setting the header part, this is important */
    
     
    	//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: text/calendar;\nmethod=REQUEST;\n";  
    	$headers .= '        charset="UTF-8"';				
    	$headers .= "\n";						
    	$headers .= "Content-Transfer-Encoding: 7bit";	
    
    	//Create Email Body (HTML)
    	$message = "--$mime_boundary\r\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 '.$to_name.',</p>';
    	$message .= '<p>'.$description.'</p>';
    	$message .= "</body>\n";
    	$message .= "</html>\n";
    	$message .= "--$mime_boundary\r\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:REQUEST
    BEGIN:VEVENT
    STATUS:NEEDS ACTION
    EXPECT: IMMEDIATE
    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
    X-MICROSOFT-CDO-IMPORTANCE:1
    CLASS:PUBLIC
    BEGIN:VALARM
    TRIGGER:-PT3D
    ACTION:DISPLAY
    DESCRIPTION:Reminder
    END:VALARM
    BEGIN:VTIMEZONE
    TZID:US/Central
    END:VTIMEZONE
    END:VEVENT
    END:VCALENDAR';   
    
    
    	$message .= 'Content-Type: text/calendar;name="meeting.ics";method=REQUEST\n';
    	$message .= "Content-Transfer-Encoding: 8bit\n\n";
    	$message .= $ical;            
     
    	//SEND MAIL
    	ob_start();
    
    	wp_mail( $email, $subject, $message, $headers, $attachments );
    
    	ob_end_clean();
      
     
    }
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    I don't know the exact standards for mails, but this looks pretty broken. There is no boundary definition in the Content-Type header, the new lines are a wild mixture of \r\n and \n (\r\n is the correct one), and the random whitespace between the headers also looks weird.

    It might be a good idea to completely throw away this lowlevel stuff and use a mailer library like phpmailer. Only use mail() if you really know what you're doing.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    1
    Rep Power
    0
    Originally Posted by wattsyourweb
    Hi there,

    I am trying to send an email using PHP (in Wordpress) that is a calendar request. The calendar request arrives fine however the message body (which should be HTML) is blank.

    Below is the code. Any help would be GREATLY appreciated.

    Tania
    Code:
    -------------------------------------
    //$firstname is the first name of target
    //$lastname is the last name of target
    //$email is the targets email address
    //$meeting_date is straight from a DATETIME mysql field and assumes UTC.
    //$meeting_name is the name of your meeting
    //$meeting_duration is the duration of your meeting in seconds (3600 = 1 hour)
    //$message_body is the body of the message.
     
    function sendIcalEmail($firstname,$lastname,$email,$subject,$meeting_date,$meeting_name,$meeting_duration,$message_body,$attachments = "") {
    
    	$meetingstamp = STRTOTIME($meeting_date . " UTC");    
    	$dtstart= GMDATE("Ymd\THis\Z",$meetingstamp);
    	$dtend= GMDATE("Ymd\THis\Z",$meetingstamp+$meeting_duration);
    	$todaystamp = GMDATE("Ymd\THis\Z");
    	$from_name = "My Company";
    	$from_address = "company@domain.com.au";
     	
    	
    	$meeting_description = $message_body."\n\n";
    	$meeting_location = "My Office"; //Where will your meeting take place
     
     
    	//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()."@quattrocatering.com.au";
     
    	//Create Mime Boundry
    	$mime_boundary = "----Meeting Booking----".MD5(TIME());
    
    /*Setting the header part, this is important */
    
     
    	//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: text/calendar;\nmethod=REQUEST;\n";  
    	$headers .= '        charset="UTF-8"';				
    	$headers .= "\n";						
    	$headers .= "Content-Transfer-Encoding: 7bit";	
    
    	//Create Email Body (HTML)
    	$message = "--$mime_boundary\r\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 '.$to_name.',</p>';
    	$message .= '<p>'.$description.'</p>';
    	$message .= "</body>\n";
    	$message .= "</html>\n";
    	$message .= "--$mime_boundary\r\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:REQUEST
    BEGIN:VEVENT
    STATUS:NEEDS ACTION
    EXPECT: IMMEDIATE
    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
    X-MICROSOFT-CDO-IMPORTANCE:1
    CLASS:PUBLIC
    BEGIN:VALARM
    TRIGGER:-PT3D
    ACTION:DISPLAY
    DESCRIPTION:Reminder
    END:VALARM
    BEGIN:VTIMEZONE
    TZID:US/Central
    END:VTIMEZONE
    END:VEVENT
    END:VCALENDAR';   
    
    
    	$message .= 'Content-Type: text/calendar;name="meeting.ics";method=REQUEST\n';
    	$message .= "Content-Transfer-Encoding: 8bit\n\n";
    	$message .= $ical;            
     
    	//SEND MAIL
    	ob_start();
    
    	wp_mail( $email, $subject, $message, $headers, $attachments );
    
    	ob_end_clean();
      
     
    }

    Code:
    $message = "BEGIN:VCALENDAR\r\n";
    		$message .= "VERSION:2.0\r\n";
    		$message .= "CALSCALE:GREGORIAN\r\n";
    		$message .= "METHOD:REQUEST\r\n";
    		$message .= "BEGIN:VEVENT\r\n";
    		$message .= "STATUS:CONFIRMED\r\n";
    		$message .= "DTSTART:".$dates."T".$dstart."\r\n";
    		$message .= "DTEND:".$dates."T".$dend."\r\n";
    		$message .= "ORGANIZER;RSVP=TRUE;CN=\"".$toName[0]->name."\":mailto:".$from."\r\n";
    		$message .= "UID:".$inid."\r\n";
    		$message .= 'X-ALT-DESC;FMTTYPE=text/html:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">\n<HTML>\n<HEAD>\n<META NAME="Generator" CONTENT="MS Exchange Server version 08.01.0240.003">\n<TITLE></TITLE>\n</HEAD>\n<BODY>\n'.$description.'</BODY>\n</HTML>'."\r\n";
    		$message .= "ATTENDEE;PARTSTAT=NEEDS-ACTION;RSVP= TRUE;CN=recipient:".$result[0]->email."\r\n";
    		$message .= "LOCATION:room1\r\n";
    		$message .= "TRANSP:OPAQUE\r\n";
    		$message .= "SEQUENCE:".++$seq."\r\n";
    		$message .= "DESCRIPTION:\r\n";
    		$message .= "SUMMARY:".$subject."\r\n";
    		$message .= "ACTION:DISPLAY\r\n";
    		$message .= "END:VEVENT\r\n";
    		$message .= "END:VCALENDAR\r\n";

IMN logo majestic logo threadwatch logo seochat tools logo