通过php发送jpg作为附件

时间:2018-01-30 18:13:59

标签: php email

我正在使用php的mail()从我的服务器发送邮件。我想发送jpg作为附件,但我总是收到一个0长文件。我坚持下去了。

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);

    $eol = PHP_EOL;

    // Basic headers
    $header = "From: ".$from_name." <".$from_mail.">".$eol;
    $header .= "Reply-To: ".$replyto.$eol;
    $header .= "MIME-Version: 1.0".$eol;
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"";

    // Put everything else in $message
    $message = "--".$uid.$eol;
    $message .= "Content-Type: text/html; charset=ISO-8859-1".$eol;
    $message .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
    $message .= $message.$eol;
    $message .= "--".$uid."--".$eol;
    $message .= "Content-Type: image/jpg; name=\"".$filename."\"".$eol;
    $message .= "Content-Transfer-Encoding: base64".$eol;
    $message .= "Content-Disposition: attachment; file=\"".$filename."\"".$eol;
    $message .= $content.$eol;
    $message .= "--".$uid."--".$eol;

    if (mail($mailto, $subject, $message, $header))
    {
        echo "mail_success";
    }
    else
    {
        echo "mail_error";
    }

0 个答案:

没有答案