By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,739 Members | 1,523 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,739 IT Pros & Developers. It's quick & easy.

cannot add ics file attachment to email

P: n/a
So I have been trying to get this to work all day. I can't get a local
file on my web server to attach to an email. Right now I have it
printing out in the body of the email. Please help me with any
thouhgts on how to get it in as an attachment.

CODE:
<?php
ini_set(SMTP, "172.18.1.65");
ini_set(smtp_port, "25");

$engineer_email = $_REQUEST['engineer_email'] ;
$recipient = $_REQUEST['engineer_email'] ;
$PM_email = $_REQUEST['PM_email'] ;
$email = $PM_email;
$customer_name = $_REQUEST['customer_name'];
$customer_street_address = $_REQUEST['customer_street_address'];
$customer_name = $_REQUEST['customer_name'];
$individual_name = $_REQUEST['individual_name'];
$customer_city = $_REQUEST['customer_city'];
$customer_state = $_REQUEST['customer_state'];
$customer_phone = $_REQUEST['customer_phone'];
$individual_email = $_REQUEST['individual_email'];

$day = $_REQUEST['day'];
$dayofweek = $_REQUEST['dayofweek'];
$month = $_REQUEST['month'];
$day = $_REQUEST['day'];
$year= $_REQUEST['year'];
$time = $_REQUEST['time'];

$cust_confirmed = $_REQUEST['cust_confirmed'];
$hardware_confirmed = $_REQUEST['hardware_confirmed'];
$ss_confirmed = $_REQUEST['ss_confirmed'];
$hours_left = $_REQUEST['hours_left'];
$time_sheet = $_REQUEST['time_sheet'];

$ob = "----=_OuterBoundary_000";
$ib = "----=_InnerBoundery_001";
//CHECKS
if( empty($recipient) || empty($PM_email) ||
empty($hardware_confirmed) || empty($customer_name) ||
empty($customer_street_address) || empty($customer_name) ||
empty($individual_name) || empty($customer_city) ||
empty($customer_state) || empty($customer_phone) || empty($day) ||
empty($dayofweek) || empty($month) || empty($year) || empty($time)
|| empty($cust_confirmed) || empty($ss_confirmed )) {

echo "You did not supply all the necessary fields. Please use the back
button and complete.";
exit;
}

if( $ss_confirmed == "no") {
echo "You did not supply answer yes to 'Spreadsheet schedule in
black.' You must use this tool only when those are completed";
exit;
}

if( $cust_confirmed == "no") {
echo "You did not supply answer yes to 'Customer confirmed'. You must
use this tool only when those are completed";
exit;
}

//HERE WE MAKE THE VCAL FILE

$myFile = "dispatch.ics";

$fh = fopen("$myFile", "x+");

$stringData = "BEGIN:VCALENDAR\n";
$stringData .= "PRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//
EN\n";
$stringData .= "VERSION:2.0\n";
$stringData .= "METHOD:PUBLISH\n";
$stringData .= "BEGIN:VEVENT\n";
$stringData .= "ORGANIZER:MAILTO:Ad***********@exchange.org\n ";
$stringData .= "DTSTART:20060306T050000Z\n";
$stringData .= "DTEND:20060306T053000Z\n";
$stringData .= "LOCATION:location\n";
$stringData .= "TRANSP:OPAQUE\n";
$stringData .= "SEQUENCE:0\n";
$stringData .= "UID:
040000008200E00074C5B7101A82E0080000000020DC2A1392 43C60100000000000000000000DEF81CB498E1594B90378312 C3D6551D
\n";
$stringData .= "DTSTAMP:20060309T045649Z\n";
$stringData .= "DESCRIPTION:FreeFormText.\nMore FreeFormText.\n\n";
$stringData .= "SUMMARY:subject\n";
$stringData .= "PRIORITY:5\n";
$stringData .= "X-MICROSOFT-CDO-IMPORTANCE:1\n";
$stringData .= "CLASS:PUBLIC\n";
$stringData .= "END:VEVENT\n";
$stringData .= "END:VCALENDAR\n";

fwrite($fh, $stringData);

//CLOSE VCAL FILE
fclose($fh);

$headers .= "MIME-Version: 1.0\r\n";
$headers .= "From: ".$email."\n";
$headers .= "To: ".$engineer_email."\n";
$headers .= "CC: ".$PM_email."\n";
$headers .= "Reply-To: ".$email."\n";

$headers .= "X-Priority: 1\n";
$headers .= "X-Mailer: DT Formmail".VERSION."\n";
$headers .= "Content-Type: multipart/mixed;\n\tboundary=\"".
$ob."\"\n";

