0

可能重复:
如何使用带有附件的 PEAR 邮件包使用 PHP 发送电子邮件

我想使用生成的附件(不是服务器上的文件)发送一封带有 PHP 的电子邮件,我对语法很好奇。

<?php
@require_once "Mail.php";

$from = "foo@me.com>";
$to = "blah@me.com>";
$subject = "Hi!";
$body = "Attached is the file\n\n";

$attachment = header( 'Content-Type: text/csv' );
$attachment = $attachment.header( 'Content-Disposition: attachment;filename=example.csv');
$attachment =  'transactionid,valid,fname,lname,email,studentid,status
            "654","1","First Name","Last Name","Email@me.com","1000000"';
$body = $body.$attachment;


$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);


$smtp = @Mail::factory('smtp', array('host' => $host,
                                    'port' => $port,
                                    'auth' => true,
                                    'username' => $mail_username,
                                    'password' => $mail_password));

$mail = @$smtp->send($to, $headers, $body);
if (@PEAR::isError($mail)) {
   echo("<p>" . $mail->getMessage() . "</p>");
} else {
   echo("<p>Message successfully sent!</p>");
}
?>

编辑:请注意,我知道有很多在电子邮件中附加来自服务器的文件的示例,但我问的是如何将生成的 csv 文件(不在服务器上)附加到电子邮件中。

4

1 回答 1

2

从如何使用带有附件的 PEAR Mail 包使用 PHP 发送电子邮件开始

然后替换以下内容:

if ($mime->addAttachment($file,'application/pdf')){

$file =  'transactionid,valid,fname,lname,email,studentid,status
            "654","1","First Name","Last Name","Email@me.com","1000000"';
if ($mime->addAttachment($file,'text/csv', 'example.csv', false)){

请参阅http://pear.php.net/manual/en/package.mail.mail-mime.addattachment.php上的文档

于 2012-11-24T21:04:54.770 回答