Jump to content
ngrmm

attach a dynamic .ics-file to a mail

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();

 

Share this post


Link to post
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

Share this post


Link to post
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?

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By ngrmm
      I have a page which sends out an email based on queries in the url
      <?php // event ID $eventID = $input->get('eventID','int'); $event = $pages->get($eventID); // config $adminEmail = "events@test.com"; $fromEmail = "noreply@test.com"; $fromName = "test"; $emailSubject = "Test Email"; // HTML BODY $emailBody = ""; // HOW TO ??? … // send mail $m = new WireMail(); $m->to($adminEmail); $m->from($fromEmail, $fromName); $m->subject($emailSubject); $m->bodyHTML($emailBody); $m->send(); ?> The email body is a bit complex: standard html/css tables and some php (pw variables).
      I put my emailbody (html) in a seperate file emailbody.inc but don't know how to include it?
       
    • By Mithlesh
      Hi there,
      My form is not getting submitted, it is showing:
      Unable to verify successful email delivery of this form submission.
      Attaching for your reference as well: 

      In the Backend, it is showing Connection timed out with smtp.gmail.com
      Pl guide me how to resolve that
    • By abdulqayyum
      hello processwire community,
      i am using LoginRegister module for front-end user registration. i am facing an issue verification email not receiving while WireMailSmtp is working perfectly and user receives message
      "Thank you, a confirmation code has been emailed to you. When you receive the email, click the link it contains, or paste the confirmation code below."
      received this message in front-end but does not receive any email relevant it.
      please help me in this case how i can solve it.
      Please check screen shots for SMTP test and message which receives user.
      Thanks AbdulQayyum,

      First screenshot SMTP test message, second when user receives message after clicking register button.
    • By louisstephens
      I have been using the wiresmtpmail module in a few installs for sending out automated emails etc etc, and it works flawlessly. However, I now need to send an email when a user submits a form, and for the life of me I can not figure out how to get the form to submit.
      if(isset($_POST['Submit'])) { $mail = wireMail(); // calling an empty wireMail() returns a wireMail object $toName = $sanitizer->text($input->post->fullname); $toEmail = $sanitizer->email($input->post->email); $phone = $sanitizer->text($input->post->phone); $comments = $sanitizer->text($input->post->comments); $subject = $page->store_name . $page->title; $numSent = wireMail($toName, '', $subject, $textBody, $options); } If I remove the if statement, the form submits just fine, however you can just refresh the page (without adding anything to the fields), and it will submit a blank email every refresh. Has anyone gotten this working with a submit button?
    • By dragan
      I'm suddently getting this error message whenever I want to edit a page:
      Error: Class 'ProcessWire\WireMail' not found (line 58 of /home/somedir/www/foo/wire/core/WireMailTools.php)
      It results in server error 500.
      I'm certainly never touching anything in the wire folder, the WireMail files are all there.
      Has anybody an idea what causes this? I'm practically blocked.
×
×
  • Create New...