I am using php mail() function with attachment using Content-Type: multipart/mixed header but it always return noname.txt
Here is my codes:
$upload_name=$_FILES["upload"]["name"];
$upload_type=$_FILES["upload"]["type"];
$upload_size=$_FILES["upload"]["size"];
$upload_temp=$_FILES["upload"]["tmp_name"];
$subject = "Subject";
$to="example#example.com";
$message="message";
$fp = fopen($upload_temp, "rb");
$file = fread($fp, $upload_size);
$file = chunk_split(base64_encode($file));
$num = md5(time());
//Normal headers
$headers = "From: Info Mail<example#example.com>\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; ";
$headers .= "boundary=".$num."\r\n";
$headers .= "--$num\r\n";
// This two steps to help avoid spam
$headers .= "Message-ID: <".gettimeofday()." TheSystem#".$_SERVER['SERVER_NAME'].">\r\n";
$headers .= "X-Mailer: PHP v".phpversion()."\r\n";
// With message
$headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
$headers .= "".$message."\n";
$headers .= "--".$num."\n";
// Attachment headers
$headers .= "Content-Type:".$upload_type." ";
$headers .= "name=\"".$upload_name."\"r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; ";
$headers .= "filename=\"".$upload_name."\"\r\n\n";
$headers .= "".$file."\r\n";
$headers .= "--".$num."--";
// SEND MAIL
$flgchk=#mail($to, $subject, $message, $headers);
fclose($fp);
In Mail Inbox It Returns:
Thanks In Advance...
Remove the last boundary, finish the mail with the last attachment string only, this removes the noname.txt in gmail.
Your code is perfect
change the code here.
In Attachment headers
Replace
$headers .= "Content-Type:".$upload_type." ";
with
$headers .= "Content-Type: multipart/mixed; ";
$header = "From: Info Mail<example#example.com>\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$num."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$num."\r\n";
$header .= "Content-type:text/html; charset=UTF-8\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$num."\r\n";
$header .= "Content-Type: application/octet-stream; name=\"".$upload_name."\"\r\n"; // use different content types here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$fname."\"\r\n\r\n";
$header .= $file."\r\n\r\n";
$header .= "--".$num."--";
I used the above headers for mails which seemed to work.
$upload_name=$_FILES["your_file_input_name"]["name"];
$upload_type=$_FILES["your_file_input_name"]["type"];
$upload_size=$_FILES["your_file_input_name"]["size"];
$upload_temp=$_FILES["your_file_input_name"]["tmp_name"];
$subject = "Your Subject";
$to="example#example.com";
$message="Your Message";
$fp = fopen($upload_temp, "rb");
$file = fread($fp, $upload_size);
$file = chunk_split(base64_encode($file));
$num = md5(time());
//Normal headers
$headers = "From: Info Mail<example#example.com>\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; ";
$headers .= "boundary=".$num."\r\n";
$headers .= "--$num\r\n";
// This two steps to help avoid spam
$headers .= "Message-ID: <".gettimeofday()." TheSystem#".$_SERVER['SERVER_NAME'].">\r\n";
$headers .= "X-Mailer: PHP v".phpversion()."\r\n";
// With message
$headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
$headers .= "".$message."\n";
$headers .= "--".$num."\n";
// Attachment headers
$headers .= "Content-Type: application/".$upload_type." ";
$headers .= "name=\"".$upload_name."\"r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; ";
$headers .= "filename=\"".$upload_name."\"\r\n\n";
$headers .= "".$file."\r\n";
$headers .= "--".$num."--";
// SEND MAIL
$flgchk=#mail($to, $subject, $message, $headers);
fclose($fp);
if($flgchk)
{
echo 'Sent Successfully';
}
else
{
echo 'Unexpected error occurred!!! Please try again.';
}
Related
What's wrong in my code?
Mail sending fine and text also fine but the attachment showing like noname.txt
Please help.
$upload_name=$_FILES["resume"]["name"];
$upload_type=$_FILES["resume"]["type"];
$upload_size=$_FILES["resume"]["size"];
$upload_temp=$_FILES["resume"]["tmp_name"];
$file = $upload_temp;
$content = chunk_split(base64_encode(file_get_contents($file)));
$num = md5(uniqid(time()));
$email_to = "noname#example.com";
$email_subject = $name . " Applied for a job in website careers page";
$email_message = "<b>Form details below.</b><br />";
$email_message .= "<tr><td><strong>Last Name</strong> </td><td>".$name."</td></tr>";
$email_message .= "<tr><td><strong>Phone</strong> </td><td>".$phone."</td></tr>";
$headers .='Reply-To: '. $email_to . "\r\n" ;
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$num."\"\r\n\r\n";
$headers .= "This is a multi-part message in MIME format.\r\n";
$headers .= "--".$num."\r\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
$headers .= $email_message."\r\n\r\n";
$headers .= "--".$num."\n";
$headers .= "Content-Type:".$upload_type."; name=\"".$upload_name."\"\r\n\r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; filename=\"".$upload_name."\"\r\n\r\n";
$headers .= $content."\r\n\r\n";
$headers .= "--".$num."--";
if(#mail($email_to, $email_subject, "", $headers)){
echo "Mail Send";
}
else {
echo "Mail not sent.";
}
Please help. Thanks in advance.
Have you consider using PHPMailer? In my experience, saves yourself from a lot of these hassles.
Otherwise, try changing:
$headers .= "Content-Type:".$upload_type.";name=\"".$upload_name."\"\r\n\r\n";
To:
$headers .= "Content-Type: multipart/mixed; name=\"".$upload_name."\"\r\n\r\n";
Am trying to create a mail form with attachment where the reply address is different from the received address. They receiving emails works properly but if you decide to reply to them, it adds mime-version to the end of the reply email. (ex: email#gmail.commime-version)
See code below.
<?php
// Email address to which you want to send email
$to = $_POST["gut"];
$subject = $_POST["fieldSubject"];
$message = nl2br($_POST["fieldDescription"]);
/*********Creating Uniqid Session*******/
$txtSid = md5(uniqid(time()));
$headers = "";
$headers .= "From: ".$_POST["fieldFormName"]."<".$_POST["fieldFormus"].">\nReply-To: ".$_POST["fieldFormEmail"]."";
$headers .= "MIME-Version: 1.0" . "\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$txtSid."\"\n\n";
$headers .= "This is a multi-part message in MIME format.\n";
$headers .= "--".$txtSid."\n";
$headers .= "Content-type: text/html; charset=utf-8\n";
$headers .= "Content-Transfer-Encoding: 7bit\n\n";
$headers .= $message."\n\n";
/***********Email Attachment************/
if($_FILES["attachment"]["name"] != "")
{
$txtFilesName = $_FILES["attachment"]["name"];
$txtContent = chunk_split(base64_encode(file_get_contents($_FILES["attachment"]["tmp_name"])));
$headers .= "--".$txtSid."\n";
$headers .= "Content-Type: application/octet-stream; name=\"".$txtFilesName."\"\n";
$headers .= "Content-Transfer-Encoding: base64\n";
$headers .= "Content-Disposition: attachment; filename=\"".$txtFilesName."\"\n\n";
$headers .= $txtContent."\n\n";
}
// # is for skiping Errors //
$flgSend = #mail($to,$subject,null,$headers);
if($flgSend)
{
echo 'Your email as being sent successFully.';
}
else
{
?>
What happens if you change:
$headers .= "From: ".$_POST["fieldFormName"]."<".$_POST["fieldFormus"].">\nReply-To: ".$_POST["fieldFormEmail"]."";
To
$headers .= "From: ".$_POST["fieldFormName"]."<".$_POST["fieldFormus"].">\nReply-To: ".$_POST["fieldFormEmail"]."\n";
Notice the \n at the end of the line.
I was testing a PHP Form for my client, when I realized that file attachment wasn´t optional, I have the full code here (it´s pretty long). Form works fine, but if you don´t choose any file, the e-mail will never arrive to it´s destination. I want to send the mail anyway and receive it without the attach.
I´m using PHP and jForm for validation.
Here is the input tag for file attaching (there to many input tags at the form.php file to list them here)
<input type="file" name="adjunto" id="adjunto">
And here is the send.php file (the true form is too long to publish here, i´m writing a shorter version of it)
<?php
// THE USER CHOOSES FROM A JOB LIST
$busqueda = $_POST["busqueda-list"];
$cargo = $_POST["cargo-list"];
// FILLS PERSONAL INFORMATION
$nombre = $_POST["nombre"];
$apellido = $_POST["apellido"];
$sexo = $_POST["sexo-list"];
// WE STYLE A MESSAGE WITH LOTS OF INPUTS
$mensaje = 'In the real form here is a table with lots of input styled';
// AND THEN WE ATTACH (in spanish spells adjunto)
$adjunto_name=$_FILES["adjunto"]["name"];
$adjunto_type=$_FILES["adjunto"]["type"];
$adjunto_size=$_FILES["adjunto"]["size"];
$adjunto_temp=$_FILES["adjunto"]["tmp_name"];
// IF THERE IS A FILE ATTACHED
if (is_uploaded_file($adjunto_temp))
{if($adjunto_type=="application/octet-stream" or $adjunto_type=="text/plain" or $adjunto_type=="application/msword" or $adjunto_type=="application/vnd.openxmlformats-officedocument.wordprocessingml.document" or $adjunto_type=="application/pdf" )
{
// MEET THE HEADERS
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "From: ".$email."\n";
// MAIL HEADERS with attachment
$fp = fopen($adjunto_temp, "rb");
$file = fread($fp, $adjunto_size);
$file = chunk_split(base64_encode($file));
$num = md5(time());
// Normal headers
$headers = "From: ".$email."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; ";
$headers .= "boundary=".$num."\r\n";
$headers .= "--$num\r\n";
// This two steps to help avoid spam
$headers .= "Message-ID: <".gettimeofday()." TheSystem#".$_SERVER['SERVER_NAME'].">\r\n";
$headers .= "X-Mailer: PHP v".phpversion()."\r\n";
// With mensaje
$headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
$headers .= "".$mensaje."\n";
$headers .= "--".$num."\n";
// Attachment headers
$headers .= "Content-Type:".$adjunto_type." ";
$headers .= "name=\"".$adjunto_name."\"r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; ";
$headers .= "filename=\"".$adjunto_name."\"\r\n\n";
$headers .= "".$file."\r\n";
$headers .= "--".$num."--";
// WE SEND MAIL WITH HEADERS
#mail("name#example.com", "".$busqueda." ".$cargo."", $mensaje, $headers);
fclose($fp);
}
// IF NOT, JUST SEND MAIL
else
#mail("ezemosho#gmail.com", "".$busqueda." ".$cargo."", $mensaje);
}
?>
Please I need a hand with this, I´m doing my best (i´ve already search here and other forums) and I think this maybe a problem many people had before!
Thank you!
you need to give one more condition on top
if (is_uploaded_file($adjunto_temp)) {
if($adjunto_type=="application/octet-stream" or $adjunto_type=="text/plain" or $adjunto_type=="application/msword" or $adjunto_type=="application/vnd.openxmlformats-officedocument.wordprocessingml.document" or $adjunto_type=="application/pdf" )
{
........... // your other stuff with respect to headers
#mail("name#example.com", "".$busqueda." ".$cargo."", $mensaje, $headers);
}
}
else
#mail("name#example.com", "".$busqueda." ".$cargo."", $mensaje);
now the mail will go on both conditions.
UPDATED WITH YOUR CODE
if (is_uploaded_file($adjunto_temp))
{
if($adjunto_type=="application/octet-stream" or $adjunto_type=="text/plain" or $adjunto_type=="application/msword" or $adjunto_type=="application/vnd.openxmlformats-officedocument.wordprocessingml.document" or $adjunto_type=="application/pdf" )
{
// MEET THE HEADERS
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "From: ".$email."\n";
// MAIL HEADERS with attachment
$fp = fopen($adjunto_temp, "rb");
$file = fread($fp, $adjunto_size);
$file = chunk_split(base64_encode($file));
$num = md5(time());
// Normal headers
$headers = "From: ".$email."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; ";
$headers .= "boundary=".$num."\r\n";
$headers .= "--$num\r\n";
// This two steps to help avoid spam
$headers .= "Message-ID: <".gettimeofday()." TheSystem#".$_SERVER['SERVER_NAME'].">\r\n";
$headers .= "X-Mailer: PHP v".phpversion()."\r\n";
// With mensaje
$headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
$headers .= "".$mensaje."\n";
$headers .= "--".$num."\n";
// Attachment headers
$headers .= "Content-Type:".$adjunto_type." ";
$headers .= "name=\"".$adjunto_name."\"r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; ";
$headers .= "filename=\"".$adjunto_name."\"\r\n\n";
$headers .= "".$file."\r\n";
$headers .= "--".$num."--";
// WE SEND MAIL WITH HEADERS
#mail("name#example.com", "".$busqueda." ".$cargo."", $mensaje, $headers);
fclose($fp);
}
}
// IF NOT, JUST SEND MAIL
else
{
// MAIL HEADERS
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "From: ".$email."\n";
// This two steps to help avoid spam
$headers .= "Message-ID: <".gettimeofday()." TheSystem#".$_SERVER['SERVER_NAME'].">\r\n";
$headers .= "X-Mailer: PHP v".phpversion()."\r\n";
#mail("ezemosho#gmail.com", "".$busqueda." ".$cargo."", $mensaje);
}
Put the else statement for your first if condition, in your code you are using else for the condition2.
if (condition1) {
if (condition2) {
.........
.........
}
} else {
}
With this code:
$Message = 'TEXT';
$boundary = "---";
$headers = "From: $from\nX-Mailer: Carline Server www.carline.ru";
$headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"";
$body = "--$boundary\n";
$body .= "Content-type: text/html; charset='utf-8'\n";
$body .= "Content-Transfer-Encoding: quoted-printable\n\n";
$body .= "Content-Disposition: attachment; filename==?utf-8?B?".base64_encode($_FILES['photo1']['name'])."?=\n\n";
$body .= $Message."\n";
$body .= "--$boundary\n";
$text = file_get_contents($_FILES['photo1']['tmp_name']);
$body .= "Content-Type: application/octet-stream; name==?utf-8?B?".base64_encode($_FILES['photo1']['name'])."?=\n";
$body .= "Content-Transfer-Encoding: base64\n";
$body .= "Content-Disposition: attachment; filename==?utf-8?B?".base64_encode($_FILES['photo1']['name'])."?=\n\n";
$body .= chunk_split(base64_encode($text))."\n";
$body .= "--".$boundary ."--\n";
mail($AdminEmail, "RE:", $body, $headers);
It sends a file-content in the message body
Content-Type: application/octet-stream; name==?utf-8?B?MDAwMDU3ODcuanBn?=
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename==?utf-8?B?MDAwMDU3ODcuanBn?=
/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcG
BwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz/2wBDAQICAgMDAwYDAwYMCAcIDAwMDAwM
DAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAz/wAARCAGQAZADASIA
...
How to change this code for sending file as attachment?
From php.net # http://php.net/manual/en/function.mail.php
Send Multi attachment email
<?php
function multi_attach_mail($to, $files, $sendermail){
// email fields: to, from, subject, and so on
$from = "Files attach <".$sendermail.">";
$subject = date("d.M H:i")." F=".count($files);
$message = date("Y.m.d H:i:s")."\n".count($files)." attachments";
$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 = "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
// preparing attachments
for($i=0;$i<count($files);$i++){
if(is_file($files[$i])){
$message .= "--{$mime_boundary}\n";
$fp = #fopen($files[$i],"rb");
$data = #fread($fp,filesize($files[$i]));
#fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"".basename($files[$i])."\"\n" .
"Content-Description: ".basename($files[$i])."\n" .
"Content-Disposition: attachment;\n" . " filename=\"".basename($files[$i])."\"; size=".filesize($files[$i]).";\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
}
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $sendermail;
$ok = #mail($to, $subject, $message, $headers, $returnpath);
if($ok){ return $i; } else { return 0; }
}
?>
An example of header construction
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
// content type: mixed or related ( better mixed for attachments
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";
// use different content types here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
You should be using pre-written libraries (e.g. http://swiftmailer.org/) to send emails, esp. the ones with attachments, whether inline or not.
Constructing valid and cross-mail client supported message is considerably advance task, esp. if you have never done this before. So why spend time re-inventing a weel when someone did the job for you already?
Put this question from the other end and if you want to build your own library, analyze what's already been done in SwiftMailer and use those snippets where needed.
I run PHP on IIS6. I have some PHP that successfully sends a 1KB image as an attachment on an email. When I try and attach a 500KB PDF however (having changed the Content-Type), it hangs and after a few minutes I get "FastCGI process exceeded configured request timeout" (Error Number 258 (0x80070102)).
Any thoughts on why it's taking so long to attach the PDF? The solution is not to increase the timeout limit, I can't have users sitting there for 3+ minutes while the file gets sent.
I've included my code below:
$headers = "From: ".$from."\r\n";
$headers .= "Reply-To: ".$from."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$headers .="This is a multipart message in MIME format. \r\n\r\n";
$headers .= "--".$uid."\r\n\r\n";
$headers .= "Content-Type: text/plain; charset-iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: quoted-printable\r\n\r\n";
$headers .= $text . "\r\n\r\n";
$headers .= "--".$uid."\r\n\r\n";
$headers .= "Content-Type: text/html; charset-iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$headers .= $html . "\r\n\r\n";
$headers .= "--".$uid."\r\n\r\n";
$headers .= "Content-Type: image/png; name=\"".$filename."\"\r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$attachment = chunk_split(base64_encode(file_get_contents($path.$filename)));
$headers .= $attachment . "\r\n\r\n";
$headers .= "--".$uid."\r\n\r\n";
//send the email
$mail_sent = #mail( $to, $subject, $text, $headers );
Thanks in advance for any advice.
Put the attachment in the message parameter of the mail() function instead of the additional headers parameter.
I ran into the same problem today and found that I couldn't submit large files as part of the headers parameter in the mail() function.
e.g.
$headers = "From: ".$from."\r\n";
$headers .= "Reply-To: ".$from."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$body .="This is a multipart message in MIME format. \r\n\r\n";
$body .= "--".$uid."\r\n\r\n";
$body .= "Content-Type: text/plain; charset-iso-8859-1\r\n";
$body .= "Content-Transfer-Encoding: quoted-printable\r\n\r\n";
$body .= $text . "\r\n\r\n";
$body .= "--".$uid."\r\n\r\n";
$body .= "Content-Type: text/html; charset-iso-8859-1\r\n";
$body .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$body .= $html . "\r\n\r\n";
$body .= "--".$uid."\r\n\r\n";
$body .= "Content-Type: image/png; name=\"".$filename."\"\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n";
$body .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$attachment = chunk_split(base64_encode(file_get_contents($path.$filename)));
$body .= $attachment . "\r\n\r\n";
$body .= "--".$uid."\r\n\r\n";
//send the email
$mail_sent = #mail( $to, $subject, $body, $headers );