Php Mail Attachment error - php

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.

Try this...
I have add example code for attach file to send in mail
<?php
function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
$file = $path.$filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($file);
$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";
$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."--";
if (mail($mailto, $subject, "", $header)) {
echo "mail send ... OK"; // or use booleans here
} else {
echo "mail send ... ERROR!";
}
}
?>
If you send zip file try to use following
<?php $my_file = "filename.zip";
$my_path = $_SERVER['DOCUMENT_ROOT']."/path/";
$my_name = "resr";
$my_mail = "test#resr.com";
$my_replyto = "test#test.com";
$my_subject = "This is a mail with attachment.";
$my_message = "Hallo,\r\ndo you like this script? I hope it will help.\r\n\r\ngr. Olaf";
mail_attachment($my_file, $my_path, "recipient#mail.org", $my_mail, $my_name, $my_replyto, $my_subject, $my_message);
?>

Related

email shows html tags it`s not appear in html format

I am sending email using editor but it`s also send the html tags and i want to show only html view and This is my email attachment code --
$file = 'uploads/email_attachments/'.$row['email_image'];
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$from_name = "Tino Cesar";
$from_mail = "info#tino.co.in";
$replyto = $row['email'];
$message = $row['message'];
$header = "From: ".$name." <".$email.">\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";
$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";
$headers .= "Content-type:text/html;charset=UTF-8" . "\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."--";
$mailto = $row['email'];
$subject = $row['subject'];
if (mail($mailto, $subject, "", $header)) {
echo "mail send ... OK"; // or use booleans here
} else {
echo "mail send ... ERROR!";
}
change this
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
to
$header .= "Content-type:text/html;charset=UTF-8" . "\r\n";
remove this line
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";

Email Attachments using php mail() function - Working for gmail but not working for yahoo

I have used the below code for sending the mail with attachments. It's receiving the mail fine for us in gmail account but in yahoo account that the mail is not received. Please refer the below code,
<?php
$my_file = "fb_icon_hover.png";
$my_path = $_SERVER['DOCUMENT_ROOT']."/poc/mailChecking/";
$my_name = "ETU";
//$my_mail = "abibith#gmail.com";
$my_mail = "abibith#gmail.com";
//$my_replyto = "arul.it02#gmail.com";
$mail_data = file_get_contents($_SERVER['DOCUMENT_ROOT']."/poc/mailChecking/postajob_mailcontent.html");
$mail_data = str_replace('{COMPANY_NAME}',"Testing Company", $mail_data);
$mail_data = str_replace('{JOB_NAME}',"Testing jobname", $mail_data);
$mail_data = str_replace('{EMAIL}',"abibith#gmail.com", $mail_data);
$mail_data = str_replace('{JOB_POSITION}',"Software Engineer", $mail_data);
$mail_data = str_replace('{JOBTYPE}',"Programmer", $mail_data);
$mail_data = str_replace('{PHONE}',"04132660407", $mail_data);
$mail_data = str_replace('{ADDRESS}',"Testing Address", $mail_data);
$mail_data = str_replace('{CITY}',"Testing City", $mail_data);
$mail_data = str_replace('{STATE}',"Testing State", $mail_data);
$mail_data = str_replace('{POSTCODE}',"605004", $mail_data);
$mail_data = str_replace('{JOB_DESCRIPTION}',"This is a testing Job", $mail_data);
/*echo "====>".$mail_data;
die();*/
$my_subject = "This is a mail with attachment.";
//$my_message = "Hallo,\r\ndo you like this script? I hope it will help.\r\n\r\ngr. Olaf";
$my_message = $mail_data;
mail_attachment($my_file, $my_path, "abibith#gmail.com", $my_mail, $my_name, $my_subject, $my_message);
function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $subject, $message) {
$file = $path.$filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($file);
$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";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/html; 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."--";
if (mail($mailto, $subject, "", $header)) {
echo "mail send ... OK"; // or use booleans here
} else {
echo "mail send ... ERROR!";
}
}
?>
So kindly assist our request and do the needful.
Thanks,
Arularasan D,
Senior Programmer.
$to = "myemail#mydomain.com";
$from = "Website <website#mydomain.com>";
$subject = "Test Attachment Email";
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// attachment name
$filename = "document.pdf";
//$pdfdoc is PDF generated by FPDF
$attachment = chunk_split(base64_encode($pdfdoc));
// main header
$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
// no more headers after this, we start the body! //
$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$body .= "This is a MIME encoded message.".$eol;
// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;
// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";
// send message
if (mail($to, $subject, $body, $headers)) {
echo "mail send ... OK";
} else {
echo "mail send ... ERROR";
}
Had same problem. But later formed code like in upper example (thanks Alin Purcaru for it) and problem was solved. Hope it helps mate )

