PHP mail with attachment - php

Hello i'm trying to send email from form^) Here is my code
<?php
$to = "some email";
$from = "site";
$subject = "Resume";
$boundary = "---";
if (isset($_POST['ok'])){
$filename = $_POST['fileField'];
$resumeLink = $_POST['linkField'];
$message = "Attachment: " .$resumeLink;
/* Headers*/
$headers = "From: $from\nReply-To: $from\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"";
$body = "--$boundary\n";
/* Add message */
$body .= "Content-type: text/html; charset='utf-8'\n";
$body .= "Content-Transfer-Encoding: quoted-printablenn";
$body .= "Content-Disposition: attachment; filename==?utf-8?B?".base64_encode($filename)."?=\n\n";
$body .= $message."\n";
$body .= "--$boundary\n";
$file = fopen($filename, "r"); //open file
$text = fread($file, filesize($filename)); //read file
fclose($file); //close file
/* Add message type */
$body .= "Content-Type: application/octet-stream; name==?utf-8?B?".base64_encode($filename)."?=\n";
$body .= "Content-Transfer-Encoding: base64\n";
$body .= "Content-Disposition: attachment; filename==?utf-8?B?".base64_encode($filename)."?=\n\n";
$body .= chunk_split(base64_encode($text))."\n";
$body .= "--".$boundary ."--\n";
if(mail($to, $subject, $body, $headers)){header("Location: /");}
}
?>
Attachment will send, but it size will be 1 byte without any content inside. Whats wrong?

It's hard to tell exactly, but you've added header fields into mail body, you should add them in headers

Related

Cannot set name to an ics file when sending it via php mail

I'm trying to set name of a file when i send it using mail() function, i send it but in gmail what i received is noname file without extension.
Could anyone help me please?
<?php
$temp = tmpfile();
fwrite($temp, $ical);
rewind($temp);
$to_email = 'test#gmail.com';
$subject = 'Testing PHP Mail';
$message = 'Hola caracola';
$from_email = 'test#test.com';
$path = stream_get_meta_data($temp)['uri'];
$num = md5(time());
$headers = "MIME-Version: 1.0\r\n"; // Defining the MIME version
$headers .= "From:".$from_email."\r\n"; // Sender Email
$headers .= "Reply-To: ".$from_email."\r\n"; // Email addrress to reach back
$headers .= "Content-Type: multipart/mixed;\r\n"; // Defining Content-Type
$body ="Content-Type: text/calendar; name=invite.ics\r\n";
$body .="Content-Disposition: attachment; filename=\"invite.ics\"\r\n";
$body .= $content; // Attaching the encoded file with email
mail($to_email,$subject,$body,$headers);
fclose($temp);
}
?>
Thankyou
I just made it,
I paste the code below, the problem is that php applies new policies to new lines headers so i just change a bit the code to avoid this limitation and solved the problem:
<?php
$temp = tmpfile();
fwrite($temp, $ical);
rewind($temp);
$to_email = 'test#gmail.com';
$subject = 'Testing PHP Mail';
$message = 'Hello';
$from_email = 'test#test.com';
$path = stream_get_meta_data($temp)['uri'];
$eol = PHP_EOL;
$filename = "invite.ics";
$mailto = $to_email;
$from_mail = $from_email;
$from_name = "Events";
$replyto = $from_email;
$file = $path;
$file_size = filesize($path);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$header = "From: ".$from_name." <".$from_mail.">".$eol;
$header .= "Reply-To: ".$replyto.$eol;
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"";
$message = "--".$uid.$eol;
$message .= "Content-Type: application/pdf; name=\"".$filename."\"".$eol; // use different content types here
$message .= "Content-Transfer-Encoding: base64".$eol;
$message .= "Content-Disposition: attachment; filename=\"".$filename."\"".$eol;
$message .= $content.$eol;
$message .= "--".$uid."--";
mail($mailto, $subject, $message, $header);
fclose($temp);
?>

PHP mail: attachement sent but won't open

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?

PHP mail attachment is not working

