• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 235
  • Last Modified:

Email attachment

Hi,

I know how to send an email using PHP :

               $to = $_POST['to'];
            $from = 'From: '.$_POST['from'];
            $subject = '['.$_POST['tag'].'] '.$_POST['subject'];
            $message = $_POST['message'];

            mail($to, $subject, $message, $from);

My question is, how can I attach a TEXT file ? Suppose I have a text file called "order.txt" and I want to attach it in the mail.

I need this to send the order to a company, but it only accept email attachment.

Thanks
0
db2trade
Asked:
db2trade
  • 3
  • 2
1 Solution
 
Diablo84Commented:
See Mail_Mime class: http://pear.php.net/package/Mail_Mime

or better still PHP Mailer: http://phpmailer.sourceforge.net/

PHP Mailer example:

require("class.phpmailer.php");

$mail = new PHPMailer();

$mail->From     = "from@domain.com";
$mail->FromName = "Your name";
$mail->AddAddress("address@domain.com","Some Guy");
$mail->AddAttachment("file.txt");


$mail->Subject  =  "Here is the subject";
$mail->Body     =  "This is the <b>HTML body</b>";
$mail->AltBody  =  "This is the text-only body";

if(!$mail->Send())
{
   echo "Message was not sent <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}

echo "Message has been sent";


See the example on their site for usage guide, also see their other examples: http://phpmailer.sourceforge.net/extending.html
0
 
Diablo84Commented:
A rough guide to using phpmailer if you need it: http:Q_21141127.html#12148132
0
 
b_locoCommented:
Your question made me curious because I use the mail function a lot but never thought/needed of doing it.

Anyway, found this, have a look:

http://www.sitepoint.com/article/advanced-email-php/5
0
 
Diablo84Commented:
Really not worth going through it just to send an attachment, using a class will cut out all the work for you and allow you to easily add one or more attachments just by adding a line to the file. The advanced error checking and other features provided also make using a class such as phpmailer much more worthwhile.
0
 
b_locoCommented:
Diablo84:

It's funny, because when I started to write my reply there was still no answer
When I finished writing it I saw your answer, had a look and realised just that ;)
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now