Unable to send html mail with an attachment

Php code used:
$boundary = uniqid("np");
$header = "MIME-Version: 1.0\r\n" ;
$header .= "From: $from\r\n";
$header .= "To: $to\r\n";
if (!is_null($reply_to)) $header .= "Reply-To: $reply_to \r\n";
$header .= "Content-Type: multipart/mixed;boundary=" . $boundary . "\r\n";
//here is the content body
$message = "This is a multi part message in MIME format.\n\n";
$message .= "--" . $boundary . "\n";
$message .= "Content-type: text/html;charset=utf-8\n\n";
$message .= "Hello, <BR> This is a text email, the <bold>html version</bold>.";
$message .= "<BR>Regards";
$message .= "<BR>Your Name";
$message .= "\n\n--" . $boundary . "\n";
$file_name = "file_01.txt";
$file_content = "Ciao - file 1";
$chunked_content = chunk_split(base64_encode($file_content));
$message .= "Content-Type: application/octet-stream; name=\"$file_name\"\n";
$message .= "Content-Disposition: attachment; filename=\"$file_name\"\n";
$message .= "Content-Transfer-Encoding: base64\n\\n" . $chunked_content . "\n\n";
$message .= "--{$boundary}--";
Results: HTML Mail IS OK, there is the attached file with right filename but it's empty
What's wrong ?
Edit: ! tried to replace every \n with \r\n but the results is an empty mail..
I use the following code for email attachments. Try this
function mailWithAttachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message)
{
$file = $path.$filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($file);
$header = "From: ".$from_name." <".$from_mail.">\n";
$header .= "Reply-To: ".$replyto."\n";
$header .= "MIME-Version: 1.0\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\n\n";
$header .= "This is a multi-part message in MIME format.\n";
$header .= "--".$uid."\n";
$header .= "Content-type:text/html; charset=iso-8859-1\n";
$header .= $message."\n\n";
$header .= "--".$uid."\n";
$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\n"; // use different content types here
$header .= "Content-Transfer-Encoding: base64\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\n\n";
$header .= $content."\n\n";
$header .= "--".$uid."--";
if(mail($mailto, $subject, "", $header))
{
return 1;
}
else
{
return 0;
}
}

why my email printing HTML

i am sending a attachment with email with PHP .
It is going fine. but i wants to send some details of users.
The detail is printing in HTML instead of message.
$my_file = 'attach.txt';
$my_path = "path/".$file_name;
$my_name = "noreply#mail.com ";
$my_mail = "noreply#mail.com";
$my_replyto = "noreply#mail.net";
$my_subject = "test E-Mail";
$my_message = htmlspecialchars_decode("<html><body><center><table width='400px' height='400px'><th>career</th><tr><td>Name</td><td>My name</td></tr><tr><td>Email</td><td>My Email</td></tr><tr><td>Post</td><td>My Post</td></tr><tr><td>Address</td><td>My Address</td></tr></table> </center><br><br></body></html>");
$mailto = "myemail#gmail.com";
$content = chunk_split(base64_encode(file_get_contents($my_path)));
$uid = md5(uniqid(time()));
$name = basename($my_path);
$header = "From: ".$my_mail."\r\n";
$header .= "Reply-To: ".$my_replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$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 .= $my_message."\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/octet-stream; name=\"".$my_file."\"\r\n";
// use different content types here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$my_file."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
if (mail($mailto, $my_subject, '', $header)) {
echo "email send";
exit;
} else {
echo "Error during mail send.!";
exit;
}
change
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
with
$header .= "Content-type:text/html; charset=iso-8859-1\r\n";

PHP Mail, CC Field

How can I include a CC field in this function?
function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
$file = $path.$filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($file);
$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";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: text/html; charset=iso-8859-1\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."--";
if (mail($mailto, $subject, "", $header)) {
echo "OK"; // or use booleans here
} else {
echo "ERROR!";
}
}
Add this while declaring headers:
$headers .= 'Cc: somebody#example.com' . "\r\n";
Add $header .= "CC: ".$cc."\r\n"; after the Reply-To-line.
http://www.w3schools.com/PHP/func_mail_mail.asp
Please don't build your own MIME emails. Use something like PHPMailer instead. Far easier to use and less brittle. Adding a CC with that is as simple as:
$mail = new PHPMailer();
$mail->AddCC('somebody#example.com');

Categories