i am using php mail function to sent attachment, but its not working. Here is my code:
$headers = array(
"Mime-Version: 1.0",
"Content-Type: text/html; charset=charset=UTF-8",
"From: Test <myemail>",
"Content-Disposition: attachment; filename=\"" . $_SERVER['DOCUMENT_ROOT']."/project_name/test.txt" . "\"\r\n"; // For Attachment
);
$headers = join("\r\n", $headers);
mail($to, $subject, $body, $headers);
Email sent successfully and file is attached, but the content is hidden.
I recommend you to use PHPMailer.
To use PHPMailer:
Download the PHPMailer script from here: http://github.com/PHPMailer/PHPMailer
Extract the archive and copy the script's folder to a convenient place in your project.
Include the main script file -- require_once('path/to/file/class.phpmailer.php');
Now use as :
$email = new PHPMailer();
$email->From = 'you#example.com';
$email->FromName = 'Your Name';
$email->Subject = 'Message Subject';
$email->Body = $bodytext;
$email->AddAddress( 'destinationaddress#example.com' );
$file_to_attach = 'PATH_OF_YOUR_FILE_HERE';
$email->AddAttachment( $file_to_attach , 'nameOfFile.pdf' );
return $email->Send();
Your content type should be
"Content-Type: multipart/mixed; charset=charset=UTF-8",
see this answer, this can guide you solve your problem.
I also have a big doubt, if it is correct
"Content-Disposition: attachment; filename=\"" . $_SERVER['DOCUMENT_ROOT']."/project_name/test.txt" . "\"\r\n"; // For Attachment
combination of single and double quote does not seems to good. Can you please change it to
"Content-Disposition: attachment; filename=\" . $_SERVER['DOCUMENT_ROOT'] . "/project_name/test.txt" . "\"\r\n";
or try using single quote as you are not parsing any variable.
If you want to write your own code and don't want to implement the mail libraries. Please do it like this
$handle = fopen($file_name, "r");
$content = fread($handle, filesize($file_name));
fclose($handle);
$encoded_content = chunk_split(base64_encode($content));
$boundary = md5("foobar");
$message = "I have some message";
//header
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";
//plain text
$body = "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($message));
//attachment
$body .= "--$boundary\r\n";
$body .="Content-Type: $file_type; name=".$file_name."\r\n";
$body .="Content-Disposition: attachment; filename=".$file_name."\r\n";
$body .="Content-Transfer-Encoding: base64\r\n";
$body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
$body .= $encoded_content;
mail($to, $subject, $body, $headers);
$to = "YOUR EMAIL ID";
$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";
}

php mail() : attachments sent but not body text

I have seen similar questions, but nothing directly on topic...
I have a multi part mail script with attachments. The attachments get sent fine, but the main body text, which is populated from a form, isn't sent. I tried sending with the attachment function commented out and the form elements went through. My code is:
if (empty($_POST['RadioGroup1'])){
echo "PLease select a version of message";
} else {$selected_msg = $_POST['RadioGroup1'];}
if (isset($_FILES) && (bool) $_FILES){
$files = array();
// Check for attachments
$questions = $_POST['questions'];
//loop through all the files
foreach($_FILES as $name=>$file){
// define the variables
$file_name = $file['name'];
$temp_name = $file['tmp_name'];
//check if file type allowed
$path_parts = pathinfo($file_name);
//move this file to server
$server_file = "reports/$path_parts[basename]";
move_uploaded_file($temp_name, $server_file);
//add file to array of file
array_push($files,$server_file);
}
// define mail var
$to = $email;
$from = "[server]";
$subject = "Closed Case: $case_id $casename";
$headers = "From: $from";
//define boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Header know about boundary
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n";
$headers .= " boundary=\"{$mime_boundary}\"";
// Define plain text mail
$message .= "--{$mime_boundary}\n";
$message .= "Content-Type: text/html; charset=\"iso-8859-1\"\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n" . $message . "\r\n";
$message .= "--{$mime_boundary}\n";
// preparing attachments
foreach($files as $file){
$aFile = fopen($file, "rb");
$data = fread($aFile,filesize($file));
fclose($aFile);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"}\n";
$message .= " name=\"$file\"\n";
$message .= "Content-Disposition: attachment;\n";
$message .= " filename=\"$file\"\n";
$message .= "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}\n";
} // END foreach attachment
$message .= $questions;
$message .= $selected_msg;
$ok = #mail($to, $subject, $message, $headers);
if ($ok) {
echo "<p class=\"success\">mail sent to $to!</p>";
The script runs without errors and does sent the file attachments OR the body text. Any insight would be appreciated.
Instead of calling the form variables from within --- $message .= $questions;. $message .= $selected_msg; Hard code the variables into the $message.=
Like-- $message .= "Content-Transfer-Encoding: 7bit\r\n" . $questions . "\r\n" . $selected_msg . "\r\n";
If you like add both text and attachment in email, then used bellow code
$bodyMeaasge = 'Your Body Meaasge';
$filename = yourfile.pdf;
$path = '../';
$mpdf->Output($path.$filename,'F');
$file = $path . "/" . $filename;
$to = "senderemail#gmail.com";
$subject = "My Subject";
$random_hash = md5(date('r', time()));
$headers = "From:your#domain.com\r\n" .
"X-Mailer: PHP" . phpversion() . "\r\n" .
"MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $random_hash\r\n\r\n";
if(file_exists($file))
{
$content = file_get_contents($file);
$content = chunk_split(base64_encode($content));
//plain text
$body = "--$random_hash\r\n";
$body .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($bodyMeaasge));
//attachment
$body .= "--$random_hash\r\n";
$body .="Content-Type: application/octet-stream; name=".$filename."\r\n";
$body .="Content-Disposition: attachment; filename=".$filename."\r\n";
$body .="Content-Transfer-Encoding: base64\r\n";
$body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
$body .= $content;
}else{
//plain text
$body = "--$random_hash\r\n";
$body .= "Content-Type: text/html; charset=utf-8\r\n"; // use different content types here
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($bodyMeaasge));
}
if (mail($to,$subject,$body,$headers)) {
header("Location:".$url.'&success=successfully mail send.');
} else {
header("Location:".$url.'&error=There was a problem sending the email.');
}

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

Categories