Download a file from web Server - php

I have a web running on a server and I manage several files. I want users can download a certain files generated but I can't force to download this files, and I tried several ways to do it. Mails with files attached (3 different ways) or by forcing the file download through the web browser... Nothing happens.
I have this configuration in JS (jQuery) to PHP script:
$.ajax({
type: "POST",
url: "./download_file.php",
data: {download_file: jsonFilename},
cache: false,
success: function(data){
alert(data);
}
});
In PHP force download:
$file = json_decode($_POST['download_file']);
header("Content-type: application/octet-stream");
header("Content-disposition: attachment; filename=" . $file);
readfile("./uploads/subtitles/" . $file);
In PHP PHPMailer:
require_once('./PHPMailer-master/class.phpmailer.php');
$file = json_decode($_POST['download_file']);
$bodytext = 'Here is your file!';
$email = new PHPMailer();
$email->From = 'admin#mail.com';
$email->FromName = 'Web Administrator';
$email->Subject = 'Download';
$email->Body = $bodytext;
$email->AddAddress( 'user#mail.co' );
$file_to_attach = './path/' . $file;
$email->AddAttachment( $file_to_attach , $file );
return $email->Send();
Also I tried this other way in 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()));
$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!";
}
}
$file = json_decode($_POST['download_file']);
$bodytext = 'Here is your file';
//start editing and inputting attachment details here
$my_file = $file;
$my_path = "./path/";
$my_name = "Web Administrator";
$my_mail = "admin#mail.com";
$my_replyto = "admin#mail.com";
$my_subject = "whatever";
$my_message = $bodytext;
mail_attachment($my_file, $my_path, "user#mail.co", $my_mail, $my_name, $my_replyto, $my_subject, $my_message);
I only want user gets his file, but nothing seems to work by the moment.
My web is running on a CentOS server and web-server lighthttpd.
Thank you

Related

How to attach pdf in mail of html content in mail on form submit using php?

I want to attach a pdf file of html content when form submit.
Html form is made with css so pdf is generated with css.
generated pdf has attached in mail on form submit.
you can use this function to send email. I hope you have created file to attach so you can pass file name i fuction.
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()));
$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";
$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."--";
// Messages for testing only, nobody will see them unless this script URL is visited manually
if (mail($mailto, $subject, "", $header)) {
echo "Message sent!";
} else {
echo "ERROR sending message.";
}
}
If you are using phpmailer fuction so this code will help you to send pdf file:
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "smtp.server.net";
$mail->SMTPAuth = true;
$mail->Username = "username#domain.com";
$mail->Password = "password1";
$mail->From = "username#domain.com";
$mail->FromName = "Software something";
$mail->AddAddress("targetguy#domain.com", "Target Guy");
$mail->AddReplyTo("username#domain.com", "Software Simian");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = "Subject";
$mail->Body = "Message";
$mail->AltBody = strip_tags("Message:);
$mail->AddAttachment("filename.pdf");
if(!$mail->Send()){
$resultstatus = 'Failed';
}

send attachment in php mail

I want to send a file in a link which want to get download.I linked the path of the file in anchor tag.But I doesn't get downloaded.That file gets open in next page without downloading.I want to download a file in anchor tag.I want to download in from a link instead of attachment.
move_uploaded_file($_FILES['resume'] ['tmp_name'],'resume/'.$_FILES['resume'][name]);
$url='resume/'.$_FILES['resume']['name'];
$from = $email;
$to="websoftbms#gmail.com";
$headers1 = "From: $from\n";
$headers = "From: $email\r\n";
$headers .= "Reply-To: websoftbms#gmail.com\r\n";
$headers .= "Return-Path: sathurka.mca#gmail.com\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$body = "
Hello,<br>
This mail is sent via blumounts.com<br>
Name:$user<br>
Email:$email<br>
Subject:$subject<br>
message:$message<br>
resume :<a href='//domain.com/website/$url' download>Download</a> <br>
";
$body.="<br>
Thank you,<br>
$user<br>";
if( $sentmail = mail( $to,"Sent via career form.", $body, $headers ))
{
echo '<script>
window.alert("Email sent");
window.reload();
</script>';
}
Try this...
The mail() function doesn’t support attachment or HTML mail by default. You need to use different headers and MIME mail parts to make this possible. Many shared hosting providers doesn’t allow the usage of this function and it might be disabled.
Normally you will pass three values to the mail() function plus some headers. In the example below I skip the value for the message value, because the message is defined as a MIME part together with the attachment.
<?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()));
$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!";
}
}
$my_file = "file.extension";
$my_path = "/your_path/to_the_attachment/";
$my_name = "Olaf Lederer";
$my_mail = "my#mail.com";
$my_replyto = "my_reply_to#mail.net";
$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);
Download PHPMailer from here
Create a PHP test file :
<?php
include_once("phpmailer/class.phpmailer.php");
$mail = new PHPMailer () ;
$mail->IsSMTP () ;
// UPDATED CODE -->>
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = // your gmail address "user3386779#gmail.com"
$mail->Password = // your gmail password "passwordUser3386779!"
// <<-- UPDATED CODE
// if you want to format your message body wih HTML Tags
$mail->IsHTML ( true ) ;
$mail->From = $sender_s ;
$mail->Subject = $subject_s ;
$mail->Body = $mail_body_lt ;
// -- Rc : you can loop to add multiple receivers ....
$mail->AddAddress (trim($rc_s));
// -- Cc : you can loop to add multiple receivers ....
$mail->AddCC (trim($cc_s));
// -- Attach file
if (file_exists($attached_files_s) !== TRUE) {
sprintf("file: %s doesn't exist.", $attached_files_s);
}
else {
// Attachement: you can loop to attach multiple files ....
$mail->AddAttachment($attached_files_s);
}
// -- sending mail and catch errors
if ( ! $mail->send () ) {
return $mail->ErrorInfo ;
}
Instead of linking to the real file, make a link to a PHP page with the filename as an argument, and add this header :
// $mimetype is the mimetype of your file.
// You may force it, or use finfo functions to get it :
// http://php.net/manual/fr/function.finfo-file.php
header('Content-type: '.$mimetype);
header('Content-Disposition: attachment; filename="'.$file.'"');
You may of course have to adapt the paths.