//MESSAGE
$message .= "\n--".$ob."\n";
$message .= "Content-Type: multipart/alternative;\n\tboundary=\"".
$ib."\"\n\n";
$message .= "\n--".$ib."\n";
$message .= "Content-Type: text/html;\n\tcharset=\"iso-8859-1\"\n";
$message .= "Content-Transfer-Encoding: quoted-printable\n\n";
//WHAT
$message .= "<b>What: <br>";
$message .= "</b>$customer_name <br>";
$message .= "<br>";
//WHO
$message .= "<b>Who: <br>";
$message .= "</b>$individual_name <br>";
$message .= "$customer_phone <br>";
$message .= "$individual_email <br>";
$message .= "<br>";
//WHERE
$message .= "<b>Where: <br>";
$message .= "</b>$customer_street_address <br>";
$message .= "$customer_city, $customer_state <br>";
$message .= "<br>";

//WHEN
$message .= "<b>When: <br>";
$message .= "</b>$dayofweek <br>";
$message .= "$time <br>";
$message .= "$month $day, $year<br>";
$message .= "<br>";

//CONFIRMATION INFO

$message .= "<b>Checkpoints: <br>";
$message .= "</b>Was the activity confirmed with the customer?
&nbsp;&nbsp; $cust_confirmed <br>";
$message .= "</b>Is the activity in black on the schedule?
&nbsp;&nbsp; $ss_confirmed <br>";

if( $hardware_confirmed == "no") {
$message .= " <font color='#FF0000'>Is the required hardware
already onsite? &nbsp;&nbsp; $hardware_confirmed </font>
<br>";
}
else{

$message .= "</b>Is the required hardware already onsite?
&nbsp;&nbsp; $hardware_confirmed <br>";
}
$message .= "</b>Do we have any hours left in a T&M?
&nbsp;&nbsp; $hours_left <br>";
$message .= "</b>Do you assign the task to the engineer on his
timesheet? &nbsp;&nbsp; $time_sheet <br>";
$message .= "<br>";

//HERE WE ATTACH THE FILE
$fileatt = "dispatch.ics"; // Path to the file
$fileatt_type = "application/octet-stream"; // File Type
$fileatt_name = "dispatch.ics"; // Filename that will be used for the
file as the attachment

$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$data = chunk_split(base64_encode($data));

$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}\n";
unset($data);
unset($file);
unset($fileatt);
unset($fileatt_type);
unset($fileatt_name);

$message .= "\n--".$ib."--\n";

$message .= "\n--".$ob."--\n";
$subject = "Scheduled activity for $customer_name ";

mail($recipient, $subject, $message, $headers);
?>















OUTPUT in email form:

What:
asd

Who:
asd
sad
asdasd

Where:
asd
sada, MA

When:
Monday
9am
February 2, 2008

Checkpoints:
Was the activity confirmed with the customer? yes
Is the activity in black on the schedule? yes
Is the required hardware already onsite? yes
Do we have any hours left in a T&M? yes
Do you assign the task to the engineer on his timesheet? yes

------=uterBoundary_000 Content-Type: application/octet-stream;
name=ispatch.ics" Content-Disposition: attachment;
filename=ispatch.ics" Content-Transfer-Encoding: base64
BEGIN:VCALENDAR PRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//
EN VERSION:2.0 METHOD:PUBLISH BEGIN:VEVENT
ORGANIZER:MAILTO:Ad***********@exchange.org DTSTART:20060306T050000Z
DTEND:20060306T053000Z LOCATION:location TRANSP:OPAQUE SEQUENCE:0 UID:
040000008200E00074C5B7101A82E0080000000020DC2A1392 43C60100000000000000000000DEF81CB498E1594B90378312 C3D6551D
DTSTAMP:20060309T045649Z DESCRIPTION:FreeFormText. More FreeFormText.
SUMMARY:subject PRIORITY:5 X-MICROSOFT-CDO-IMPORTANCE:1 CLASS:PUBLIC
END:VEVENT END:VCALENDAR
Oct 23 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Erik Witkop wrote:
So I have been trying to get this to work all day. I can't get a local
file on my web server to attach to an email. Right now I have it
printing out in the body of the email. Please help me with any
thouhgts on how to get it in as an attachment.

CODE:
<?php
ini_set(SMTP, "172.18.1.65");
ini_set(smtp_port, "25");

