I need some help here with a really weird situation.. i made a webmail form to send emails with multiple attachments, it worked out of the box yesterday but today it only sends 1 file as an attachment and not multiple. O_O
I revert my SVN to an older revision also and the problem still persists, any idea? this is really weird.. how working stuff brake from one day to another, i think the problem must be the SMTP server or something. Help please!
Code:
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$attachments = TRUE;
$body = "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" .
$body . "\n\n";
$body .= "--{$mime_boundary}\n";
$attach = array();
for($i=0; $i<count($_FILES['file']['name']);$i++)
{
$tmp_name = $_FILES['file']['tmp_name'][$i];
$type = $_FILES['file']['type'][$i];
$name = $_FILES['file']['name'][$i];
$size = $_FILES['file']['size'][$i];
if (file_exists($tmp_name)){
$kb = $size/1024;
$kbSize = round($kb*100)/100;
if(is_uploaded_file($tmp_name)){
$file = fopen($tmp_name,'rb');
$fdata = fread($file,filesize($tmp_name)); //stream file to var and send it in headers
fclose($file);
$fdata = chunk_split(base64_encode($fdata));
$body .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$name\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$name\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $fdata . "\n\n";
$body .= "--{$mime_boundary}-\n";
}
}
}
$sent = myMailer::sendAttached($from,$to,$subject,$body,$mime_boundary,$multi);
MyMailer class, sendAttached Method:
public static function sendAttached($from,$to,$subject,$body,$mime,$multi=FALSE)
{
$headers = 'From: Website <'.$from.'>'. "\r\n".
'Reply-To: '.$from. "\r\n".
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime}\"";
if($multi == TRUE && is_array($to)){
foreach($to as $item)
{
mail(trim($item),$subject,$body,$headers);
}
return TRUE;
}else{
return mail($to,$subject,$body,$headers);
}
}
Did you perhaps at some point move theo following outside the for loop:
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
Ive never tried to create a complex multipart message from scratch like this (i always use an existing library like Swift Mailer or Zend_Mail), but doesnt each file have to be its own part and thus have its own boundries?
Related
I have a script to email out attachments, but i cannot get the body of the email to be included it just sends blank
I have tried different charsets, UTF8 and the ISO8559
I tried
"Content-Type: text/html; charset=\"utf-8\n" .
"Content-Type: text/plaintext; charset=\"utf-8\n" .
But made no difference....
$name = "XXXXXXXX";
$email = "XXXXXX";
$to = "XXXXXXXXX";
$from = "XXXXXXXX";
$subject = "Thank you for your payment";
$mainMessage = "ANy text here to send in email";
$fileatt = "inv/".$invRef.".pdf";
$fileatttype = "application/pdf";
$fileattname = $invRef.".pdf";
$headers = "From: $from";
// File
$file = fopen($fileatt, 'rb');
$data = fread($file, filesize($fileatt));
fclose($file);
// This attaches the file
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$message = "" .
"-{$mime_boundary}\n" .
"Content-Type: text/html; charset=\"utf-8\n" .
"Content-Transfer-Encoding: 7bit\n" .
$mainMessage . "\n";
$data = chunk_split(base64_encode($data));
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatttype};\n" .
" name=\"{$fileattname}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileattname}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"-{$mime_boundary}-\n";
I have tried other mailers such as swift & simple external libraries but they caused me more headaches for other reasons
This has got me the closest to sending the email
The attachment works, the email sends, subject is fine.
Just nothing in the body
This php script works just fine sending my pdf file by e-mail.
The problem is the script doesn't send any message as specified in $mainMessage.
Why do that problem occur, with the script only sending the pdf file without any message?
// Settings
$name = "Name";
$email = "someome#anadress.com";
$to = "$name <$email>";
$from = "email#email.com";
$subject = "Here is your attachment";
$mainMessage = "Hi, here's the file.";
$fileatt = "test.pdf";
$fileatttype = "application/pdf";
$fileattname = "newname.pdf";
$headers = "From: $from";
// File
$file = fopen($fileatt, 'rb');
$data = fread($file, filesize($fileatt));
fclose($file);
// This attaches the file
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$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" .
$mainMessage . "\n\n";
$data = chunk_split(base64_encode($data));
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatttype};\n" .
" name=\"{$fileattname}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileattname}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"-{$mime_boundary}-\n";
// Send the email
if(mail($to, $subject, $message, $headers)) {
echo "The email was sent.";
}
else {
echo "There was an error sending the mail.";
}
Try using \r\n instead of your \ns. Also, I would suggest using a library for sending emails, such as PHPMailer or SwiftMailer, instead.
EDIT: There appears to be an extra (or missing) " when you declare your charset (at "Content-Type: text/plain; charset=\"iso-8859-1\n" - it outputs Content-Type: text/plain; charset="iso-8859-1).
you didn't notice that you need to complete this line:
$message = "This is a multi-part message in MIME format.\r\n" .
"--{$mime_boundary}\r\n" .
"Content-Type: text/plain; charset=utf-8 \r\n" .
"Content-Transfer-Encoding: 7bit\r\n" .
$mainMessage . "\r\n\r\n";
you are missing one "-" in "-{$mime_boundary}\r\n".
it should be as the example I wrote :3
I am trying to attach two files at maximum, to send them by mail, but the files are sent by mail as binary code, so when I open the received mail I found the files as binary, here is my code:
$files = array();
if(is_uploaded_file($_FILES['cv']['tmp_name']))
array_push($files, $_FILES['cv']);
if(is_uploaded_file($_FILES['portfolio']['tmp_name']))
array_push($files, $_FILES['portfolio']);
$subject = "Contact Mail";
$headers = 'From: '.$email_fromto."\r\n".
"subject: {$subject}";
$randomVal = md5(time());
$mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n" ;
$headers .= " boundary=\"{$mimeBoundary}\"";
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mimeBoundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
"From: $sex $fname $lname.\r\n".
"Message: {$message}";
$email_fromto = "mail#mail.com";
foreach($files as $userfile){
$tmpName = $userfile['tmp_name'];
$fileType = $userfile['type'];
$fileName = $userfile['name'];
if(file($tmpName)){
$file = fopen($tmpName,'rb');
$data = fread($file,filesize($tmpName));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "--{$mimeBoundary}\n" .
"Content-Type: {$fileType};\n" .
" name=\"{$fileName}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mimeBoundary}--\n";
}
}
so where is the error in what I did?
use like this
// generate a random string to be used as the boundary marker
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
// now we'll build the message headers
$headers = "From: $from\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";
// here, we'll start the message body.
// this is the text that will be displayed
// in the e-mail
$message="This is an example"
foreach($_FILES as $userfile){
// store the file information to variables for easier access
$tmp_name = $userfile['tmp_name'];
$type = $userfile['type'];
$name = $userfile['name'];
$size = $userfile['size'];
// if the upload succeded, the file will exist
if (file_exists($tmp_name)){
// check to make sure that it is an uploaded file and not a system file
if(is_uploaded_file($tmp_name)){
// open the file for a binary read
$file = fopen($tmp_name,'rb');
// read the file content into a variable
$data = fread($file,filesize($tmp_name));
// close the file
fclose($file);
// now we encode it and split it into acceptable length lines
$data = chunk_split(base64_encode($data));
}
}
// now we'll insert a boundary to indicate we're starting the attachment
// we have to specify the content type, file name, and disposition as
// an attachment, then add the file content.
// NOTE: we don't set another boundary to indicate that the end of the
// file has been reached here. we only want one boundary between each file
// we'll add the final one after the loop finishes.
$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";
}
// here's our closing mime boundary that indicates the last of the message
$message.="--{$mime_boundary}--\n";
// now we just send the message
if (#mail($to, $subject, $message, $headers))
echo "Mail was Send Sucessfully";
else
echo "Failed to send";
can anyone give me some idea how to attach a file from the server and send it as attachment through email??
i have the following code:
<?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 .= "\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
// 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>";
}
?>
but this code does not attach file from the server.
please help me out.. thanks in advance..!!
One question: Do you want to send e-mails with attachment or do you want to improve your PHP skills with this problem?
If you only want to send mails, have a look at PHPMailer which is very good and easily to handle. For myself I'm using this for years without problems.
For improving your skills: You say that the attachment is encoded in base64, but I cannot find the line where you do something like $data = base64_encode($data); You add the $data of your attachment in plain to the mail.
I have a function to send an email with multiple word documents as attachments. Only problem is that no matter how many documents I send, only 1 shows up in the email, and also, it shows up as corrupted. The file path name is correct, I have tested this, but when I try to open the document from the email, Microsoft word complains about corrupted file. Somebody please tell me what I am doing wrong?
function mail_attachment_multiple($to, $subject, $message, $attachment, $from){//sends email as an attachment. attachment parameter is the path to file
$fileatt_type = "application/msword"; // File Type
$email_from = $from; // Who the email is from
$email_subject = $subject; // The Subject of the email
$email_txt = $message; // Message that the email has in it
$email_to = $to; // Who the email is to
$headers = "From: ".$email_from;
$msg_txt="\n\n You have recieved a new attachment message from $from";
$email_message = "";
//loop through array: $key = filename; $value = filenamePATH
foreach($attachment as $key => $value){
$fileatt_name = $key; //name of file
$fileatt_path = $value; // file path (http://www.example.com/filePath)
$file = fopen($fileatt_path,'rb');
$temp = get_headers($fileatt_path, 1);
$file_size = $temp['Content-Length'];
$data = fread($file, $file_size); //filesize($fileatt_path)
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$email_txt .= $msg_txt;
$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_txt . "\n\n";
$data[$key] = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_path}\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" ."--{$mime_boundary}--\n";
}
return #mail($email_to, $email_subject, $email_message, $headers);?>
Thanks for the help!
You should really use a mail library for this sort of thing. I highly recommend SwiftMailer.