PHP attach files with HTML message
Moderator: General Moderators
-
The princess
- Forum Newbie
- Posts: 5
- Joined: Thu Feb 14, 2008 3:13 am
PHP attach files with HTML message
How can I send HTML message with attach 3 files or more ?
Re: PHP attach files with HTML message
Can you post what you have tried?
-
The princess
- Forum Newbie
- Posts: 5
- Joined: Thu Feb 14, 2008 3:13 am
Re: PHP attach files with HTML message
I try this code:
<?php
if ($_SERVER['REQUEST_METHOD']=="POST"){
// we'll begin by assigning the To address and message subject
$to="xxx@ggg.com";
$subject="E-mail with attachment";
// get the sender's name and email address
// we'll just plug them a variable to be used later
$from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">";
// generate a random string to be used as the boundary marker
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
// now we'll build the message headers
$headers = "From: $from\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";
// here, we'll start the message body.
// this is the text that will be displayed
// in the e-mail
$message="<table width='100%' border='0' cellpadding='0' cellspacing='0' class='content'>
<tr>
<td colspan='4'><h3>Information about customer</h3></td>
</tr>
<tr>
<td width='8%' bgcolor='#F0F1F4'> </td>
<td width='27%' bgcolor='#F0F1F4'>Customer name :</td>
<td width='3%' bgcolor='#F0F1F4'> </td>
<td width='62%' bgcolor='#F0F1F4'>$name</td>
</tr>
<tr>
<td width='8%' > </td>
<td width='27%' >Company :</td>
<td width='3%'> </td>
<td width='62%' >$company</td>
</tr>
<tr>
<td width='8%' bgcolor='#F0F1F4'> </td>
<td width='27%' bgcolor='#F0F1F4'>Job title :</td>
<td width='3%' bgcolor='#F0F1F4'> </td>
<td width='62%' bgcolor='#F0F1F4'>$job</td>
</tr>
<tr>
<td> </td>
<td>Countery :</td>
<td> </td>
<td>$countery</td>
</tr>
<tr>
<td bgcolor='#F0F1F4'> </td>
<td bgcolor='#F0F1F4'>City :</td>
<td bgcolor='#F0F1F4'> </td>
<td bgcolor='#F0F1F4'>$city</td>
</tr>
<tr>
<td> </td>
<td>E-mail :</td>
<td> </td>
<td>$email</td>
</tr>
<tr>
<td bgcolor='#F0F1F4'> </td>
<td bgcolor='#F0F1F4'>Tellephone :</td>
<td bgcolor='#F0F1F4'> </td>
<td bgcolor='#F0F1F4'>$tell</td>
</tr>
<tr>
<td> </td>
<td>Fax :</td>
<td> </td>
<td>$fax</td>
</tr>
<tr>
<td bgcolor='#F0F1F4'> </td>
<td bgcolor='#F0F1F4'>Cell :</td>
<td bgcolor='#F0F1F4'> </td>
<td bgcolor='#F0F1F4'>$cell</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr class='content-title'>
<td colspan='4'><h3>Detailed Information</h3></td>
</tr>
<tr>
<td bgcolor='#F0F1F4'> </td>
<td bgcolor='#F0F1F4'>Fabrics1 :</td>
<td bgcolor='#F0F1F4'> </td>
<td bgcolor='#F0F1F4'>$fabrics1</td>
</tr>
<tr>
<td bgcolor='#F0F1F4'> </td>
<td bgcolor='#F0F1F4'>Fabrics2 :</td>
<td bgcolor='#F0F1F4'> </td>
<td bgcolor='#F0F1F4'>$fabrics2</td>
</tr>
<tr>
<td> </td>
<td>Color :</td>
<td bgcolor='$input_field_4'> </td>
<td>$input_field_4
</td>
</tr>
<tr>
<td bgcolor='#F0F1F4'> </td>
<td bgcolor='#F0F1F4'>Size: </td>
<td bgcolor='#F0F1F4'> </td>
<td bgcolor='#F0F1F4'>$size</td>
</tr>
<tr>
<td> </td>
<td>Quantity:</td>
<td> </td>
<td>$quantity</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr class='content-title'>
<td colspan='4'><h3>Information data</h3></td>
</tr>
<tr>
<td colspan='4'>$comments</td>
</tr>
</table>";
// next, we'll build the invisible portion of the message body
// note that we insert two dashes in front of the MIME boundary
// when we use it
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
// now we'll process our uploaded files
foreach($_FILES as $userfile){
// store the file information to variables for easier access
$tmp_name = $userfile['tmp_name'];
$type = $userfile['type'];
$name = $userfile['name'];
$size = $userfile['size'];
// if the upload succeded, the file will exist
if (file_exists($tmp_name)){
// check to make sure that it is an uploaded file and not a system file
if(is_uploaded_file($tmp_name)){
// open the file for a binary read
$file = fopen($tmp_name,'rb');
// read the file content into a variable
$data = fread($file,filesize($tmp_name));
// close the file
fclose($file);
// now we encode it and split it into acceptable length lines
$data = chunk_split(base64_encode($data));
}
// now we'll insert a boundary to indicate we're starting the attachment
// we have to specify the content type, file name, and disposition as
// an attachment, then add the file content.
// NOTE: we don't set another boundary to indicate that the end of the
// file has been reached here. we only want one boundary between each file
// we'll add the final one after the loop finishes.
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n";
}
}
// here's our closing mime boundary that indicates the last of the message
$message.="--{$mime_boundary}--\n";
// now we just send the message
if (@mail($to, $subject, $message, $headers))
echo "Message Sent";
else
echo "Failed to send";
} else {
?>
<?php
if ($_SERVER['REQUEST_METHOD']=="POST"){
// we'll begin by assigning the To address and message subject
$to="xxx@ggg.com";
$subject="E-mail with attachment";
// get the sender's name and email address
// we'll just plug them a variable to be used later
$from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">";
// generate a random string to be used as the boundary marker
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
// now we'll build the message headers
$headers = "From: $from\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";
// here, we'll start the message body.
// this is the text that will be displayed
// in the e-mail
$message="<table width='100%' border='0' cellpadding='0' cellspacing='0' class='content'>
<tr>
<td colspan='4'><h3>Information about customer</h3></td>
</tr>
<tr>
<td width='8%' bgcolor='#F0F1F4'> </td>
<td width='27%' bgcolor='#F0F1F4'>Customer name :</td>
<td width='3%' bgcolor='#F0F1F4'> </td>
<td width='62%' bgcolor='#F0F1F4'>$name</td>
</tr>
<tr>
<td width='8%' > </td>
<td width='27%' >Company :</td>
<td width='3%'> </td>
<td width='62%' >$company</td>
</tr>
<tr>
<td width='8%' bgcolor='#F0F1F4'> </td>
<td width='27%' bgcolor='#F0F1F4'>Job title :</td>
<td width='3%' bgcolor='#F0F1F4'> </td>
<td width='62%' bgcolor='#F0F1F4'>$job</td>
</tr>
<tr>
<td> </td>
<td>Countery :</td>
<td> </td>
<td>$countery</td>
</tr>
<tr>
<td bgcolor='#F0F1F4'> </td>
<td bgcolor='#F0F1F4'>City :</td>
<td bgcolor='#F0F1F4'> </td>
<td bgcolor='#F0F1F4'>$city</td>
</tr>
<tr>
<td> </td>
<td>E-mail :</td>
<td> </td>
<td>$email</td>
</tr>
<tr>
<td bgcolor='#F0F1F4'> </td>
<td bgcolor='#F0F1F4'>Tellephone :</td>
<td bgcolor='#F0F1F4'> </td>
<td bgcolor='#F0F1F4'>$tell</td>
</tr>
<tr>
<td> </td>
<td>Fax :</td>
<td> </td>
<td>$fax</td>
</tr>
<tr>
<td bgcolor='#F0F1F4'> </td>
<td bgcolor='#F0F1F4'>Cell :</td>
<td bgcolor='#F0F1F4'> </td>
<td bgcolor='#F0F1F4'>$cell</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr class='content-title'>
<td colspan='4'><h3>Detailed Information</h3></td>
</tr>
<tr>
<td bgcolor='#F0F1F4'> </td>
<td bgcolor='#F0F1F4'>Fabrics1 :</td>
<td bgcolor='#F0F1F4'> </td>
<td bgcolor='#F0F1F4'>$fabrics1</td>
</tr>
<tr>
<td bgcolor='#F0F1F4'> </td>
<td bgcolor='#F0F1F4'>Fabrics2 :</td>
<td bgcolor='#F0F1F4'> </td>
<td bgcolor='#F0F1F4'>$fabrics2</td>
</tr>
<tr>
<td> </td>
<td>Color :</td>
<td bgcolor='$input_field_4'> </td>
<td>$input_field_4
</td>
</tr>
<tr>
<td bgcolor='#F0F1F4'> </td>
<td bgcolor='#F0F1F4'>Size: </td>
<td bgcolor='#F0F1F4'> </td>
<td bgcolor='#F0F1F4'>$size</td>
</tr>
<tr>
<td> </td>
<td>Quantity:</td>
<td> </td>
<td>$quantity</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr class='content-title'>
<td colspan='4'><h3>Information data</h3></td>
</tr>
<tr>
<td colspan='4'>$comments</td>
</tr>
</table>";
// next, we'll build the invisible portion of the message body
// note that we insert two dashes in front of the MIME boundary
// when we use it
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
// now we'll process our uploaded files
foreach($_FILES as $userfile){
// store the file information to variables for easier access
$tmp_name = $userfile['tmp_name'];
$type = $userfile['type'];
$name = $userfile['name'];
$size = $userfile['size'];
// if the upload succeded, the file will exist
if (file_exists($tmp_name)){
// check to make sure that it is an uploaded file and not a system file
if(is_uploaded_file($tmp_name)){
// open the file for a binary read
$file = fopen($tmp_name,'rb');
// read the file content into a variable
$data = fread($file,filesize($tmp_name));
// close the file
fclose($file);
// now we encode it and split it into acceptable length lines
$data = chunk_split(base64_encode($data));
}
// now we'll insert a boundary to indicate we're starting the attachment
// we have to specify the content type, file name, and disposition as
// an attachment, then add the file content.
// NOTE: we don't set another boundary to indicate that the end of the
// file has been reached here. we only want one boundary between each file
// we'll add the final one after the loop finishes.
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n";
}
}
// here's our closing mime boundary that indicates the last of the message
$message.="--{$mime_boundary}--\n";
// now we just send the message
if (@mail($to, $subject, $message, $headers))
echo "Message Sent";
else
echo "Failed to send";
} else {
?>
Re: PHP attach files with HTML message
Right off the bat I don't see anything horribly wrong with that. I'm not a mail expert though. Chris is the mail expert around here and if you wait a bit he can probably give you some insight.
-
The princess
- Forum Newbie
- Posts: 5
- Joined: Thu Feb 14, 2008 3:13 am
Re: PHP attach files with HTML message
When I open the message I see HTML tags
Re: PHP attach files with HTML message
Some searching yielded this example:
You probably want to check and make sure the email is being formatted correctly.
Code: Select all
Content-Type: multipart/related; boundary="boundary-example";
type="text/html"
--boundary-example
Content-Type: text/html; charset="US-ASCII"
... ... <IMG SRC="fiction1/fiction2"> ... ...
... ... <IMG SRC="cid:97116092811xyz@foo.bar.net"> ... ...
--boundary-example
Content-Type: image/gif
Content-ID: <97116092511xyz@foo.bar.net>
Content-Location: fiction1/fiction2
--boundary-example
Content-Type: image/gif
Content-ID: <97116092811xyz@foo.bar.net>
Content-Location: fiction1/fiction3
--boundary-example--
-
The princess
- Forum Newbie
- Posts: 5
- Joined: Thu Feb 14, 2008 3:13 am
Re: PHP attach files with HTML message
Thank you I will try it
Re: PHP attach files with HTML message
I don't know how versed in PHP you are, but you may want to have a look at http://www.swiftmailer.org/
It will take care of proper formatting for you and there are examples on Chris's site.
It will take care of proper formatting for you and there are examples on Chris's site.
-
The princess
- Forum Newbie
- Posts: 5
- Joined: Thu Feb 14, 2008 3:13 am
Re: PHP attach files with HTML message
I see some examples at this site , it may works ,many thanks for your time