$engineer_email = $_REQUEST['engineer_email'] ;
$recipient = $_REQUEST['engineer_email'] ;
$PM_email = $_REQUEST['PM_email'] ;
$email = $PM_email;
$customer_name = $_REQUEST['customer_name'];
$customer_street_address = $_REQUEST['customer_street_address'];
$customer_name = $_REQUEST['customer_name'];
$individual_name = $_REQUEST['individual_name'];
$customer_city = $_REQUEST['customer_city'];
$customer_state = $_REQUEST['customer_state'];
$customer_phone = $_REQUEST['customer_phone'];
$individual_email = $_REQUEST['individual_email'];

$day = $_REQUEST['day'];
$dayofweek = $_REQUEST['dayofweek'];
$month = $_REQUEST['month'];
$day = $_REQUEST['day'];
$year= $_REQUEST['year'];
$time = $_REQUEST['time'];

$cust_confirmed = $_REQUEST['cust_confirmed'];
$hardware_confirmed = $_REQUEST['hardware_confirmed'];
$ss_confirmed = $_REQUEST['ss_confirmed'];
$hours_left = $_REQUEST['hours_left'];
$time_sheet = $_REQUEST['time_sheet'];

$ob = "----=_OuterBoundary_000";
$ib = "----=_InnerBoundery_001";
//CHECKS
if( empty($recipient) || empty($PM_email) ||
empty($hardware_confirmed) || empty($customer_name) ||
empty($customer_street_address) || empty($customer_name) ||
empty($individual_name) || empty($customer_city) ||
empty($customer_state) || empty($customer_phone) || empty($day) ||
empty($dayofweek) || empty($month) || empty($year) || empty($time)
|| empty($cust_confirmed) || empty($ss_confirmed )) {

echo "You did not supply all the necessary fields. Please use the back
button and complete.";
exit;
}

if( $ss_confirmed == "no") {
echo "You did not supply answer yes to 'Spreadsheet schedule in
black.' You must use this tool only when those are completed";
exit;
}

if( $cust_confirmed == "no") {
echo "You did not supply answer yes to 'Customer confirmed'. You must
use this tool only when those are completed";
exit;
}

//HERE WE MAKE THE VCAL FILE

$myFile = "dispatch.ics";

$fh = fopen("$myFile", "x+");

$stringData = "BEGIN:VCALENDAR\n";
$stringData .= "PRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//
EN\n";
$stringData .= "VERSION:2.0\n";
$stringData .= "METHOD:PUBLISH\n";
$stringData .= "BEGIN:VEVENT\n";
$stringData .= "ORGANIZER:MAILTO:Ad***********@exchange.org\n ";
$stringData .= "DTSTART:20060306T050000Z\n";
$stringData .= "DTEND:20060306T053000Z\n";
$stringData .= "LOCATION:location\n";
$stringData .= "TRANSP:OPAQUE\n";
$stringData .= "SEQUENCE:0\n";
$stringData .= "UID:
040000008200E00074C5B7101A82E0080000000020DC2A1392 43C60100000000000000000000DEF81CB498E1594B90378312 C3D6551D
\n";
$stringData .= "DTSTAMP:20060309T045649Z\n";
$stringData .= "DESCRIPTION:FreeFormText.\nMore FreeFormText.\n\n";
$stringData .= "SUMMARY:subject\n";
$stringData .= "PRIORITY:5\n";
$stringData .= "X-MICROSOFT-CDO-IMPORTANCE:1\n";
$stringData .= "CLASS:PUBLIC\n";
$stringData .= "END:VEVENT\n";
$stringData .= "END:VCALENDAR\n";

fwrite($fh, $stringData);

//CLOSE VCAL FILE
fclose($fh);

$headers .= "MIME-Version: 1.0\r\n";
$headers .= "From: ".$email."\n";
$headers .= "To: ".$engineer_email."\n";
$headers .= "CC: ".$PM_email."\n";
$headers .= "Reply-To: ".$email."\n";

$headers .= "X-Priority: 1\n";
$headers .= "X-Mailer: DT Formmail".VERSION."\n";
$headers .= "Content-Type: multipart/mixed;\n\tboundary=\"".
$ob."\"\n";

