PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 16th, 2013, 02:26 AM
wattsyourweb wattsyourweb is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 1 wattsyourweb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 m 49 sec
Reputation 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,$me  ssage_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();
  
 
}

Reply With Quote
  #2  
Old January 16th, 2013, 04:41 AM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,854 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 15 h 10 m 12 sec
Reputation Power: 813
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.

Reply With Quote
  #3  
Old February 8th, 2013, 03:08 AM
magickiss magickiss is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 1 magickiss User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 m 49 sec
Reputation Power: 0
Quote:
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,$me  ssage_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";

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > How to send PHP email calendar request with HTML body

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap