Jump to content

[SOLVED] Trouble with mail () Bcc/Cc/Attachments


GreenSmurf

Recommended Posts

This is my code. Everything looks pretty normal. It does infact work if $headers only has the From: information in it and if the attachment section is completely removed. Any advice would help. Below is the code and output.

 

<?
include ("include/dbconnect.php");
include ("include/format.inc.php");
include ("include/header.inc.php");

$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";

$my_email = $_REQUEST["myemail"];
$my_name = ucfirst($_REQUEST["myname"]);
$e_subject = $_REQUEST["subject"];
$email = $_REQUEST["email"];
$names = $_REQUEST["names"];
$bus = $_REQUEST["bus"];

$to_email = explode(" ", $email);
$to_name = explode(" ", $names);
$headers = "From: \"$my_name\" $my_email\r\n";
$headers .= "Reply-To: \"$my_name\" $my_email\r\n";
$headers .= "Bcc: [email protected]; $my_email\r\n";
$headers .= "MIME-Version: 1.0\r\n"."Content-Type: multipart/mixed;\r\n"." boundary=\"{$mime_boundary}\"\r\n";

$message = "This is a multi-part message in MIME format.\r\n" .
"--{$mime_boundary}\r\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\r\n" .
"Content-Transfer-Encoding: 7bit\r\n" .
$message . "\r\n";
	  
foreach($_FILES as $userfile)
{
$tmp_name = $userfile['tmp_name'];
$type = $userfile['type'];
$name = $userfile['name'];
$size = $userfile['size'];

if (file_exists($tmp_name))
{
   if(is_uploaded_file($tmp_name))
   {
      $file = fopen($tmp_name,'rb');

      $data = fread($file,filesize($tmp_name));

      fclose($file);


      $data = chunk_split(base64_encode($data));
   }

   $message .= "--{$mime_boundary}\r\n" .
      "Content-Type: {$type};\r\n" .
      " name=\"{$name}\"\r\n" .
      "Content-Disposition: attachment;\r\n" .
      " filename=\"{$fileatt_name}\"\r\n" .
      "Content-Transfer-Encoding: base64\r\n" .
   $data . "\r\n";
}
}

if ($bus != "" AND $bus != " ")
   $company = explode(" ", $bus);
else
    $company = array_fill(0, count($to_email),"Non business");
for($i = 0; $i < count($to_email)-1; $i++){
  $message = $_REQUEST["comments"];
  if ($my_email != "" && $my_name != ""){
   if ($to_email[$i] != ""){
      if ($e_subject == "" OR $message == ""){
         echo "Subject or body of e-mail left blank.<br>";
      }
      else {
           $to_name[$i] = str_replace('-',' ',$to_name[$i]);
           //echo $to_name[$i]."<br>";
           //echo $company[$i]."<br>";
           $message=str_replace('[clientname]',$to_name[$i],$message);
           $message=str_replace('[company]',$company[$i],$message);
           //echo $to_email[$i]."<br><br>";
	   //echo $e_subject."<br><br>";
	   //echo $message."<br><br>";
	   //echo $headers."<br><br>";
           mail($to_email[$i],$e_subject,$message,$headers) or die("Email error");
           
           echo "Mail sent to ".$to_email[$i]."<br>";
     }
  }
    else {
      echo " Recipient e-mail was left blank.<br>";
    }
  }
  else {
     echo "Your e-mail or name was not entered properly.<br>";
  }
}
echo "<br><a href='index$page_ext'>Home Page</a>";

include ("include/footer.inc.php");
?>

 

[email protected]

This is a test.

Attached is a GIF image for testing.

From: "B-rad" [email protected] Reply-To: "B-rad" [email protected] Bcc: [email protected]; MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="==Multipart_Boundary_xb63513d182c978d7321dc6d4a827150ex"

Mail.php:

<?

include ("include/dbconnect.php");
include ("include/format.inc.php");
include ("include/header.inc.php");

$email = str_replace(";", " ", $_REQUEST['email']);
$names = str_replace(";", " ", $_REQUEST['names']);
$bus = str_replace(";", " ", $_REQUEST['bus']);

?><form action="FormToEmail.php" method="post" enctype="multipart/form-data">
<table border="0" width="550px" cellspacing="5">
<colgroup>
  <col width="65px">
  <col width=1*>
  <col width=2*>
