When I am going to submit this (Warning: file() [function.file]: Filename cannot be empty) warning shows. How can I solve this problem. Please help I am new in php. Email has been sent but files doesn't attach. I want to sending mail with attach with raw php.
may code is look like this:
$tmpName = $_FILES['attachment']['tmp_name'];
$fileType = $_FILES['attachment']['type'];
$fileName = $_FILES['attachment']['name'];
/* Start of headers */
$headers = "From: $fromName";
if (file($tmpName)) {
/* Reading file ('rb' = read binary) */
$file = fopen($tmpName,'rb');
$data = fread($file,filesize($tmpName));
fclose($file);
/* a boundary string */
$randomVal = md5(time());
$mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";
/* Header for File Attachment */
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n" ;
$headers .= " boundary=\"{$mimeBoundary}\"";
/* Multipart Boundary above message */
$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";
/* Encoding file data */
$data = chunk_split(base64_encode($data));
/* Adding attchment-file to message*/
$message .= "--{$mimeBoundary}\n" .
"Content-Type: {$fileType};\n" .
" name=\"{$fileName}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mimeBoundary}--\n";
}
echo $message;
$sendmail = mail ("$to", "$subject", "$message", "$headers");
if($sendmail){
echo "A email has been sent to: $to";
}
else{
echo "Error in Email sending";
}
}
if (file($tmpName)) {
You probably want if (file_exists($tmpName)) {?
Also make sure $tmpName is actually filled out.
Related
I am generating pdf and sent it through email as attachment the code is given below for this i skipped some code for generating pdf (not needed).
Please check the code:
//pdf generated not given avobe code of how it comes
$pdfdoc = $pdf->Output("confirmation-".$client_code.".pdf", "S");
$attachment = chunk_split(base64_encode($pdfdoc));
$to = $mail_to;
$from = "From: <info#ksilbd.com>";
$subject = "Here is your attachment";
$mainMessage = "BUY/SALE CONFIRMATION";
$fileatt = $attachment;
$fileatttype = "application/pdf";
$fileattname = "confirmation-".$client_code.".pdf";
$headers = "From: $from";
$file = fopen($fileatt, 'rb');
$data = fread($file, filesize($fileatt));
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}\"";
$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";
//base_64
//encoding used
//to encode data
$data = chunk_split(base64_encode($data));
//message
//concat the
//message
$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 "EMAIL SENT SUCCESSFUL.";
}
else {
echo "There was an error sending the mail.";
The message showed email sent successful and i got email too but i failed to open the attached pdf file with the mail ....
please check the problem
You may use "phpmailer".
Download the PHPMailer script from here: http://github.com/PHPMailer/PHPMailer
and Enjoy it.
I am sending a multi-part email with php with html formatting and an attachment...
All is working fine but when the user receiving that email from our website it is showing the boundary as well in last of the HTML part.
But when i remove the boundary line the email showing file contents as base64 (no attachment).
if ( isset($_POST['submit']) )
{
$to = 'admin#abc.com';
$FromSubmitting = $_POST['submitting'];
$FromTitle = $_POST['title'];
$FromName = $_POST['name'];
$FromDesignation = $_POST['designation'];
$FromQualification = $_POST['qualification'];
$FromInstitute = $_POST['institute_address'];
$FromHomeAddress = $_POST['home_address'];
$FromEmail = $_POST['email'];
$FromWorkPhone = $_POST['work_phone_number'];
$FromHomePhone = $_POST['home_phone_number'];
$FromMobilePhone = $_POST['mobile_phone_number'];
$FromPresentingAuthor = $_POST['presenting_author'];
$FromConferenceTheme = $_POST['conference_theme'];
$FromTypeOfSubmission = $_POST['type_of_submission'];
$subject = "New ".$FromSubmitting." Submitted From: ".$FromName;
/* GET File Variables */
$tmpName = $_FILES['uploaded_file']['tmp_name'];
$fileType = $_FILES['uploaded_file']['type'];
$fileName = $_FILES['uploaded_file']['name'];
/* Start of headers */
$headers = "From: $FromEmail";
if (file($tmpName)) {
/* Reading file ('rb' = read binary) */
$file = fopen($tmpName,'rb');
$data = fread($file,filesize($tmpName));
fclose($file); /* a boundary string */
$randomVal = md5(time());
$mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";
/* Header for File Attachment */
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n" ;
$headers .= " boundary=\"{$mimeBoundary}\"";
/* Multipart Boundary above message */
$message .= "--{$mimeBoundary}\n"
. "Content-Type: text/html; charset=UTF-8\n"
. "Content-Transfer-Encoding: 8bit\n\n"
. "<table>
<tr><td>Submission Type:</td><td>$FromSubmitting</td></tr>
<tr><td>Name:</td><td>$FromTitle $FromName</td></tr>
<tr><td>Designation:</td><td>$FromDesignation</td></tr>
<tr><td>Qualification:</td><td>$FromQualification</td></tr>
<tr><td>Institute:</td><td>$FromInstitute</td></tr>
<tr><td>Home Address:</td><td>$FromHomeAddress</td></tr>
<tr><td>Work Phone:</td><td>$FromWorkPhone</td>
<td>Home Phone:</td>
<td>$FromHomePhone</td><td>Mobile Phone:</td>
<td>$FromMobilePhone</td></tr>
<tr><td>Presenting Author:</td><td>$FromPresentingAuthor</td></tr>
<tr><td>Conference Theme:</td><td>$FromConferenceTheme</td></tr>
<tr><td>Type of Submission:</td><td>$FromTypeOfSubmission</td></tr>
</table>"
. "--{$mimeBoundary}\n"; // when removing this line it shows Image-2
/* Encoding file data */
$data = chunk_split(base64_encode($data));
/* Adding attchment-file to message*/
$message .= "--{$mimeBoundary}\n"
. "Content-Type: {$fileType};\n"
. " name=\"{$fileName}\"\n"
. "Content-Transfer-Encoding: base64\n\n"
. $data . "\n\n"
. "--{$mimeBoundary}--\n";
}
$flgchk = mail ("$to", "$subject", "$message", "$headers");
}
Email Screen Shot Image-1 :
Email Screen Shot Image-2 :
I am not sure but you can try this.
/* Adding attchment-file to message*/
$message .= "--{$mimeBoundary}\n"
."Content-Type: application/octet-stream;\n"
. " name=\"{$fileName}\"\n"
. "Content-Transfer-Encoding: base64\n\n"
. $data . "\n\n"
. "--{$mimeBoundary}--\n";
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";
My file is attached and sent with no error, but the attachment in my Inbox folder shows as follows:
// this is included in the email subject and body, so not correct
file attach boundary="==Multipart_Boundary_xb46dc57fb3a58f8a859f39992cfd954ex"
This is a multi-part message in MIME format.
// this is included in the email subject and body, so not correct
--==Multipart_Boundary_xb46dc57fb3a58f8a859f39992cfd954ex
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
// this is included in the email subject and body, so not correct
test message
--==Multipart_Boundary_xb46dc57fb3a58f8a859f39992cfd954ex
Content-Type: image/jpeg;
name="23.jpg"
Content-Transfer-Encoding: base64
/9j/4AAQSkZJRgABAQEAYABgAAD/4QC8RXhpZgAASUkqAF4AAAAEABoBBQABAAAAPgAAABsBBQAB
AAAARgAAACgBAwABAAAAAgAAADEBAgAQAAAATgAAAAAAAABgAAAAAQAAAGAAAAABAAAAUGFpbnQu
TkVUIHYzLjA4AAUAGgEFAAEAAAA+AAAAGwEFAAEAAABGAAAAKAEDAAEAAAACAAAAMQECABAAAAB
My sendemail.php code is:
<?php
$headers= "MIME-Version: 1.0";
$headers= "Content-type: text/plain; charset=iso-8859-1";
$headers= "From: $_POST[name] <$_POST[email]>";
$headers= $_POST['name'];
$subject = $_POST['subject'];
$message = $_POST['message'];
/* GET File Variables */
$tmpName = $_FILES['attachment']['tmp_name'];
$fileType = $_FILES['attachment']['type'];
$fileName = $_FILES['attachment']['name'];
if (file_exists($tmpName)) {
/* Reading file ('rb' = read binary) */
$file = fopen($tmpName,'rb');
$data = fread($file,filesize($tmpName));
fclose($file);
/* a boundary string */
$randomVal = md5(time());
$mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";
/* Header for File Attachment */
$headers = "\nMIME-Version: 1.0\n";
$headers = "Content-Type: multipart/mixed;\n" ;
$headers = " boundary=\"{$mimeBoundary}\"";
/* Multipart Boundary above message */
$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";
/* Encoding file data */
$data = chunk_split(base64_encode($data));
/* Adding attchment-file to message*/
$message .= "--{$mimeBoundary}\n" .
"Content-Type: {$fileType};\n" .
" name=\"{$fileName}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mimeBoundary}--\n";
}
$to_user = "nur_Selam#yahoo.com";
$flgchk = mail($to_user, $subject, $message, $headers);
//require_once ('./includes/functions_mail.php');
if ($flgchk) {
echo "<META HTTP-EQUIV=\"refresh\" CONTENT=\"2;url=forwardtoother.php?page=sender.html\">";
echo "Done, <br>";
echo "Redirecting you to your Inbox";
}
else
{
echo "<META HTTP-EQUIV=\"refresh\" CONTENT=\"2;url=forwardtoother.php?page=sender.html\">";
echo "Failed to send email, <br>";
echo "Redirecting you to your Inbox";
}
?>
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.