Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Send mail with multiple attachments 1

Status
Not open for further replies.

TanTrazz

Programmer
Aug 18, 2001
54
NL
Hi guys,

Im making a mailform with attachments. It works with the code below for one attachment. But i want to have 2 attachtments attached. Does someone know how i can do it?

Code:
<?php
// Your e-mail adress:
$mailto = "naam@sitenaam.nl";
# Maximum size of attachment in bytes:
$max_attach_size = 500000;
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<html>
<head>
<title>Formmailer</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body><?php
/*if (empty($_POST['form_submitted']))
{
?></font><p><font color="#FFFFFF">Please fill out the form:</font></p>
<font color="#FFFFFF"><?php
}
*/
if (isset($_POST["form_submitted"]))
{
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$text = $_POST['text'];
unset($errors);
if ($email != "" and !preg_match("/^[^@]+@.+\.\D{2,5}$/", $email)) $errors[] = "e-mail address lijkt incorrect";
if ($text == "") $errors[] = "Geen bericht geplaatst";
if ($_FILES['probe']['size'] > $max_attach_size) $errors[] = "Bijlage is te groot(".number_format($_FILES['probe']['size']/1000,0,",","")." KB) - maximum size: ".number_format($max_attach_size/1000,0,",","")." KB";
if (empty($errors))
{
$text = stripslashes($text);
$subject = stripslashes($subject);
if ($name != "") $mail_name=$name; else $mail_name="Unknown";
if ($subject != "") $mail_subject = $subject; else $mail_subject = "No subject";
if ($email != "") $mail_email = $email; else $mail_email = "email@unknown.xyz";
$ip = $_SERVER["REMOTE_ADDR"];
// if attachment, MIME-Mail:
if (isset($_FILES['probe']['name']) && trim($_FILES['probe']['name']) != "")
{
// read and encode file:
$datei_content = fread(fopen($_FILES['probe']['tmp_name'],"r"),filesize($_FILES['probe']['tmp_name']));
$datei_content = chunk_split(base64_encode($datei_content),76,"\n");
// Boundary:
$boundary = md5(uniqid(rand()));
// Mail-Header:
$mail_header = "From: ".$mail_name." <".$mail_email.">\n";
$mail_header .= "X-Sender-IP: ".$ip."\n";
$mail_header .= "MIME-Version: 1.0\n";
$mail_header .= "Content-Type: multipart/mixed; boundary=\"".$boundary."\"\n";
$mail_header .= "This is a multi-part message in MIME format.\n";
// Mail-Text:
$mail_header .= "--".$boundary;
$mail_header .= "\nContent-Type: text/plain";
$mail_header .= "\nContent-Transfer-Encoding: 8bit";
$mail_header .= "\n\n".$text;
// Attachment:
$mail_header .= "\n--".$boundary;
$mail_header .= "\nContent-Type: ".$_FILES['probe']['type']."; name=\"".$_FILES['probe']['name']."\"";
$mail_header .= "\nContent-Transfer-Encoding: base64";
$mail_header .= "\nContent-Disposition: attachment; filename=\"".$_FILES['probe']['name']."\"";
$mail_header .= "\n\n".$datei_content;
// End:
$mail_header .= "\n--".$boundary."--";
// Sende E-Mail und gebe Fehler bzw. Bestaetigung aus
if (@mail($mailto,$mail_subject,"",$mail_header)) $sent = true; else $errors[] = "no connection to the mailserver - please try again later";
}
// no attachment, normal E-mail:
else
{
$mail_header = "From: ".$mail_name." <".$mail_email.">\n";
$mail_header .= "X-Sender-IP: $ip\n";
$mail_header .= "Content-Type: text/plain";
if (@mail($mailto,$mail_subject,$text,$mail_header)) $sent = true; else $errors[] = "no connection to the mailserver - please try again later";
}
// copy to sender:
if (isset($sent) && isset($email) && $email != "" && isset($_POST['copy']))
{
if (isset($_FILES['probe']['name']) && trim($_FILES['probe']['name']) != "") $copy_mail_text = "Copy of the e-mail:\n\n".$text."\n\nAttachment: ".$_FILES['probe']['name']; else $copy_mail_text = "Copy of the e-mail:\n\n".$text;
$header= "From: ".$mailto."\n";
$header .= "X-Sender-IP: ".$ip."\n";
$header .= "Content-Type: text/plain";
@mail($email, $mail_subject, $copy_mail_text, $header);
}
}
}
if (empty($sent))
{
if(isset($errors))
{
?></font><p class="caution"><font color="#FFFFFF">Error:</font></p><ul>
<font color="#FFFFFF"><?php foreach($errors as $f) { ?></font><li>
<font color="#FFFFFF"><?php echo $f; ?></li><?php } ?></font></ul>
<font color="#FFFFFF"><br /><?php
}
?></font><form method="post" action="<?php echo basename($_SERVER["PHP_SELF"]); ?>" enctype="multipart/form-data"><div>
<p><font color="#FFFFFF"><span class="style2"><font face="Verdana" size="2">
Naam</font></span><font face="Verdana" size="2"><span class="style2">:</span><br />
</font>
</font><font color="#FFFFFF">
<input name="name" value="<?php if (isset($name)) echo htmlentities(stripslashes($name)); else echo ""; ?>" size="25" style="font-family: Verdana; border: 1px dashed #000000" />
<font face="Verdana" size="2">
<br />
<span class="style2">E-mail:</span><br />
</font>
</font><font color="#FFFFFF">
<input name="email" value="<?php if (isset($email)) echo htmlentities(stripslashes($email)); else echo ""; ?>" size="25" style="font-family: Verdana; border: 1px dashed #000000" />
<font face="Verdana" size="2">
<br />
<span class="style2">Onderwerp:</span><br />
</font>
</font><font color="#FFFFFF">
<input name="subject" value="<?php if (isset($subject)) echo htmlentities(stripslashes($subject)); else echo ""; ?>" size="25" style="font-family: Verdana; border: 1px dashed #000000" />
<font face="Verdana" size="2">
<br />
<span class="style2">Bericht:</span><br />
</font>
</font><font color="#FFFFFF">
<textarea name="text" cols="25" rows="7" style="font-family: Verdana; border: 1px dashed #000000"><?php if (isset($text)) echo htmlentities(stripslashes($text)); else echo ""; ?>
</textarea>
<font face="Verdana" size="2">
<br />
<span class="style2">Bijlagen:</span><br />
</font>
</font><font color="#FFFFFF">
<input type="file" name="probe" value="<?php if (isset($_POST['probe'])) echo htmlentities(stripslashes($_POST['probe'])); else echo ""; ?>" size="16" style="font-family: Verdana; border: 1px dashed #000000"/>
</font>
</p>
</p>
<p><font face="Verdana"><font color="#FFFFFF"><input type="checkbox" name="copy" value="true" /></font><font size="2" color="#FFFFFF">
<span class="style2">Kopietje naar jezelf sturen?</span> </font></font>
<font color="#FFFFFF">
<input type="submit" name="form_submitted" value="OK - Verzenden" style="font-family: Verdana; border: 1px dashed #000000" /></font><font face="Verdana" size="2" color="#FFFFFF">
</font> </p>
</div>
</form><font color="#FFFFFF"><?php
}
else
{
if (empty($email)) { ?></font></font><font color="#FFFFFF"> </font>
<p><font color="#FFFFFF"><b><font face="Verdana" size="1">Bedankt</font></b><font size="1" face="Verdana"><b>!</b><br />
Je mail is verzonden alleen kan ik je niet terug mailen omdat je geen e-mail 
adres hebt ingevuld! </font></font></p>
<font face="Verdana" size="1"><font color="#FFFFFF"><?php }
else { ?></font></font><font color="#FFFFFF"> </font>
<p><font color="#FFFFFF"><b><font face="Verdana" size="1">Bedankt</font></b><font size="1" face="Verdana"><b>!</b><br />
Je bericht is met succes verzonden ik zal zo spoedig mogelijk terug mailen. </font>
</font></p>
<font face="Verdana" size="1" color="#FFFFFF"><?php }
}
?>

tnx in advance

TT
 
this script works for 2 attachments:

Code:
<?php
if ($_SERVER['REQUEST_METHOD']=="POST"){

   // we'll begin by assigning the To address and message subject
   $to="mailme@aol.com";
   $to1= $_POST['email'];
   $subject="ASSIGNMENT REQUEST FORM";

   // 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 = "From: " . $_POST['name'] . "\r\n";   

   // 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
  mail($to, $subject, $message, $headers);
  mail($to1, $subject, $message, $headers);
  
  header('Location: [URL unfurl="true"]http://www.wherever.com/');}[/URL]
 ?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top