Page 1 of 1

Mail with attachment

Posted: Mon Aug 16, 2004 12:26 pm
by Draco_03
hi this is the code i'm using

Code: Select all

<?php
<?php
$piece = $_POST["piece"];
$email = $_POST["email"];
$serie = $_POST["serie"];
$msg = $_POST["msg"];
$warranty = $_POST["warranty"];
$destinataire = $_POST["destinataire"];

$boundary = "kader-12345";

$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary="$boundary"";
"--$boundary";
$headers .= "Content-Type: text/plain; charset="iso-8859-1"";
$headers .= "Content-Transfer-Encoding: 7bit";


$to  = "$email";
$subject = "LPG Canada";
$message = "
<html>
<head>
<title>LPG Canada</title>
</head>
<body>
Hi i'm sending you\n\n
<br /><br />
<b>Piece :</b>$piece\n<br />
<b>serial number :</b> $serie\n<br />
<b>Warranty :</b> $warranty\n\n<br /><br />
<b>The reasons are the follwing :</b>\n<br />
$msg
</body>
</html>
";
"--$boundary";
$headers .= "Content-Type: image/jpeg; name="../file.txt"";
$headers .= "Content-Transfer-Encoding: base64";
$headers .= "Content-Disposition: attachment";
$headers .= "<base64 encoded data for the zendlogo.jpg image>";
"--$boundary--";

$headers .= "To: <$destinataire>\n";
$headers .= "From: <$email>\n";

if (mail($to, $subject, $message, $headers)) {
			echo("<br /><br /><br /><center>Merci<br /><a href="../index.php">Accueil</a></center><br />");
			} else {
			echo("<center>Erreure, veuillez <a href="javascript:history.back();">cliquer ici</a> et recommencer</center>");
			}
?>
I do not get any "text" displayed BUT i do have the "attachment" sign (in outlook) next to the email, but i don't see any file actually attached to it..

I don't know nothing about attachment in emails... but i didn't want to use phpmailer.

Any help would be appreciated

Posted: Mon Aug 16, 2004 12:29 pm
by Draco_03
oh and i'm pretty sure my mistake is the $boundary thingy

Posted: Mon Aug 16, 2004 1:48 pm
by Draco_03
OK i tryed something else (tutorial over the net)

Code: Select all

<?php
$piece = $_POST["piece"];
$email = $_POST["email"];
$serie = $_POST["serie"];
$msg = $_POST["msg"];
$warranty = $_POST["warranty"];
$destinataire = $_POST["destinataire"];

/* recipients */
$to  = "$email";

/* subject */
$subject = "LPG Canada";

// Obtain file upload vars
$fileatt      = $_FILES['fileatt']['tmp_name'];
$fileatt_type = $_FILES['fileatt']['type'];
$fileatt_name = $_FILES['fileatt']['name'];

if (is_uploaded_file($fileatt)) {
 // Read the file to be attached ('rb' = read binary)
 $file = fopen($fileatt,'rb');
 $data = fread($file,filesize($fileatt));
 fclose($file);
 
  // Generate a boundary string
 $semi_rand = md5(time());
 $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
 
 // Add the headers for a file attachment
 $headers .= "\nMIME-Version: 1.0\n" .
             "Content-Type: multipart/mixed;\n" .
             " boundary="{$mime_boundary}"";

 // Add a multipart boundary above the plain message
 $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" .
            $msg . "\n\n";

 // Base64 encode the file data
 $data = chunk_split(base64_encode($data));

 // Add file attachment to the message
 $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";
}

$headers .= "To: <$destinataire>\n";
$headers .= "From: <$email>\n";

if (mail($to, $subject, $message, $headers)) {
			echo("<br /><br /><br /><center>Merci<br /><a href="../index.php">Accueil</a></center><br />");
			} else {
			echo("<center>Erreure, veuillez <a href="javascript:history.back();">cliquer ici</a> et recommencer</center>");
			}
?>
My form is

Code: Select all

&lt;form enctype="multipart/form-data" name="email" method="post" action="confirm/mailing_en_3.php"&gt;
	&lt;input type="hidden" name="MAX_FILE_SIZE" value="100000" /&gt;
    Choose a file to upload: &lt;input name="userfile" type="file" /&gt;
      &lt;table width="450" border="0" cellpadding="0" cellspacing="0"&gt;
        &lt;tr&gt; 
          &lt;td align="left" valign="top"&gt;Votre courriel&lt;/td&gt;
          &lt;td align="left" valign="top"&gt;&lt;input name="email" type="text" /&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt; 
          &lt;td align="left" valign="top"&gt;Destinataire &lt;/td&gt;
          &lt;td align="left" valign="top"&gt;&lt;input name="destinataire" type="text" /&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt; 
          &lt;td align="left" valign="top"&gt;Pi&amp;egrave;ce&lt;/td&gt;
          &lt;td align="left" valign="top"&gt;&lt;input name="piece" type="text" /&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt; 
          &lt;td align="left" valign="top"&gt;# de s&amp;eacute;rie&lt;/td&gt;
          &lt;td align="left" valign="top"&gt;&lt;input name="serie" type="text" /&gt;&lt;/td&gt;
        &lt;/tr&gt;
		&lt;tr&gt; 
          &lt;td align="left" valign="top"&gt;Warranty&lt;br /&gt;&lt;br /&gt;&lt;/td&gt;
          &lt;td align="left" valign="top"&gt;&lt;input name="warranty" type="radio" value="yes" /&gt;yes
		  								&lt;input name="warranty" type="radio" value="no" /&gt;no&lt;br /&gt;&lt;br /&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt; 
          &lt;td align="left" valign="top"&gt;Message&lt;/td&gt;
          &lt;td align="left" valign="top"&gt;&lt;textarea name="msg" cols="30"&gt;&lt;/textarea&gt;&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/table&gt;
      &lt;br /&gt;
      &lt;br /&gt;
        &lt;input type="submit" value="Soumettre" /&gt;
    &lt;/form&gt;