Jump to content

[SOLVED] download link in email


Hooch

Recommended Posts

Is it possible to have a link in an email (being sent from a webpage on my site)

with the option to download (and not to view)?

I have the email working great, but when you click the link it pops up a new

browser and shows the file.  I would like to have the link ask if you want

to download instead.

 

FYI here is my code

		//send the email
	//Set up info 
	$_SESSION['filetoupload'] = $_POST['filetoupload'];
	$_SESSION['all_emails']   = $_POST['all_emails'];

	$file_name = $_SESSION['file_name'];   
	$to        = "[email protected]";
	$subject   = stripslashes($_POST['txtSubject']);		
	$message   = stripslashes($_POST['txtMessage']);
	$message  .= "<br />";
	if($_SESSION['filetoupload'] == 1)
	{
		$message .= "<br />";
		$message .= "To view file please click the link below.";
		$message .= "<br /><br />";
		$message .= '<a href="http://www.mysite.org/files/emails/'.$file_name.'">'.$file_name.'</a>';
		$message .= "<br /><br />";
	}
	$message .= "--------------------";	
	$message .= "<br />     mysite.org     <br />";
	$headers  = "From: mysite.org\r\n";
	$headers .= "Return-Path: <".$to.">\r\n";
	$headers .= "MIME-Version: 1.0\r\n";
	$headers .= "Bcc:".$_SESSION['all_emails']."\r\n";
	$headers .= "Content-Type: text/HTML; charset=ISO-8859-1\r\n"; 

	if(mail($to, $subject, nl2br($message), $headers))
	{
		echo '<b>Mail Sent</b>';
	}
		else
	{
		echo '<b>Mail not sent</b>';
	}

Link to comment
https://forums.phpfreaks.com/topic/129788-solved-download-link-in-email/
Share on other sites

I have implemented the download script.

 

I changed

$message .= '<a href="http://www.mysite.org/files/emails/'.$file_name.'">'.$file_name.'</a>';

to

$message .= '<a href="http://www.mysite.org/files/emails/file.php?file='.$file_name.'">Download</a>';

Then I needed to create a file called file.php in the folder where the file is.

Here is file.php

<?php

// force to download a file
// ex, ( [url=http://localhost/php/download.php?file=C:/Apache]http://localhost/php/download.php?file=C:/Apache[/url] Group/Apache2/hongkong.php )
// hope this can save your time :-)

$file = $_GET['file'];

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 

header("Content-Type: application/force-download");
header( "Content-Disposition: attachment; filename=".basename($file));

header( "Content-Description: File Transfer");
//Inform the browser of what's coming -- allowing it properly report download progress and estimated completion time
header('Accept-Ranges: bytes');
header('Content-Length: ' . filesize($file));
@readfile($file);

?>

 

Thank you thorpe

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.