通过电子邮件发送两个附件,PHP

时间:2017-02-20 18:26:19

标签: php email

需要您的建议如何使用两个附件(一次性可下载链接)制作邮件并使用PHP发送。

现在,我有一个只发送一个链接的代码,它工作正常。实际上我是如何构建我的程序的,我有两个PHP文件:url.phpget_file.php。在get_file.php我使用url.php. 通过电子邮件为我要发送的文档创建一次性可下载URL。以下是get_file.php的代码:

<?php

/* Retrieve the given token: */
$token = $_GET['q'];

if( strlen($token)<32 )
{
        die("Invalid token!");
}

/* Define the file for download: */
$secretfile = "file1.pdf";

/* This variable is used to determine if the token is valid or not: */
$valid = 0;

/* Define what file holds the ids. */
$file = "urls.txt";

/* Read the whole token-file into the variable $lines: */
$lines = file($file);

/* Truncate the token-file, and open it for writing: */
if( !($fd = fopen("urls.txt","w")) )
        die("Could not open $file for writing!");

/* Aquire exclusive lock on $file. */
if( !(flock($fd,LOCK_EX)) )
        die("Could not equire exclusive lock on $file!");

/* Loop through all tokens in the token-file: */
for( $i = 0; $i < count($lines); $i++ )
{
        /* Is the current token the same as the one defined in $token? */
        if( $token == rtrim($lines[$i]) )
        {
                $valid = 1;
        }
        /* The code below will only get executed if $token does NOT match the
           current token in the token file. The result of this will be that
           a valid token will not be written to the token file, and will
           therefore only be valid once. */
        else
        {
                fwrite($fd,$lines[$i]);
        }
}

/* We're done writing to $file, so it's safe release the lock. */
if( !(flock($fd,LOCK_UN)) )
        die("Could not release lock on $file!");

/* Save and close the token file: */
if( !(fclose($fd)) )
        die("Could not close file pointer for $file!");

/* If there was a valid token in $token, output the secret file: */
if( $valid )
{
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="'.basename($secretfile).'"';
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    /*header('Content-Length: ' . filesize($file));*/
    readfile($secretfile);
}
else
{
        print "Invalid URL!";
}

?>

以下是url.php的代码:

<?php

$token = md5(uniqid(rand(),1));

$file = "urls.txt";
if( !($fd = fopen($file,"a")) )
        die("Could not open $file!");
if( !(flock($fd,LOCK_EX)) )
        die("Could not aquire exclusive lock on $file!");
if( !(fwrite($fd,$token."\n")) )
        die("Could not write to $file!");
if( !(flock($fd,LOCK_UN)) )
        die("Could not release lock on $file!");
if( !(fclose($fd)) )
        die("Could not close file pointer for $file!");

$cwd = substr($_SERVER['PHP_SELF'],0,strrpos($_SERVER['PHP_SELF'],"/"));

 function clean_string($string) {

      $bad = array("content-type","bcc:","to:","cc:","href");

      return str_replace($bad,"",$string);

    }


$a = "http://".$_SERVER['HTTP_HOST']."$cwd/get_file.php?q=$token";

$email_to = $_POST['email']; 
$email_to_bcc = "mail@mail.com";
$email_from = "mail@mail.com";
$email_subject = "download link";
$email_message = "Thank you for your purchase.\n\n";
$comments = $a;
$email_message .= "Here is your one-time link to download the file:\n".clean_string($comments);

$headers = 'From: '.$email_from."\r\n" .

'BCC: '.$email_to_bcc."\r\n" .

'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion(); 

mail($email_to, $email_subject, $email_message, $headers);
?>

问题是我的get_file.php中是否可以定义两个文件进行下载,创建两个可下载的链接,而不是在标题中创建两个附件?

0 个答案:

没有答案