Email attachment

Posted on 2004-11-18
Last Modified: 2009-07-29

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.

Question by:db2trade
    LVL 27

    Accepted Solution

    See Mail_Mime class:

    or better still PHP Mailer:

    PHP Mailer example:


    $mail = new PHPMailer();

    $mail->From     = "";
    $mail->FromName = "Your name";
    $mail->AddAddress("","Some Guy");

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

       echo "Message was not sent <p>";
       echo "Mailer Error: " . $mail->ErrorInfo;

    echo "Message has been sent";

    See the example on their site for usage guide, also see their other examples:
    LVL 27

    Expert Comment

    A rough guide to using phpmailer if you need it: http:Q_21141127.html#12148132
    LVL 2

    Expert Comment

    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:
    LVL 27

    Expert Comment

    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.
    LVL 2

    Expert Comment


    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 ;)

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now