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);
}
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'";
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";
I am trying to send files as attachment through php mail function. My code is sending the files but it is showing size 0KB. I already google it but didn't found a solution. Please help me.
HTML:
<form method="post" action="code.php" enctype="multipart/form-data">
<input type="file" name="file[]" multiple="multiple" />
<button type="submit">Submit</button>
</form>
PHP:
<?php
foreach ($_FILES['file']['name'] as $filename){
$files[] = $filename; // create array of terms
}
// email fields: to, from, subject, and so on
$to = "receiver_email_here";
$from = "sender_email_here";
$subject ="My subject";
$message = "My message";
$headers = "From: $from";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart 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" . $message . "\n\n";
$message .= "--{$mime_boundary}\n";
// preparing attachments
for($x=0;$x<count($files);$x++){
$file = fopen($files[$x],"rb");
$data = fread($file,filesize($files[$x]));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}\n";
}
mail($to, $subject, $message, $headers);
?>
I think I am doing something wrong in this part but I am not sure.
for($x=0;$x<count($files);$x++){
$file = fopen($files[$x],"rb");
$data = fread($file,filesize($files[$x]));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}\n";
}
Thanks
You are trying to open $_FILES['file']['name'], but the name field contains the name of the original file, not the path to the file on the server. To access the file on the server, use
$_FILES['file']['tmp_name']
Also to make sure that the file was uploaded correctly, use
$_FILES['file']['error'] == UPLOAD_ERR_OK
I am making a piece of code for a website and made the following code, however i need to put html in the email however it doesn't send it, it only attaches the image, please help?
<?php
$emailSubject = 'Application - '.$_POST['full_name'].'';
$webMaster = 'email#webaddress.com';
$from = $_POST["email"];
$tmpName = $_FILES['attachment']['tmp_name'];
$fileType = $_FILES['attachment']['type'];
$fileName = $_FILES['attachment']['name'];
$headers = "From: $fromName";
if (file($tmpName)) {
$file = fopen($tmpName,'rb');
$data = fread($file,filesize($tmpName));
fclose($file);
$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" .
$message . "\n\n";
$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";
}
$html_message = "<html>"
$email_message = mail($webMaster, $emailSubject, $message, $headers);
$result = 'Result Code Here On Page';
echo "$result";
?>
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.