PHP mail: attachement sent but won't open - php

The mail works well and I receive it in my mailbox and I receive the attachment too but I can't opent it, and I'm really in a situation that I can't use any library like SwiftMailer or PHPMailer so I need to make this work.
This is my code:
function mail_attachment($to, $subject, $msg, $from, $file) {
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode(file_get_contents($file)));
$uid = md5(uniqid(time()));
$eol = PHP_EOL;
// Basic headers
$header = "From: Equipe Grifoflex <".$from.">".$eol;
$header .= "Reply-To: ".$to.$eol;
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"";
// Put everything else in $message
$message = "--".$uid.$eol;
$message .= "Content-Type: text/html; charset=ISO-8859-1".$eol;
$message .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$message .= $msg.$eol;
$message .= "--".$uid.$eol;
$message .= "Content-Type: application/pdf; name=\"".$file."\"".$eol;
$message .= "Content-Transfer-Encoding: base64".$eol;
$message .= "Content-Disposition: attachment; filename=\"".$file."\"".$eol;
$message .= $content.$eol;
$message .= "--".$uid."--";
return mail($to, $subject, $message, $header);
}
What am I missing here?

Related

php mail with attachment and html

I have been trying to get mail to send html emails however it seems that whenever i change the header from
$message .= "Content-type:text/plain; charset=iso-8859-1\r\n"; to
$message .= "Content-type:text/html; charset=iso-8859-1\r\n";
the email does not arrive.When using text/plain, i receive the email however the html is not rendered, in other words i received the code rather than formatted html. Changing to text/html, the email is not received at all. I have tried using UTF-8 but can not seem to solve it after hours of searching. Any help would be appreciated.
function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $emessage) {
$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";
$message = "--".$uid."\r\n";
$message .= "Content-type:text/plain; charset=iso-8859-1\r\n";
//$message .= "Content-Type: text/html; charset=\"UTF-8\""."\r\n";
//$message .= "Content-type:text/html; charset=UTF-8\r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$message .= $emessage."\r\n\r\n";
$message .= "--".$uid."\r\n";
$message .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";
$message .= "Content-Transfer-Encoding: base64\r\n";
$message .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$message .= $content."\r\n\r\n";
$message .= "--".$uid."--";
return mail($mailto, $subject, $message, $header);
}

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";

Php Mail Attachment error

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);
?>

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;
}
}

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