PHP服务器端不发送PDF到电子邮件

时间:2017-04-21 06:03:13

标签: php pdf

这确实给了我挑战,我想确定我在做什么。现在我想我将tmp_name添加到邮件中,以便附加从html客户端浏览的PDF。

我在想我是这样做的

<?php
$ip = getenv('REMOTE_ADDR');
$hostname = gethostbyaddr($ip);

$email_to = "a@domain.com";
$email_subject = "My subject";
$fullname = $_POST['fullname'];
$institution = $_POST['institution'];
$month = $_POST['month'];
$Day = $_POST['Day'];
$year = $_POST['year'];
$courses = $_POST['courses'];
$marital_status = $_POST['marital_status'];
$cover_letter = $_POST['cover_letter'];
$file_name = $_FILES['resume']['name'];
$file_size = $_FILES['resume']['size'];
$file_type = $_FILES['resume']['type'];
$tmp_name = $_FILES['resume']['tmp_name'];

$email_msg = "IP: $ip\n Host Name: $hostname\n Name in Full : $fullname\n Institution: $institution\n DOB : $month/$Day/$year\n Courses: $courses\n Marital Status: $marital_status\n Cover Letter : $cover_letter\n Attachment: $tmp_name\n\n";

$file = fopen($tmp_name,'rb');
$data = fread($file,filesize($tmp_name));
fclose($file);

$semi_rand     = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers = "MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" ;
$message = "This is a multi-part message in MIME format.\n\n" .
"-{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_msg  . "\n\n";
$data = chunk_split(base64_encode($data));
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$file_type};\n" .
" name=\"{$tmp_name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$tmp_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"-{$mime_boundary}-\n";




$sendmemail = @mail($email_to, $email_subject, $email_msg, $headers);

if($sendmemail)
    {

                echo ("<SCRIPT LANGUAGE='JavaScript'>
                window.alert('Registeration Complete')
                window.location.href='[url]';
                </SCRIPT>"); 
    }

?>

其中$ tmp_name是附件的名称(PDF,DOC,DOCX,RTF)等。

1 个答案:

答案 0 :(得分:0)

我很瘦,你必须$data使用$tmp_name 见下面的例子。
    $ file = file_get_contents(“picture.jpg”);

$message .= "Content-Type: image/jpg; name=\"picture.jpg\"\r\n"
  ."Content-Transfer-Encoding: base64\r\n"
  ."Content-disposition: attachment; file=\"picture.jpg\"\r\n"
  ."\r\n"
  .chunk_split(base64_encode($file))
  ."--1a2a3a--";