When i use the php mail function to send mail is can send easily but if i continuously let said 10 email suddenly it tell me can not sent the mail.

Can anyone tell me why ??

Recommended Answers

All 4 Replies

Perhaps the email is being classified as spam or maybe theres a bug in your code. Try posting the code you used for the 10 emails for us to check for bugs. Note that you may want to replace the email address with fake ones in the sample code you posted on the forums.

Perhaps the email is being classified as spam or maybe theres a bug in your code. Try posting the code you used for the 10 emails for us to check for bugs. Note that you may want to replace the email address with fake ones in the sample code you posted on the forums.

Here the code:
index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>UNISEM Help Desk System</title>
<style type="text/css">
<!--
.style9 {	font-size: 45px;
	color: #000000;
	font-family: Georgia, "Times New Roman", Times, serif;
	font-weight: bold;
}
.style15 {font-size: 14px; font-weight: bold; font-family: Arial, Helvetica, sans-serif; }
body {
	background-color: #fcf5f5;
}
-->
</style>
</head>

<body>
<table width="1005" height="77" border="0" bordercolor="#a32626" bgcolor="#a32626">
  <tr>
    <td>&nbsp;</td>
  </tr>
</table>
<table width="1005" height="117" border="0">
  <tr>
    <td><div align="center"><span class="style9">Your IP is: <?php echo $_SERVER['REMOTE_ADDR'] ; ?></span></div></td>
  </tr>
</table>
<table width="425" height="33" border="0">
<script language="JavaScript" type="text/javascript">
				<!-- Begin
				var now = new Date();
				var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
				var months = new Array('January','February','March','April','May','June','July','August','September','October','November','Dicember');
				var date = ((now.getDate()<10) ? "0" : "")+ now.getDate();
				function fourdigits(number)	{
					return (number < 1000) ? number + 1900 : number;
												}
				today =  days[now.getDay()] + ", " +
							  date + " " +
							  months[now.getMonth()] + " " +
								(fourdigits(now.getYear())) ;
				document.write("<H1>" +today+ "</H1>");
				//  End -->
				</script>
  <tr>
    <td>&nbsp;</td>
  </tr>
</table>
<div id="page">
  <!-- end #content -->
  <form action="send.php" method="post" enctype="multipart/form-data" >
    <table width="740" height="314" border="0" align="center">
      <tr>
        <td width="277" class="style15">To</td>
        <td width="453"><input type="text" name="to" value="" size="45"  /></td
      >
      </tr>
      <tr>
        <td><strong class="style15">From (Name) </strong></td>
        <td><input type="text" name="from" value="" size="45"  /></td
      >
      </tr>
      
      
      <tr>
        <td><span class="style15"> Your E-Mail Address (Optional)</span></td>
        <td><label>
          <input type="text" name="email" size="45" />
        </label></td>
      </tr>
      <tr>
        <td><span class="style15">Phone Ext</span></td>
        <td><label>
          <input type="text" name="phone" size="25" />
        </label></td>
      </tr>
      <tr>
        <td><span class="style15">IP Address</span></td>
        <td><label>
          <input type="text" name="ip" size="25" />
        </label></td>
      </tr>
      <tr>
        <td class="style15">Subject</td>
        <td><input type="text" name="subject" size="45" /></td>
      </tr>
      <tr>
        <td><span class="style15">Problem</span></td>
        <td><label>
          <textarea cols="60" rows="15" name="message"></textarea>
          </p>
        </label></td>
      </tr>
      <tr>
        <td><strong class="style15">File Attachment:</strong></td>
        <td><p>
          <input type="file" name="fileatt" />
        </p>
          <p>
            <input type="file" name="fileatt" />
          </p>
          <p>
            <input type="file" name="fileatt" />
</p></td>
      </tr>
      <tr>
        <td><label>
          <input type="Submit" name="submit" value="Submit" />
        </label></td>
        <td>&nbsp;</td>
      </tr>
    </table>
    <label></label>
    <p>&nbsp;</p>
    <p>
      <label></label>
    </p>
  </form>
</div>
<table width="1009" height="32" border="0" bordercolor="#950000" bgcolor="#950000">
  <tr>
    <td>&nbsp;</td>
  </tr>
</table>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
</body>
</html>

Below is the send.php

<?php

$to= $_POST['to'];
$from=$_POST['from'];
$subject = $_POST['subject'];
$message=$_POST['message'];

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

$headers = "From: $from";

foreach($_FILES['fileatt'] as $file) { 
    if(is_uploaded_file($file['tmp_name'])) { 
  // 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" .
             $message . "\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";
}

// Send the message
$ok = @mail($to, $subject, $message, $headers);
if ($ok) {
  echo "<p>Mail sent! Yay PHP!</p>";
} else {
  echo "<p>Mail could not be sent. Sorry!</p>";
}
?>

After checking line by line I discovered only 1 line with 1 symbol needs adding. In the gap just before the comment // Send the message simple add a second closing bracket to close the foreach loop. That's the biggest bug I can see and below is an example of what needs changing. Let me know if that works.

//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
}

} //this was added
// Send the message
$ok = @mail($to, $subject, $message, $headers);
if ($ok) {
echo "<p>Mail sent! Yay PHP!</p>";
} else {
echo "<p>Mail could not be sent. Sorry!</p>";
}
?>

I received your pm and the only other problem I can see is that you only specified one email address. Below is an example of specifying mutliple email addresses:

<?php

$to= $_POST['to'];
$from=$_POST['from'];
$subject = $_POST['subject'];
$message=$_POST['message'];

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

$headers = "From: $from";

foreach($_FILES['fileatt'] as $file) { 
    if(is_uploaded_file($file['tmp_name'])) { 
  // 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" .
             $message . "\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";
}

// Send the message
$emails=array($to,
'email@hotmail.com',
'email@gmail.com',
'test@gmail.com',
'another@hotmail.com');
$errors=0;
foreach ($emails AS $email) {
    if (!mail($email, $subject, $message, $headers)) {
    $errors+=1;
    }
}
if (errors==0) {
  echo "<p>All mail sent! Yay PHP!</p>";
} else {
  echo "<p>$errors emails could not be sent. Sorry!</p>";
}
?>
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.