发送电子邮件标头附件的语法

时间:2012-11-24 20:17:15

标签: php email pear

  

可能重复:
  How to send emails with PHP using the PEAR Mail package with attachment

我想使用生成的附件(不是服务器上的文件)发送带有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文件(不在服务器上)附加到电子邮件中。

1 个答案:

答案 0 :(得分:2)

How to send emails with PHP using the PEAR Mail package with attachment

开始

然后替换以下内容:

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

上的文档