Miscellaneous

  Home arrow Miscellaneous arrow Mail() From Start To Finish
MISCELLANEOUS

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

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

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    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

    blog comments powered by Disqus

    MISCELLANEOUS ARTICLES

    - Oracle Database XE: Indexes and Sequences
    - Modifying Tables in Oracle Database XE
    - Oracle Database XE: Tables and Constraints
    - More on Oracle Databases and Datatypes
    - Oracle Database XE Datatypes: Datetime and L...
    - Oracle Database XE Datatypes: Character and ...
    - From Databases to Datatypes
    - Firefox 3.6.6 Released with Improved Plug-in...
    - Attention Bloggers: WordPress 3.0 Now Releas...
    - Reflection in PHP 5
    - Inheritance and Other Advanced OOP Features
    - Advanced OOP Features
    - Linux from Scratch V.6.6 Review
    - Linux Gaining in Strength
    - Install Slackware on Your Old PC


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 5 - Follow our Sitemap