梨邮件发送没有附件的电子邮件

时间:2015-04-29 14:20:47

标签: php email sendmail pear

我正在尝试使用PHP PEAR Mail发送带有一些附件的邮件。我正在阅读文本文件以获取filenames,然后使用此文件创建filePath,我可以根据PEAR addAttachment()语法附加文件。但是当我收到邮件时,它没有任何附件。

我的代码

function sendMail() {
    $pdf_filename = file_get_contents("/tmp/uploads/filelog/pdfFiles.txt");
    $csv_filename = file_get_contents("/tmp/uploads/filelog/csvFiles.txt");

    $text = 'Text version of email';
    $html = '<html><body>HTML version of email</body></html>';
    $csv_file = '/tmp/uploads/csv/' . $csv_filename;
    $pdf_file = '/tmp/uploads/pdf/' . $pdf_filename;
    $crlf = "\n";
    $hdrs = array (
            'From' => 'you@yourdomain.com',
            'Subject' => 'Test mime message' 
    );

    $mime = new Mail_mime ( array (
            'eol' => $crlf 
    ) );
    $mime->setTXTBody ( $text );
    $mime->setHTMLBody ( $html );
    $mime->addAttachment ( $csv_file, 'text/csv' );
    $mime->addAttachment ( $pdf_file, 'application/pdf' );
    $body = $mime->get ();
    $hdrs = $mime->headers ( $hdrs );
    $mail = & Mail::factory ( 'mail' );
    $mail->send ( 'user@sippycup.co.uk', $hdrs, $body );
    if (PEAR::isError ( $mail )) {
        echo ("<p>" . $mail->getMessage () . "</p>");
    } else {
         echo ("<p>Message successfully sent!</p>");
    }
}
sendMail();

为什么这不起作用?即使我已确认服务器上存在这两个文件。

1 个答案:

答案 0 :(得分:0)

生成了一些空格,一个简单的trim()解决了我的问题。

$csv_trimed = trim($csv_file);
$pdf_trimed = trim($pdf_file);
.
.
.
.
.

$mime->addAttachment ( $csv_trimed, 'text/csv' );
$mime->addAttachment ( $pdf_trimed, 'application/pdf' );