Jump to content

Recommended Posts

Hi all

I am trying to write a script where the subject, message, and a possible selection of 2 files to be attached to a mail are selected from the previous screen. The files are uploaded and then I would like to pull them from the webserver and attach them to the mail. The mail is going through fine but no attachments.

 

Please help. I am including my code so far.

 

<?php
  session_start();
if ((!isset($_SESSION['username'])) && (!isset($_SESSION['password']))) {
header ("Location: index.php");
} else {
    include 'menu.php';

$query1 = "INSERT INTO mail_ref (date) VALUES (NOW())";
		$result1 = @mysql_query ($query1);
$queryref = "SELECT * from mail_ref ORDER BY ref_no ASC";
$resultref = mysql_query($queryref, $conn);
while ($rowref = mysql_fetch_assoc($resultref)){
$reference_out = $rowref["ref_no"];  
}
$_SESSION['ref'] = $reference_out;

    
$max_size_out = $_POST['MAX_FILE_SIZE'];
$subject_out = $_POST['subject'];
$message_out = $_POST['message'];
$attach1_name_sf_out = $_FILES['attach1']['name'];
$attach1_size_sf_out = $_FILES['attach1']['size'];
$attach1_type_sf_out = $_FILES['attach1']['type'];
$attach2_name_sf_out = $_FILES['attach2']['name'];
$attach2_size_sf_out = $_FILES['attach2']['size'];
$attach2_type_sf_out = $_FILES['attach2']['type'];
$mail_no = $_SESSION['ref'];
$fulldate = date("Y-m-d");

// Check to see if attachment does not exist in attachments table
if ($attach1_name_sf_out != '') {
$query_attach1 = "SELECT * from attachments WHERE filename = '{$_FILES['attach1']['name']}' AND ref_no = '$mail_no'";
$result_attach1 = mysql_query($query_attach1, $conn);
$num_attach1 = @mysql_num_rows($result_attach1);
}
if ($attach2_name_sf_out != '') {
$query_attach2 = "SELECT * from attachments WHERE filename = '{$_FILES['attach2']['name']}' AND ref_no = '$mail_no'";
$result_attach2 = mysql_query($query_attach2, $conn);
$num_attach2 = @mysql_num_rows($result_attach2);
}
// if value does not exist write entry to attachment table with a mail id

if (($num_attach1 == 0) && ($attach1_name_sf_out != '')) {
$query_add_attach1 = "INSERT INTO attachments (ref_no, filename, filesize, filetype, upload_date) VALUES ('$mail_no', '{$_FILES['attach1']['name']}', '{$_FILES['attach1']['size']}', '{$_FILES['attach2']['type']}', '$fulldate')";
$result_add_attach1 = mysql_query($query_add_attach1);
}
if (($num_attach2 == 0) && ($attach2_name_sf_out != '')) {

$query_add_attach2 = "INSERT INTO attachments (ref_no, filename, filesize, filetype, upload_date) VALUES ('$mail_no', '{$_FILES['attach2']['name']}', '{$_FILES['attach2']['size']}', '{$_FILES['attach2']['type']}', '$fulldate')";
$result_add_attach2 = mysql_query($query_add_attach2);
}
if ($attach1_name_sf_out != '') {
  $extension = explode('.', $_FILES['attach1']['name']);
$uid = mysql_insert_id();
$filename1 = $_FILES['attach1']['name'];

// Move the file over
if (move_uploaded_file($_FILES['attach1']['tmp_name'], "uploads/$filename1")) {

}
	}
if ($attach2_name_sf_out != '') {	
$extension = explode('.', $_FILES['attach2']['name']);
$uid = mysql_insert_id();
$filename2 = $_FILES['attach2']['name'];

// Move the file over
if (move_uploaded_file($_FILES['attach2']['tmp_name'], "uploads/$filename2")) {
	}
	}


?>
  <table border="0" width="100%" align="center">
  <table border="0" width="100%" align="center">
<tr><td>
<tr>
<div align="center" width="100%">
<td align="left" width="100%"><font face="arial" size="2" color="black">
<div align="justify">
  
  </div></div>
  </td>
  
  </tr></table> 
<FORM  method='GET' name='docs' action='bulk_mail_selection.php'>
<DIV id='title' align="center">
<font size="3">Metaboilic Typing Bulk Emailing System</font>
</DIV>
<DIV align="center">
<TABLE width="790" valign="TOP" cellpadding='10' cellspacing='0' align="center">
   <TR>
   	<?php
      	  
	// Display the cart if it is not empty

    include 'includes/conn_db.php';
					// Retrieve all of the information for the clients
		$query = 'SELECT * FROM patients WHERE patients_id IN (';
		foreach($_POST['mailist'] as $key => $value) {
			$query .= $key . ',';

		}
		$query = substr ($query, 0, -1) . ') ORDER BY email ASC';

		$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
      	$patients_id_out = $row['patients_id'];
	$to = $row['email'];

	// check that the file has not already being sent to the client
if ($attach1_name_sf_out != '') {
$query_attach1_exist = "SELECT * from client_attachment WHERE filename = '{$_FILES['attach1']['name']}' AND patients_id = '$patients_id_out'";
$result_attach1_exist = mysql_query($query_attach1_exist, $conn);
$num_attach1_exist = @mysql_num_rows($result_attach1_exist);
if ($num_attach1_exist == '') {
$num_attach1_exist = 0;	
}
}

if ($attach2_name_sf_out != '') {
$query_attach2_exist = "SELECT * from client_attachment WHERE filename = '{$_FILES['attach2']['name']}' AND patients_id = '$patients_id_out'";
$result_attach2_exist = mysql_query($query_attach2_exist, $conn);
$num_attach2_exist = @mysql_num_rows($result_attach2_exist);
$num_attach2_exist = 0;
}
$query = "SELECT * FROM attachments WHERE ref_no = '$reference_out'"; 

$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){
$a .= '"'. $row['filename'] . '",';

}

