php邮件截断PDF附件

时间:2013-02-28 06:00:35

标签: php email pdf

我正在尝试使用php的邮件功能发送带有pdf附件的邮件。 我收到邮件中的其他东西,但附件大小为零kb。 任何人都可以查看我的代码并在此处提出错误建议吗?

谢谢! 下面是代码:

public static function sendMailWithAttachment($to, $from, $from_name, $subject, $msg,$attachmentName, $attachmentPath){
// Setting a timezone, mail() uses this.
//date_default_timezone_set('America/New_York');
$semi_rand = md5(time());
$data = chunk_split(base64_encode($data));

$fileatt_type = "application/pdf"; // File Type
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

// set header ........................
$headers = "From: ".$from;
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";

// set email message......................
//$email_message = "Thanks for visiting ";
//$email_message .= "Thanks for visiting.<br>";// Message that the email has in it
$email_message=$msg;
$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_message .= "\n\n";
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$attachmentPath}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$attachmentName}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data .= "\n\n" .
"--{$mime_boundary}--\n";

$sent = @mail($to, $subject, $email_message, $headers);
if($sent) {
echo "Your email attachment send successfully.";
} else {
die("Sorry but the email could not be sent. Please try again!");
}
} 

2 个答案:

答案 0 :(得分:0)

我不知道为什么你使用这种方法发送带附件的邮件,而且很难调试。看看我的功能。

<?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()));
    $name = basename($file);
    $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!";
    }
}
?>

答案 1 :(得分:0)

我个人建议使用经过实践检验的邮件组件来处理我的所有邮件活动。我过去使用的一个是PHPMailer(找到here),它似乎开箱即用(并且它是免费的!)。

我编写了以下包装类来简化用法:

<?php
require_once "PHPMailer.php";

/**
 * class SimpleMailer
 * Very basic mail wrapper around PHPMailer,
 * helps to abstract some of PHPMailer's huge feature list
 * and keep the complexity as low as possible
 *
 * @author Jason Larke
 * @date  10/10/2012
 */
class SimpleMailer {
    /**
     * bool send(array, string, string, mixed, array)
     * Send a basic email to a list of recipients
     *
     * @param $recipients an array of recipients to this email. Each array entry should be either an email address, or a key-value pair of email=>value and name=>value
     * @param $subject Email subject string
     * @param $body Email body string
     * @param $from mixed, can either be a single email address or an associative array specifying both email and name keys i.e array('email'=>'admin@example.com','name'=>'Example')
     * @param $attachments An array of file-paths to files that should be attached to the email
     */
    public static function send($recipients, $subject, $body, $from, $attachments=array()) {
        // sanitize
        $recipients = (array)$recipients;
        $attachments = (array)$attachments;

        $client = new PHPMailer();
        $client->IsMail(); // use PHP's internal 'mail' function, for simplicity.

        $senderEmail = is_array($from) && !empty($from['email']) ? $from['email'] : $from;
        $senderName = is_array($from) && !empty($from['name']) ? $from['name'] : $senderEmail;

        $client->SetFrom($senderEmail, $senderName);
        $client->Subject = $subject;
        $client->MsgHTML($body);

        foreach($recipients as $to) {
            $email = is_array($to) && !empty($to['email']) ? $to['email'] : $to;
            $name = is_array($to) && !empty($to['name']) ? $to['name'] : $email;
            $client->AddAddress($email, $name);
        }

        foreach($attachments as $attach)
            $client->AddAttachment($attach);

        $result = $client->Send();
        if (!$result) {
            // todo: Log any information about why the mail failed to send (accessed by $client->ErrorInfo)
        }
        return $result;
    }
}
?>

该类的示例用法如下:

SimpleMailer::send(
    'foobar@example.com', // to-address
    'This is a test email', // subject
    'Hello Foo Bar, we're testing our email service.' // content
    array('email' => 'admin@example.com', 'name' => 'Example.com'), // sender
    '/local/docs/example.pdf' // attachments (can be an array)
);