SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict jamus's Avatar
    Join Date
    Jul 2004
    Location
    Devon, UK
    Posts
    301
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Php Mailer with Attachment - Step by Step

    I have tried searching sitepoint and believe many people including myself would benifit from a walk-through on phpmailer. There are many posts that are related to different parts but none that cover from script install through to form design.

    STEP 1 - Installing phpmailer and testing

    This first step is simple. Download the files here http://phpmailer.sourceforge.net/
    Upload the ALL the files to the directory from which the script will be called. You can test all is well by uploading a file called test.php that contains the following (http://phpmailer.sourceforge.net/tutorial.html)
    PHP Code:
    <?php
    require("class.phpmailer.php");
    $mail = new PHPMailer();
    ?>
    Browse to the test.php file and if no errors are returned the install is correct.

    Whats next?

  2. #2
    SitePoint Addict jamus's Avatar
    Join Date
    Jul 2004
    Location
    Devon, UK
    Posts
    301
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Step 2 - Sending A Test Email

    Save the following code into a php document. Alter the settings as required for your server eg. "smtp.email.com", "from@email.com" & "myfriend@site.com"

    PHP Code:
    <?php
    require("class.phpmailer.php");
    $mail = new PHPMailer();
    $mail->IsSMTP();    // telling the class to use SMTP
    $mail->Host "smtp.email.com"// SMTP server
    $mail->From "from@email.com";
    $mail->AddAddress("myfriend@site.com");

    $mail->Subject "first mailing";
    $mail->Body "hi ! \n\n this is First mailing I made myself with PHPMailer !";
    $mail->WordWrap 50;

    if(!
    $mail->Send())
    {
       echo 
    "Message was not sent";
       echo 
    "Mailer Error: " $mail->ErrorInfo;
    }
    else
    {
       echo 
    "Message has been sent";
    }
    ?>
    Upload to the same diectory as before and browse to the file. The Message should be sent and confirmation message appear.

  3. #3
    SitePoint Addict jamus's Avatar
    Join Date
    Jul 2004
    Location
    Devon, UK
    Posts
    301
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    STEP 3 - Adding a form

    A simple HTML form can be used to pass details to the script to be sent instead of the fixed message used previously.

    Important parts to the form are the filename of the script
    And the variable that will contain the data from the form

    HTML FORM
    HTML Code:
    <form name="contact" method="post"  action="phpmailerscript.php">
    
    <input name="name" type="text" size="45" value="<?php echo $name ?>">
    Then in the phpmailer script the variable needs to be collected and put in the body of the sent email. For example replace the previous
    PHP Code:
    $mail->Body "hi ! \n\n this is First mailing I made myself with PHPMailer !"
    with
    PHP Code:
    $mail->Body "Name: $name\n"
    now how do we add an attachment??

  4. #4
    SitePoint Zealot
    Join Date
    Nov 2005
    Location
    Nashville, TN
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:

    $mail
    ->AddAttachment("../dir_to_file/File.dot"); 

  5. #5
    SitePoint Addict jamus's Avatar
    Join Date
    Jul 2004
    Location
    Devon, UK
    Posts
    301
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could you expand on his pmm001?

    I was actually hoping to send the attachment using the form. Using something like...

    HTML Code:
        <input class="text" type="file" name="userfile" size="50" />

  6. #6
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    my mobile portal
    ghiris.ro


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •