1

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>';
   }
user3386779
  • 6,883
  • 20
  • 66
  • 134

3 Answers3

1

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 ;
}
Halayem Anis
  • 7,654
  • 2
  • 25
  • 45
1

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);
Deenadhayalan Manoharan
  • 5,436
  • 14
  • 30
  • 50
0

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.

Marc Brillault
  • 1,902
  • 4
  • 21
  • 41