//MESSAGE
$message .= "\n--".$ob."\n";
$message .= "Content-Type: multipart/alternative;\n\tboundary=\"".
$ib."\"\n\n";
$message .= "\n--".$ib."\n";
$message .= "Content-Type: text/html;\n\tcharset=\"iso-8859-1\"\n";
$message .= "Content-Transfer-Encoding: quoted-printable\n\n";
//WHAT
$message .= "<b>What: <br>";
$message .= "</b>$customer_name <br>";
$message .= "<br>";
//WHO
$message .= "<b>Who: <br>";
$message .= "</b>$individual_name <br>";
$message .= "$customer_phone <br>";
$message .= "$individual_email <br>";
$message .= "<br>";
//WHERE
$message .= "<b>Where: <br>";
$message .= "</b>$customer_street_address <br>";
$message .= "$customer_city, $customer_state <br>";
$message .= "<br>";

//WHEN
$message .= "<b>When: <br>";
$message .= "</b>$dayofweek <br>";
$message .= "$time <br>";
$message .= "$month $day, $year<br>";
$message .= "<br>";

//CONFIRMATION INFO

$message .= "<b>Checkpoints: <br>";
$message .= "</b>Was the activity confirmed with the customer?
&nbsp;&nbsp; $cust_confirmed <br>";
$message .= "</b>Is the activity in black on the schedule?
&nbsp;&nbsp; $ss_confirmed <br>";

if( $hardware_confirmed == "no") {
$message .= " <font color='#FF0000'>Is the required hardware
already onsite? &nbsp;&nbsp; $hardware_confirmed </font>
<br>";
}
else{

$message .= "</b>Is the required hardware already onsite?
&nbsp;&nbsp; $hardware_confirmed <br>";
}
$message .= "</b>Do we have any hours left in a T&M?
&nbsp;&nbsp; $hours_left <br>";
$message .= "</b>Do you assign the task to the engineer on his
timesheet? &nbsp;&nbsp; $time_sheet <br>";
$message .= "<br>";

//HERE WE ATTACH THE FILE
$fileatt = "dispatch.ics"; // Path to the file
$fileatt_type = "application/octet-stream"; // File Type
$fileatt_name = "dispatch.ics"; // Filename that will be used for the
file as the attachment

$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$data = chunk_split(base64_encode($data));

$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}\n";
unset($data);
unset($file);
unset($fileatt);
unset($fileatt_type);
unset($fileatt_name);

$message .= "\n--".$ib."--\n";

$message .= "\n--".$ob."--\n";
$subject = "Scheduled activity for $customer_name ";

mail($recipient, $subject, $message, $headers);
?>
[lots of newlines snipped]
>
OUTPUT in email form:

What:
asd

Who:
asd
sad
asdasd

Where:
asd
sada, MA

When:
Monday
9am
February 2, 2008

Checkpoints:
Was the activity confirmed with the customer? yes
Is the activity in black on the schedule? yes
Is the required hardware already onsite? yes
Do we have any hours left in a T&M? yes
Do you assign the task to the engineer on his timesheet? yes

------=uterBoundary_000 Content-Type: application/octet-stream;
name=ispatch.ics" Content-Disposition: attachment;
filename=ispatch.ics" Content-Transfer-Encoding: base64
BEGIN:VCALENDAR PRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//
EN VERSION:2.0 METHOD:PUBLISH BEGIN:VEVENT
ORGANIZER:MAILTO:Ad***********@exchange.org DTSTART:20060306T050000Z
DTEND:20060306T053000Z LOCATION:location TRANSP:OPAQUE SEQUENCE:0 UID:
040000008200E00074C5B7101A82E0080000000020DC2A1392 43C60100000000000000000000DEF81CB498E1594B90378312 C3D6551D
DTSTAMP:20060309T045649Z DESCRIPTION:FreeFormText. More FreeFormText.
SUMMARY:subject PRIORITY:5 X-MICROSOFT-CDO-IMPORTANCE:1 CLASS:PUBLIC
END:VEVENT END:VCALENDAR
You'll probably find it easier to send attachments using PHP Mailer or
the PEAR Mail class.

PHP mailer: <URL:http://phpmailer.codeworxtech.com/>
PEAR Mail: <URL:http://pear.php.net/package/Mail>

--
Curtis
Oct 23 '08 #2

P: n/a
*** Erik Witkop escribió/wrote (Wed, 22 Oct 2008 18:49:38 -0700 (PDT)):
So I have been trying to get this to work all day. I can't get a local
file on my web server to attach to an email. Right now I have it
printing out in the body of the email. Please help me with any
thouhgts on how to get it in as an attachment.
[...]
OUTPUT in email form:

What:
asd

Who:
This is the body. Headers *do* matter; check them as well. Look at the
source code of the message in your favourite e-mail client and check that
the headers are right, the boundary markers are correct in both places, you
don't have empty lines before the body...
--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor en cubitos: http://www.demogracia.com
--
Oct 23 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.