0

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

I want to send an email with PHP using a generated attachment (not a file on the server), I was curious about the syntax.

<?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>");
}
?>

EDIT: Please note, i know there are many examples of attaching a file from the server in an email, but I am asking about attaching a generated csv file (that is NOT on the server) to an email.

Community
  • 1
  • 1
Arian Faurtosh
  • 17,987
  • 21
  • 77
  • 115
  • The `header()` function has nothing to do with text processing / mails. And `.` is the concatenation operator, not for method calls. – mario Nov 24 '12 at 20:28
  • @mario it's not a duplicate of that, that example shows attaching from a file on the server... how do i attach using a generated cvs string? – Arian Faurtosh Nov 24 '12 at 20:32
  • We have a few hundred more duplicates. Please use the search function, if not the manual. – mario Nov 24 '12 at 20:34
  • @mari Show me one duplicate, I can't find any question similar... all questions are about attaching a file from the server... not a generated file... please read the full question – Arian Faurtosh Nov 24 '12 at 20:43
  • So. What searches have you tried? Time to check out the manual yet? – mario Nov 24 '12 at 20:45
  • @mario what manual should i check for email syntax? you stated there are a few hundred more duplicates... please link these duplicates... because i can't find them. – Arian Faurtosh Nov 24 '12 at 20:47
  • @mario let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/20045/discussion-between-arian-and-mario) – Arian Faurtosh Nov 24 '12 at 20:50
  • Checking the manual for the mail module that you are using (you are not using PHP's built-in mail function) would indeed be a good first step. – Pekka Nov 24 '12 at 21:20

1 Answers1

2

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

Then replace the following:

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

with

$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)){

see documentation at http://pear.php.net/manual/en/package.mail.mail-mime.addattachment.php

Community
  • 1
  • 1
joequincy
  • 1,395
  • 10
  • 21