</colgroup>
<tr><td>Your Email: </td><td><input type="text" size="63" name="myemail"></td><td></td></tr>
<!-- <tr><td>Your Name: </td><td><input type="text" size="63" name="myname"></td><td> </td></tr> -->
<tr><td>To: </td>
    <td>
        <textarea type="text" name="email" rows="5" cols="48" readonly><? echo $email; ?></textarea>
        <input type="hidden" name="bus" value="<? echo $bus; ?>">
        <input type="hidden" name="names" value="<? echo $names; ?>">
    </td>
    <td> </td>
</tr>
<tr><td align="right" valign="middle">Attach file: </td>
<td align="left" valign="middle"><input type="file" name="attachFile"></td>
<td> </td>
</tr>
<tr><td> </td><td><small>Please limit attachments to 8Mb</small></td><td> </td></tr>
<tr><td>Subject: </td><td><input type="text" size="63" name="subject"></td></tr>
<tr><td valign="top">Body</td><td><textarea name="comments" rows="20" cols="48"></textarea></td>
</tr>
</table>
<table>
<colgroup>
  <col width="65px">
  <col width=1* align="right">
  <col width=550px>
</colgroup>
<tr><td> </td><td>[clientname] </td><td> Client name will be inserted into e-mail.</td></tr>
<tr><td> </td><td>[company] </td><td> Client company will be inserted into e-mail.</td></tr>
<tr><td> </td><td><input type="submit" value="Send" onClick="return CheckData2()"></td></tr>
</table>
</form>
<?
include ("include/footer.inc.php");
?>

 

FormToEmail.php:

<?
include ("include/dbconnect.php");
include ("include/format.inc.php");
include ("include/header.inc.php");

$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";

$my_email = $_REQUEST["myemail"];
$my_name = ucfirst($_REQUEST["myname"]);
$e_subject = $_REQUEST["subject"];
$email = $_REQUEST["email"];
$names = $_REQUEST["names"];
$bus = $_REQUEST["bus"];
$comments = $_REQUEST["comments"];

$to_email = explode(" ", $email);
$to_name = explode(" ", $names);
$headers = "From: $my_email\r\n";
$headers .= "Reply-To: $my_email\r\n";
$headers .= "Bcc: [email protected]; $my_email\r\n";
$headers .= "MIME-Version: 1.0\r\n"."Content-Type: multipart/mixed;\r\n"." boundary=\"{$mime_boundary}\"\r\n";

$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" .
$comments . "\n\n";
	  
foreach($_FILES as $userfile)
{
$tmp_name = $userfile['tmp_name'];
$type = $userfile['type'];
$name = $userfile['name'];
$size = $userfile['size'];

if (file_exists($tmp_name))
{
   if(is_uploaded_file($tmp_name))
   {
      $file = fopen($tmp_name,'rb');

      $data = fread($file,filesize($tmp_name));

      fclose($file);


      $data = chunk_split(base64_encode($data));
   }

   $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";
}
}

if ($bus != "" AND $bus != " ")
   $company = explode(" ", $bus);
else
    $company = array_fill(0, count($to_email),"Non business");
for($i = 0; $i < count($to_email)-1; $i++){
  if ($my_email != ""){
   if ($to_email[$i] != ""){
      if ($e_subject == "" OR $message == ""){
         echo "Subject or body of e-mail left blank.<br>";
      }
      else {
           $to_name[$i] = str_replace('-',' ',$to_name[$i]);
           //echo $to_name[$i]."<br>";
           //echo $company[$i]."<br>";
           $message=str_replace('[clientname]',$to_name[$i],$message);
           $message=str_replace('[company]',$company[$i],$message);
           //echo $to_email[$i]."<br><br>";
	   //echo $e_subject."<br><br>";
	   //echo $message."<br><br>";
	   //echo $headers."<br><br>";
           mail($to_email[$i],$e_subject,$message,$headers) or die("Email error");
           
           echo "Mail sent to ".$to_email[$i]."<br>";
     }
  }
    else {
      echo " Recipient e-mail was left blank.<br>";
    }
  }
  else {
     echo "Your e-mail was not entered properly.<br>";
  }
}
echo "<br><a href='index$page_ext'>Home Page</a>";

include ("include/footer.inc.php");
?>

 

This is the working version. Thanks to all that helped me.

 

 

-Brandon

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.