Miscellaneous
  Home arrow Miscellaneous arrow Mail() From Start To Finish
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Forums Sitemap 
Dedicated Servers  
Download TestComplete 
IBM® developerWorks
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
IBM developerWorks
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MISCELLANEOUS

Mail() From Start To Finish
By: Andrew Walsh
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 2
    2004-06-17

    Table of Contents:
  • Mail() From Start To Finish
  • Sending Html Emails
  • Sending Emails With Attachments:

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Mail() From Start To Finish


    (Page 1 of 3 )

    Ever wanted to know how to send basic text emails/Html emails and emails with attachments? In this tutorial you will learn how to send emails in text, html and with attachments.

    A text email is the simplest email you can possibly send. The code you can use is easy to create and send one is easy too. Firstly I will show you how to send basic text emails from strings coded into the script, then I will finish off this section with a example that uses forms to send form data to an email.

    <?php
    /* First of all we define the variables which are the message,
    the from address, the to address and the subject along with the headers
    which are used to show who sent the email. */
    $message "Hello Someone\n this is a simple text email message\n";
    $from "someone@domain.com";
    $to "someone@anotherdomain.com";
    $subject "Simple Text Email";
    $headers "From: $from";
    /* Now we are ready to send the email so we call php's mail() function
    with the appropriate variables from above included in the brackets */
    mail($to,$subject,$message,$headers);
    ?>

    That’s all's that their is to sending basic text email messages using php's built in mail() function. Now what if you have a form that website users can use you to contact the webmaster. How do you send the information from the form to an email address you may be asking. Well its not that hard as i will show in my example below:

    <?php
    /* First we need to check if the form has been submitted by the user */
    if(!isset($submit)){ /* If form hasn&#8217;t been submitted */
    echo "&lt;form action='$_SERVER[PHP_SELF]' method='post'&gt;
    Name:&lt;input type='text' name='name'&gt;&lt;br&gt;
    Subject:&lt;input type='text' name='subject'&gt;&lt;br&gt;
    Email:&lt;input type='text' name='email'&gt;&lt;br&gt;
    Message:&lt;br&gt;
    &lt;textarea name='message' cols='40' rows='15'&gt;&lt;/textarea&gt;&lt;br&gt;
    &lt;input type='submit' name='submit' value='send'&gt;
    &lt;/form&gt;"
    ;
    }else{ 
    /* If form has been submitted */
    $to="myemail@mydomain.com";
    /* Get the form date from the variable $_POST which stores everything
    from the form since the method of the form is post if it was get
    you would use $_GET instead of $_POST. You can reference it directly e.g.
    $email but it may not work because the setting register_globals is off
    by default which means you have to use $_POST or $_GET to access form data
    */
    $from=$_POST['email'];
    $message=$_POST['message'];
    $subject=$_POST['subject'];
    $submit=$_POST['submit'];
    /* Send The Email*/
    $headers "From: $from";
    if(
    mail($to,$subject,$message,$headers)){
    echo 
    "Email Sent";
    }else{
    echo 
    "Email Sending Failed";
    }
    /* Notice that I have added an if statement to determine if the
    mail() function was run successfully, this does not mean the email has
    been sent. It may bounce off the mail server and will be sent to the 
    email address it was sent from which in this case is the email specified
    in $from
    */
    }
    ?>

    Wasn’t that easy? Lets Carry on to look at sending html emails and Sending emails with attachments.

    More Miscellaneous Articles
    More By Andrew Walsh


       · Let's add some content checking to the above. We'll check to ensure they typed out...
       · H! KendoVery well done indeed, At such a tender age U R writing such fabulous...
       · Hi,Nice tutorial, especially with having the HTML and file attachment sections...
       · Thanks for the nice scripts!To get the second script to work I must add the...
       · thank you for spotting it ill get it corrected.
       · The line $submit=$_POST['submit']; has been added to the appropriate pages.
       · Mail never arrives at destination.
       · Attachments section has been updated and proven to work. Thank you for pointing it...
       · Andrew, I copied and adapted your script for attaching a document to a form that I...
       · I've copied the code exactly but the email never makes it to my inbox. However if I...
       · I am sorry to say, that these days, sending mail with PHP is a little more...
     

    MISCELLANEOUS ARTICLES

    - Stopping CSRF Attacks in Your PHP Applicatio...
    - Quick and Dirty AJAX Tutorial
    - Flickr Puzzle Mashup
    - The PAVISE of Security
    - Creating a CAPTCHA with PHP
    - Sending SMS Thru HTTP
    - The Postal Fix - Part 2
    - Adding Mail with Exim
    - The Postal Fix - Part 1
    - Create Your Own Custom API
    - Adding Drop Shadows with PHP
    - Writing a Basic Authentication System in PHP
    - Overlapping Images with GD
    - Using Sockets in PHP
    - Dynamic CSS with PHP






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway