Jump to content

attach a dynamic .ics-file to a mail


ngrmm
 Share

Recommended Posts

i'm trying to attach a .ics-file to a mail

The Page send sends out the email but only download the ics-file. Email has no attachments.

	// event ID
	$eventID = $input->get('eventID','int');
	$event = $pages->get($eventID);

	// config
	$testEmail = $event->event_mail_test_adress;
	$fromEmail = $event->event_mail_from;
	$fromName = $event->event_mail_from_name;
	$emailSubject = $event->event_subject;
	
	// .ics
	$filename = $event->name;
	header('Content-Encoding: UTF-8');
	header('Content-type: text/calendar; charset=utf-8');
	header("Content-Disposition: attachment; filename={$filename}.ics");

	// fetch start date 
	$event_start_ts = $event->getUnformatted("date_start");

	// build the .ics data
	$ical_data  = 'BEGIN:VCALENDAR';
	$ical_data .= "\r\n";
	$ical_data .= 'VERSION:2.0';
	$ical_data .= "\r\n";
	$ical_data .= 'CALSCALE:GREGORIAN';
	$ical_data .= "\r\n";
	$ical_data .= 'METHOD:PUBLISH';
	$ical_data .= "\r\n";
	$ical_data .= 'BEGIN:VEVENT';
	$ical_data .= "\r\n";
	$ical_data .= 'SUMMARY:'.$event->title;
	$ical_data .= "\r\n";
	$ical_data .= 'UID:' . md5(uniqid(mt_rand(), true)) . '@'.$config->httpHost;
	$ical_data .= "\r\n";
	$ical_data .= 'CLASS:PUBLIC';
	$ical_data .= "\r\n";
	$ical_data .= 'STATUS:CONFIRMED';
	$ical_data .= "\r\n";
	$ical_data .= 'DTSTART:'.date('Ymd', $event_start_ts).'T'.date("His", $event_start_ts);
	$ical_data .= "\r\n";
	$ical_data .= 'DTSTAMP:'.date('Ymd').'T'.date('His');
	$ical_data .= "\r\n";
	$ical_data .= 'LOCATION:'.$event->venue;
	$ical_data .= "\r\n";
	$ical_data .= 'URL:'.$event->httpUrl;
	$ical_data .= "\r\n";
	$ical_data .= 'END:VEVENT';
	$ical_data .= "\r\n";
	$ical_data .= 'END:VCALENDAR';

	// HTML BODY
	ob_start();
	include('./_inc/emailbody.inc');
	$emailBody = ob_get_clean();

	// send email
	$m = new WireMail();
	$m->to($testEmail);
	$m->from($fromEmail, $fromName);
	$m->subject($emailSubject);
	$m->bodyHTML($emailBody);
	$m->attachment($ical_data);
	$m->send();

 

Link to comment
Share on other sites

Where does filename come from?
 

header("Content-Disposition: attachment; filename={$filename}.ics");

Why are you assigning an attachment that is not a file.

$m->attachment($ical_data);

Is the attatchment boundary defined ?

  • Like 1
Link to comment
Share on other sites

 

41 minutes ago, rick said:

Where does filename come from?


header("Content-Disposition: attachment; filename={$filename}.ics");

Why are you assigning an attachment that is not a file.


$m->attachment($ical_data);

Is the attatchment boundary defined ?

 

Filename is a $page->name

Sorry, i'm not an expert. What kind of headers do i need? And how do i define a boundery?
Is there a manual how to that?

Link to comment
Share on other sites

Hi @ngrmm,

You don't have to assign a boundary, that should be done automatically by the email module when an attachment is recognised. I was referring to troubleshooting by looking at the headers to see if there was a boundary id for an attachment. You shouldn't have any issues if you follow the WireMail api examples.

Specifically,

$m->attachment('/path/to/file.ext');

indicates your attachment should be the path to the file. You would therefore write out your ICS file and use the resulting filepath.

  • Like 2
Link to comment
Share on other sites

thank you @rick
it works!

here the code:
 

	// event ID
	$eventID = $input->get('eventID','int');
	$event = $pages->get($eventID);

	// config
	$testEmail = $event->event_mail_test_adress;
	$fromEmail = $event->event_mail_from;
	$fromName = $event->event_mail_from_name;
	$emailSubject = $event->event_mail_subject;
	$filenameDate = strftime('%d-%m-%Y', $event->date_start);

	// filename
	$filename = "{$event->name}_{$filenameDate}";
	
	// create ics file
	$file = fopen('ics/'.$filename.'.ics', 'w') or die('File can not be saved!'); 

	// fetch start date 
	$event_start_ts = $event->getUnformatted("date_start");

	// build the .ics data
	$ical_data = "\r\n";
	$ical_data .= 'BEGIN:VCALENDAR';
	
	
	
	$ical_data .= "\r\n";
	$ical_data .= 'END:VCALENDAR';
	
	fwrite($file, $ical_data);
	fclose($file);

	// HTML BODY
	ob_start();
	include('./_inc/emailbody.inc');
	$emailBody = ob_get_clean();

	// send email
	$m = new WireMail();
	$m->to($testEmail);
	$m->from($fromEmail, $fromName);
	$m->subject($emailSubject);
	$m->bodyHTML($emailBody);
	$m->attachment('ics/'.$filename.'.ics');
	$m->send();

 

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...