raghu.ndu 0 Newbie Poster

Hi,
I have this PHP code to send mail with attachment... when I send mail without attachment I am getting the message but when I attach something I am getting the attachment but the message body is blank... please help!!

Below is the PHP code :

<?php

// Your e-mail adress:

$mailto = "your_name@your_domain.com";



# Maximum size of attachment in bytes:

$max_attach_size = 500000;



?>

<?php



 if (isset($_POST["form_submitted"]))

 {

  $name = $_POST['name'];

  $email = $_POST['email'];
  
  $phone = $_POST['phone'];
  
  $mobile = $_POST['mobile'];
  
  $qualification = $_POST['qualification'];
  
  $subject = $_POST['subject'];

  $text = $_POST['text'];



  unset($errors);

  if ($email != "" and !preg_match("/^[^@]+@.+\.\D{2,5}$/", $email)) $errors[] = "e-mail address is incorrect";

  if ($text == "") $errors[] = "Message can not be blank";

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

      // 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 
	  
	 
	
	
	 $mail_message .= "Name: ".$name."\n\n";
	$mail_message .= "Email: ".$email."\n\n";
	$mail_message .= "Phone: ".$phone."\n\n";
	$mail_message .= "Mobile : ".$mobile."\n\n";
	$mail_message .= "Qualification: ".$qualification."\n\n";
	$mail_message .= "Message: ".$text."\n\n";
	
	
	

      if (@mail($mailto,$mail_subject,$mail_message,$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";
	  
	  
	  $email_message .= "Name: ".$name."\n\n";
	$email_message .= "Email: ".$email."\n\n";
	$email_message .= "Phone: ".$phone."\n\n";
	$email_message .= "Mobile : ".$mobile."\n\n";
	$email_message .= "Qualification: ".$qualification."\n\n";
	$email_message .= "Message: ".$text."\n\n";

      if (@mail($mailto,$mail_subject,$email_message,$mail_header)) $sent = true; else $errors[] = "no connection to the mailserver - please try again later";

     }



   

   }

 }



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>

	<?php echo $f; ?></li><?php } ?></ul>

<font color="#FFFFFF"><br /><?php

   }



  ?></font>
  
  
  
  <font color="#FFFFFF"><?php

 }

else

 {

  if (empty($email)) { ?></font></font><font color="#000000"> </font>

<p><font color="#000000"><b><font face="Verdana" size="2">Email Id is Invalid</font></b><font size="1" face="Verdana"><b>!</b><br />

Please go back and enter a valid email id! </font></font></p>

<font face="Verdana" size="1"><font color="#000000"><?php }

  else { ?></font></font><font color="#000000"> </font>

<p><font color="#000000"><b><font face="Verdana" size="2">Thank you for your enquiry</font></b><font size="1" face="Verdana"><b>!</b><br />

We will respond to your request within 48 hrs. </font>

</font></p>

<font face="Verdana" size="1" color="#FFFFFF"><?php }

 }





?>

Here is the Contact Form Code:

<form method="post" class="appnitro" action="phptomail.php" enctype="multipart/form-data">
<table width="450" border="0" align="center" cellpadding="10" class="stPara">
<tr>
                <td colspan="2" align="left" valign="top">
                	Personal Information (All Fields with a red <span style="color:#FF0000">*</span> are required)
                </td>
                </tr>
              <tr>
                <td width="250" align="left" valign="top">
               	  <label for='name'>Name: </label><span style="color:#FF0000">*</span>
                </td>
                <td width="200" align="left" valign="top">
               	  <input type="text" name="name" value="<?php if (isset($name)) echo htmlentities(stripslashes($name)); else echo ""; ?>" >
                </td>
    </tr>
              <tr>
                <td width="250" align="left" valign="top">
               	  <label for='email'>Your Email: </label><span style="color:#FF0000">*</span>
                </td>
                <td width="200" align="left" valign="top">
               	  <input type="text" name="email" value="<?php if (isset($email)) echo htmlentities(stripslashes($email)); else echo ""; ?>">
                </td>
    </tr>
              <tr>
                <td width="250" align="left" valign="top">
               	  <label for='phone'>Phone: </label>
                </td>
                <td width="200" align="left" valign="top">
               	  <input type="text" name="phone" value="<?php if (isset($phone)) echo htmlentities(stripslashes($phone)); else echo ""; ?>" >
                </td>
    </tr>
              <tr>
                <td width="250" align="left" valign="top">
               	  <label for='mobile'>Mobile: </label>
                </td>
                <td width="200" align="left" valign="top">
               	  <input type="text" name="mobile" value="<?php if (isset($mobile)) echo htmlentities(stripslashes($mobile)); else echo ""; ?>" >
                </td>
    </tr>
              <tr>
                <td width="250" align="left" valign="top">
               	  <label for='qualification'>Qualification: </label><span style="color:#FF0000">*</span>
                </td>
                <td width="200" align="left" valign="top">
           	    <input type="text" name="qualification" value="<?php if (isset($qualification)) echo htmlentities(stripslashes($qualification)); else echo ""; ?>" >                </td>
    </tr>
              <tr>
                <td width="250" align="left" valign="top">
               	  <label for='subject'>Subject: </label>
                </td>
                <td width="200" align="left" valign="top">
               	  <input type="text" name="subject" value="<?php if (isset($subject)) echo htmlentities(stripslashes($subject)); else echo ""; ?>" >                </td>
    </tr>
              <tr>
                <td width="250" align="left" valign="top">
               	  <label for='message'>Message:</label>
                </td>
                <td width="200" align="left" valign="top">
                <textarea name="text" cols="17">
<?php if (isset($text)) echo htmlentities(stripslashes($text)); else echo ""; ?>
</textarea><br />
                </td>
    </tr>
              <tr>
                <td width="250" align="left" valign="top">
               	  <label class="description" for="element_8">Upload your resume </label>
                </td>
                <td width="200" align="left" valign="top">
                 <input name="probe" class="element file" type="file"/> <?php if (isset($_POST['probe'])) echo htmlentities(stripslashes($_POST['probe'])); else echo ""; ?>
                </td>
    </tr>
              <tr>
                <td width="250" align="left" valign="top">&nbsp;</td>
                <td width="200" align="left" valign="top">
                <input type="submit" name="form_submitted" value="Submit"  />
                </td>
    </tr>
              
        </table>

</form>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.