I am using following code to send form data to email along with attachment but it only send attachment not data filled in form

I am using following code to send form data to email along with attachment but it only send attachment not data filled in form.
I also want to set the upload limit to 4mb
Any idea which could help me?
<?php
if(isset($_POST['submit']))
{
$name1=$_POST['name'];
$email=$_POST['email'];
$mobile=$_POST['mob'];
$applyfor= $_POST['applyfor'];
$address= $_POST['Address'];
if(is_uploaded_file($_FILES['resume']['tmp_name']))
{
$path = $_FILES['resume']['tmp_name'];
$filename = $_FILES['resume']['name'];
$file = $path;
$file_size = $_FILES['resume']['size'];
$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: $name1<$email>"."\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."--";
$message = $name1."\n" .$email."\n" .$mobile."\n" .$applyfor."\n" .$address;
$mailto = "uttamking#gmail.com";
$subject = "New resume Receive";
if (mail($mailto, $subject, $message, '', $header)) {
echo "mail send ... OK"; // or use booleans here
} else {
echo "mail send ... ERROR!";
}
}
}
?>

PHPMail sending attachment but they are empty

Basically, I am trying to send a PDF via PHPMail.
the email is sent and I receive it in outlook perfectly. the problem is that the attachment is broken and it doesnt open. I even tried sending a HTML and is also empty.
I tried researching in the forum, tried several "working codes" and other people got it working with this code... I have no clue why is not working for me.
I am using the lastest version of PHPMail 5.2.2
$mail = new PHPMailer();
$staffEmail = "staffemail";
$mail->From = $staffEmail;
$mail->FromName = "name";
$mail->AddAddress('my#email.com');
$mail->AddReplyTo($staffEmail, "name");
$mail->AddAttachment('test.pdf');
$mail->Subject = "PDF file attachment";
$mail->Body = "message!";
$mail->Send();
This custom PHP function will show the (beginning) PHP developer how-to build an e-mail script with attachment function. Please note that inside the mail function is no validation functionality available.
<?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!";
}
}
?>
Next we show an example on how-to use this function to send an e-mail message with one attached zip file:
<?php $my_file = "somefile.zip";
$my_path = $_SERVER['DOCUMENT_ROOT']."/your_path_here/";
$my_name = "Olaf Lederer";
$my_mail = "my#mail.com";
$my_replyto = "my_reply_to#mail.net";
$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);
?>
Are you looking for a script to send multiple attachments? Try our mail attachment class script.
If you like to send your websites mail messages via SMTP and Gmail, check our PHPMailer tutorial, too.
I suggest you to upload the file to the server and then attach it to the email. For example, you can use the following steps :
Upload the file
Attach it to the email
Send the email
Delete this temporary file from the server
Try this because I think thats the reason of your poblem.
Just in case: http://php.net/manual/en/features.file-upload.php
Good luck
There can be two problems either path is not correct for attachment or php dont have permission on that folder
this one is working for me trying to send a resume has attachmnet in zend
$mail = new Zend_Mail ();
$mail->setBodyHTML ( stripslashes ($message) );
// add attachment
$fileContents = file_get_contents($attachemnet);
$resume = $mail->createAttachment($fileContents);
$resume->filename = $EmployeeDeatils['resume'];
//$mail->createAttachment($attachemnet);
$mail->setFrom ( $mail_template ['from_email'], $mail_template ['from_caption'] );
$mail->addTo ( $clientemail, $employee_name );
$mail->setSubject ($subject );
try {
$mail->send ();
} catch ( Exception $e ) {
$this->_helper->errorlog ( " Send mail to member with activation link : " . $e->getMessage () );
}
You can try use this function which uses simple mail function of PHP:
function mail_attachment ($from , $to, $subject, $message, $attachment){
$fileatt = $attachment; // Path to the file
$fileatt_type = 'application/octet-stream'; // File Type
$start= strrpos($attachment, '/') == -1 ? strrpos($attachment, '//') : strrpos($attachment, '/')+1;
$fileatt_name = substr($attachment, $start, strlen($attachment)); // Filename that will be used for the file as the attachment
$email_from = $from; // Who the email is from
$email_subject = $subject; // The Subject of the email
$email_txt = $message; // Message that the email has in it
$email_to = $to; // Who the email is to
$headers = "From: ".$email_from;
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$msg_txt=""; //\n\nMail created from websiteaddress systems : http://websiteaddress.com
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nwebsiteaddress-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$email_txt .= $msg_txt;
$email_message = "This is a multi-part message in websiteaddress format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n".$email_txt. "\n\n";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
$ok = #mail($email_to, $email_subject, $email_message, $headers);
if($ok) {
//echo "Attachment has been mailed !";
//header("Location: index.php");
} else {
die("Sorry but the email could not be sent. Please go back and try again!");
}
}
Hope this will help you.

