I'm trying to send an email with an attachment in PHP 8 and, whilst it sends the email with the attachment okay, the attachments all come out as corrupted for the recipient. I've tried jpg, png and pdf and they all come out corrupted.
Worked fine with PHP 7.4 and previous but doesn't seem to work with PHP 8.
What has changed?
Here is the code -
Note: the other mail variables $recipient_email,$subject,$message are set prior to this code.
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
$tmp_name1 = $_FILES['attachment']['tmp_name'];
$type1 = $_FILES['attachment']['type'];
$name1 = $_FILES['attachment']['name'];
$size1 = $_FILES['attachment']['size'];
if (file_exists($tmp_name1)){
if(is_uploaded_file($tmp_name1)){
$file1 = fopen($tmp_name1,'rb');
$data1 = fread($file1,filesize($tmp_name1));
fclose($file1);
$data1 = chunk_split(base64_encode($data1));
}
$headers = "From: $myname <$mymail>\r\n" .
"Reply-To: <$mymail>\r\n" .
"Return-Path: <$mymail>\r\n";
"X-Mailer: PHP\r\n" .
"Disposition-Notification-To: ".$mymail." <$bcc>\r\n";
$headers .= "MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";
$body = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"utf-8\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
$body .= "--{$mime_boundary}\n" .
"Content-Type: {$type1};\n" .
" name=\"{$name1}\"\n" .
$data1 . "\n\n" .
"Content-Transfer-Encoding: base64\n\n";
$body .= "--{$mime_boundary}--\n";
mail($recipient_email,$subject,$body,$headers,"-f ".$mymail);
DOH, silly me.I had two lines of code transposed.
Should be this at the end -
"Content-Transfer-Encoding: base64\n\n" .
$data1 . "\n\n";
Instead of -
$data1 . "\n\n" .
"Content-Transfer-Encoding: base64\n\n";
Related
I am using following code for sending email with attachment but the proper file is not getting attach with mail.
$UnidID = $_COOKIE['UniqueID'];
$guid = $_COOKIE['guid'];
$target_path = "userdata/".$UniqueID."/".$iGuid."/Outputs";
$fname = getpathmail($UnidID,$guid);
$target_path = $target_path.$filname;
$fileatt_type = "application/fbf"; // File Type
$fileatt_name = $fname;
$data = $target_path;
$email_from = "EHPAdmin#fugro.in";
$email_subject = "EHP/PPP process";
$email_message = "Processed result for EHP/PPP processing";
$email_to = $_GET['Email'] ;
$headers = "From: ".$email_from;
$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_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_message .= "\n\n";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data .= "\n\n" .
"--{$mime_boundary}--\n";
$ok = #mail($email_to, $email_subject, $email_message, $headers);
I'd suggest to use a library for sending eMails as it will handle all the header related stuff. Have a look at Zend_Mail. Sending attachments is as easy as
$mail = new Zend_Mail();
$at = $mail->createAttachment($myImage);
$at->type = 'image/gif';
$at->disposition = Zend_Mime::DISPOSITION_INLINE;
$at->encoding = Zend_Mime::ENCODING_8BIT;
$at->filename = 'test.gif';
$mail->send();
You need to put all of your multipart message into $email_message:
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers = "From: ".$email_from;
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$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" .
"--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
chunk_split(base64_encode($data)) .
"--{$mime_boundary}--\n";
May be you missed this in your code
...
$file = fopen($data,'rb');
//saves content in $data itself
$data = fread($file,filesize($data));
fclose($file);
...
should work havn't executed myself.
give it a try
You should give your attachment file as:
"Content-Disposition: attachment; filename='/path/to/the/file/filename'";
Text mail works fine but I want to attach file too.
Attachment is not working.
My code is:
<form method="post" enctype="multipart/form-data">
<input name="filepc" type="file" id="filepc" class="listEm" />
</form>
if (isset($_POST['submit'])) {
$attachments = '';
if (!empty($_FILES['filepc']['tmp_name'])) {
$attachments = array($_FILES['filepc']['name']);
}
headers = "From:" . $your_email . " < " . $email . ">" . "\r\n";
$headers .= "Reply - To:" . $ct_email . "\r\n";
$headers .= "Content-Disposition: attachment; filename=\"".$attachments."\"\r\n\r\n";
//$headers = "Bcc: someone#domain . com" . "\r\n";
$headers = "X - Mailer: PHP / " . phpversion();
mail($to , $subject , $msg , $headers,$attachments);
$successMsg = '<h5 style="color:red;">Sent successfully</h5>';
// }
}
Above code works but file is not attached. Please help me find the solution for this problem.
if(!empty($_FILES['resume']['name'])) {
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
$tmp_name = $_FILES['resume']['tmp_name'];
$type = $_FILES['resume']['type'];
$file_name = $_FILES['resume']['name'];
$size = $_FILES['resume']['size'];
// Check to make sure that it is an uploaded file and not a system file
if(is_uploaded_file($tmp_name)){
// Now Open the file for a binary read
$file = fopen($tmp_name,'rb');
// Now read the file content into a variable
$data = fread($file,filesize($tmp_name));
// close the file
fclose($file);
// Now we need to encode it and split it into acceptable length lines
$data = chunk_split(base64_encode($data));
}
$mybody = "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" .
$mybody . "\n\n";
$headers = "From: $from\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";
$mybody .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$file_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
$bodys .= "$mybody <br>";
$subject = "Attachment";
$body = $body . $bodys;
mail($to, $subject, $body, $headers);
}
I've got this little bump...
I'm trying to send bcc copies but when I increase the number of headers the attached files are sent as the images code.
Here is my code:
$to="name#extension.com";
$subject="Petición de financiamiento";
$from = stripslashes($_POST['nombre'])."<".stripslashes($_POST['correo']).">";
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
$headers = "From: $from\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";
$message="Petición de financiamiento\r\n";
$message .="\r\n";
$message .= "Nombre: ".$_POST["nombre"]."\r\n";
$message .= "Correo: ".$_POST["correo"]."\r\n";
$message .= "Teléfono: ".$_POST["telef"]."\r\n";
$message .="\r\n";
$message .="\r\n";
$message .= $_POST["mensaje"];
$message .="\r\n";
$message .="\r\n";
$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";
foreach($_FILES as $userfile){
$tmp_name = $userfile['tmp_name'];
$type = $userfile['type'];
$name = $userfile['name'];
$size = $userfile['size'];
if (file_exists($tmp_name)){
if(is_uploaded_file($tmp_name)){
$file = fopen($tmp_name,'rb');
$data = fread($file,filesize($tmp_name));
fclose($file);
$data = chunk_split(base64_encode($data));
}
$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";
}
}
$message.="--{$mime_boundary}--\n";
$message.="--{$mime_boundary}--\n";
if (#mail($to, $subject, $message, $headers))
echo "Mensaje enviado";
else
echo "No se pudo enviar";
Any Idea how to add the BCC's, thank you in advance for any help.
Just put $headers .= "Bcc: $emailadress\r\n"; say after the Content-type line
that should work i guess.
$headers = "From: $from\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";
On your headers just add this line
$headers.= "Bcc: email#example.com" . "\r\n";
Check the php mail function
http://php.net/manual/en/function.mail.php
[Edited to fit the 'real' solution Sam Ram San have found by himself]
A nice, clear and expandable way to manage headers (including bcc):
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: $from";
$headers[] = "Bcc: $bcc_address1,$bcc_address2,$bcc_address3";
// and so on...
mail($to, $subject, $email, implode("\r\n", $headers));
Using this you avoid including unwanted blank spaces in front of the header field names ("Bcc" -> correct. " Bcc" -> incorrect)
Edited: as a general rule when sending emails, multiple addresses (in from, cc, bcc, cco... fields) should be separated using commas sign (","). I.e: $bcc="john#doe.com,james#doe.org,lucy#doe.com"
All I needed to do was to use a csv on the:
$to="name#extension.com,mail2#bla.com";
Thank You guys.
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
The code below works perfectly well when I change the attached file to an html file,but when I change the attached file to an image i.e. screenshot.png it fails to send the message.
<?php
$file_path = "screenshot.png"; // server path where file is placed
$file_path_type = "image/png"; // File Type
$file_path_name = "screenshot.png"; // this file name will be used at reciever end
$from = "xyz#gmail.com"; // E-mail address of sender
$to = "abc#gmail.com"; // E-mail address of reciever
$subject = "Please check the Attachment."; // Subject of email
$message = "This is the message body.<br><br>Thank You!<br><a href='http://7tech.co.in'>7tech.co.in Team</a>";
$headers = "From: ".$from;
$file = fopen($file_path,'rb');
$data = fread($file,filesize($file_path));
fclose($file);
$rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$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/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message .= "\n\n";
$data = chunk_split(base64_encode($data));
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$file_path_type};\n" .
" name=\"{$file_path_name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$file_path_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data .= "\n\n" .
"--{$mime_boundary}--\n";
if(#mail($to, $subject, $message, $headers)) {
echo "File send!";
} else {
echo 'Failed';
}
?>
Can you guys point out the error.I've tried to cahnge content type too at 1-2 places but it wasn't working.Am I missing anything?
It can occur due to a misconfiguration in your webserver. By changing the allowed filesize, maybe it will work.