How to send an email with doc and pdf files attachments (multiple attachments)

Hi,

I urgently need help here.

Currently, I am developing an email system similar to the Yahoo! email system.

I managed to send email using this code:

<?php
function send_email($to_addr,$cc_addr,$bcc_addr,$subject,$m
essage)
{
//input variables
$from_addr = “blah@yahoo.com”;
$headers = "MIME-Version: 1.0\r
";
$headers .= "Content-type: text/html; charset=iso-8859-1\r
";
$headers .= "Cc: $cc_addr\r
";
$headers .= "Bcc: $bcc_addr\r
";

$path_to_sendmail = “/usr/sbin/sendmail”;
$fp = popen(“$path_to_sendmail -Am -t”, “w”);

$num = fputs($fp, "From: $from_addr
");
$num = fputs($fp, "To: $to_addr
");

$num += fputs($fp, "Subject: $subject
");
$num += fputs($fp, "$headers

");
$num += fputs($fp, "$message

");

pclose($fp);

//return infomation
if ($num > 0) { echo “Your mail has been sent out!”; }
else {Sorry there was a problem to send email!"; }?>

My challenge now is to attach doc and pdf files and send them together with the email message.

I observed the Yahoo! email system and see how users can attach files. Basically the attach file button, the browse for file buttons (multiple attachments) and then done button when the file has been successfully attached.

I need help on how to perform these very similar processes that eventually attach the files to the email message and send them to the recipients.

  1. How do I create the browse for file feature
  2. How do I attach multiple files
  3. How do I know the size of the files
  4. How do I remove the files
    5 How do I eventually send the files with the email message

Pardon me for my limited knowledge for php. :bawling:

I am very new to php and I hope to get some guidance here.

Thank you for reading!

Have a Merry Christmas!

Regards,
Junk
:eek2:

  1. <input type=“file” />
  2. <input type=“file” name=“file1” /><input type=“file” name=“file2” /> etc.
  3. It’s all in the $_FILES superglobal. print_r($_FILES) and have a look at Handling file uploads
  4. Remove from where? THey’re removed from the temp upload location at the end of script.
  5. This is a harder one. I use the PEAR mail functions myself.

Hi,

Thanks for your advise.

Currently I am trying a sample for sending email with attachment (shown below):

mail.php:

[COLOR=Sienna]<html>
<head>
<title> Sending Email </title>
</head>
<body>
<?php
// Read POST request params into global vars
$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”;

if (is_uploaded_file($fileatt)) {
// 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 .= "
MIME-Version: 1.0
" .
“Content-Type: multipart/mixed;
" .
" boundary=\”{$mime_boundary}\“”;

// Add a multipart boundary above the plain message
$message = "This is a multi-part message in MIME format.

" .
"–{$mime_boundary}
" .
"Content-Type: text/plain; charset=\“iso-8859-1\”
" .
"Content-Transfer-Encoding: 7bit

" .
$message . "

";

// Base64 encode the file data
$data = chunk_split(base64_encode($data));

// Add file attachment to the message
$message .= “–{$mime_boundary}
" .
“Content-Type: {$fileatt_type};
" .
" name=\”{$fileatt_name}\”
" .
//“Content-Disposition: attachment;
" .
//” filename=\“{$fileatt_name}\”
" .
"Content-Transfer-Encoding: base64

" .
$data . "

" .
"–{$mime_boundary}–
";
}

// 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>”;
}
?>
</body>
</html>[/COLOR]

index.html:

<html>
<head>
<title>Send an Email</title>
</head>
<body>
<h1>Send an Email</h1>
<form action=“mail.php” method=“POST” enctype=“multipart/form-data”>
<p>To: <input type=“text” name=“to” value=“” /><br />
From: <input type=“text” name=“from” value=“” /><br />
Subject: <input type=“text” name=“subject” value=“” /></p>
<p>Message:<br />
<textarea cols=“70” rows=“20” name=“message”></textarea></p>
<p>File Attachment: <input type=“file” name=“fileatt” /></p>
<p><input type=“submit” value=“Send” /></p>
</form>
</body>
</html>

Don’t know why is it not working…Mail is send, but I didn’t receive any mails in my Yahoo! mailbox…

Waiting for your kind advise. Thanks!

FYI this code is from http://www.sitepoint.com/article/advanced-email-php/5

Merry Christmas! :blush:

Well, that’s a different question and I advised you in your other thread to contact your host.