Email Client doesn't open attachment but will download and open

I'm using PHP to send an attachment in an email, all works as expected except in the email client (with other email attachments) I can just click on it and it would launch the external application to view the file, or at least give me an option to select a program to try and view it. I'm not getting this as nothing happens when I click on the attachment. I can download it and view it and that works as expect.
Wanted to know if I'm missing something in the header.
Here is my function (it's in a class):
public function mail() {
if(!empty($this->attachment)) {
$filename = empty($this->attachment_filename) ? basename($this->attachment) : $this->attachment_filename;
$path = dirname($this->attachment);
$mailto = $this->to;
$from_mail = $this->from;
$from_name = $this->from_name;
$replyto = $this->reply_to;
$subject = $this->subject;
$message = $this->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);
$mime_type = $this->getMimeType($file); // function returns the MIME type
$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: ".$mime_type."; name=\"".$filename."\"\r\n";
$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."--";
return (mail($mailto, $subject, "", $header) ? true : false);
} else {
$header = "From: ".($this->from_name)." <".($this->from).">\r\n";
$header .= "Reply-To: ".($this->reply_to)."\r\n";
return (mail($this->to, $this->subject, $this->message, $header) ? true : false);
}
}
How I'm calling it (which works and send the email w/ the attachment as expected)
$sendit = new MailAttachment(
$to,
$subject,
$message,
$excel_report,
basename($excel_report)
);
if(!$sendit->mail()) {
return 'Error';
}
Well it turns out everything is working as expected. The issue with the mail client is the file extension.
The file(s) with the extension .xls open up on double click from the email client,
The file(s) with the extension .xlsx do not open up on double click from the email client and need to be downloaded and opened.
Hope this helps someone out.

Categories