date_default_timezone_set('America/Los_Angeles');
// Create a file with data to send
$file_name = "summary.txt";
$myfile = fopen($file_name, "w") or die("Unable to open file!");
$txt = "PHP Mail function example with attachment" . PHP_EOL;
fwrite($myfile, $txt);
fclose($myfile);
$filetype = filetype($file_name);
//read from the uploaded file & base64_encode content for the mail
$content = file_get_contents($file_name);
$encoded_content = chunk_split(base64_encode($content));
$boundary = md5("php-mail");
$from_email = 'from_email@gmail.com'; //sender email
$recipient_email = 'recipient_email@gmail.com'; //recipient email
$subject = 'Test mail'; //subject of email
$message = 'This is body of the message'; //message body
//header
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From:".$from_email."\r\n";
$headers .= "Reply-To: ".$from_email."\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: $filetype; 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;
$sentMail = mail($recipient_email, $subject, $body, $headers);
if($sentMail) //output success or failure messages
{
die('Thank you for your email');
} else {
die('Could not send mail! Please check your PHP mail configuration.');
}
No comments:
Post a Comment