function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
    
   $file = $path.$filename;
   $file_size = filesize($file);
    $handle = fopen($file, "r");
    $content = fread($handle, $file_size);
    fclose($handle);
    $content = chunk_split(base64_encode($content));
    $uid = md5(uniqid(time()));
    $name = basename($file);
    $header = "From: ".$from_name." <".$from_mail.">\r\n";
    $header .= "Reply-To: ".$replyto."\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
    $header .= "This is a multi-part message in MIME format.\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $header .= $message."\r\n\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use diff. tyoes here 
    $header .= "Content-Transfer-Encoding: base64\r\n";
    $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
    $header .= $content."\r\n\r\n";
    $header .= "--".$uid."--";
    if (mail($mailto, $subject, "", $header)) {
        echo "<table border=1><tr><td><b>Thank you<br>Your order has been sent successfully. A consultant will contact you shortly</b></td></tr></table>"; // or use booleans here
    } else {
        echo "<table border=1><tr><td><b>mail send ... ERROR!</b></td></tr></table>";
    }
}
// how to use
$query = "SELECT * FROM attachments WHERE ref_no = '$reference_out'"; 

$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){
$my_file .= '"'. $row['filename'] . '",';

}
$my_path = "uploads/";
$my_name = 'Mike Venter';
$my_mail = 'mike@metabolictyping.co.za';
$my_replyto = 'mike@metabolictyping.co.za';
$my_subject = $subject_out;
$my_message = $message_out;
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: mike@metabolictyping.co.za";
// And now mail it!
if (mail($to, $my_subject, $my_message, $headers)) {
echo "<table border=1><tr><td><b>Thank you<br>Your order has been sent successfully. A consultant will contact you shortly</b></td></tr></table>"; // or use booleans here
    } else {
        echo "<table border=1><tr><td><b>mail send ... ERROR!</b></td></tr></table>";
    }
}
print $a;
  ?>
  
   </TR>
   <TR>
      <TD>
         
      </TD>
      <TD>
          
      </TD>
      <TD style='text-align:right'>
          
      </TD>
   </TR>
   
   </table><table width="100%">

      </TD>
   </TR>
   </table><table width="790">
   <TR>
      
      <TD style='border-top: 1pt solid gray' align="center" width="790">
         <INPUT TYPE='SUBMIT'  name='submit' value='Email Address Listing'></FORM>
      </TD>
   </TR>
</TABLE>
</table>





<?php } ?>

Link to comment
https://forums.phpfreaks.com/topic/143937-mail-with-attachments/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.