Follow us on...
Follow us on Twitter
Register
+ Reply to Thread
Results 1 to 8 of 8
  1. #1
    Senior Member ciullaanthony's Avatar
    Join Date
    Jan 2012
    Location
    Arlington, Texas
    Posts
    103
    Feedback Score
    0

    PHP Send Mail Tutorial

    Intorduction To Tutorial

    Lots and lots of website use PHP forms to communicate with there users. You will have most certainly seen one by the time you see this post, or if not used one to contact someone. This contact form will take the users information that he or she has filled out and then submitted it to you. In the case below, the information will be sent to the users email.

    Create The HTML Feedback Form

    To create the form use the following code below.
    Code:
    <?php    //Document index.php ?>
    <html>
    <head>
    <title>PHP Send Mail</title>
    </head>
    <body>
    <blockquote>
    <form method="post" action="sendmail.php">
    Email:< input name="email" type="text" />
    Message:<textarea name="message" rows="10" cols="30">
    </textarea>
    <input type="submit" />
    </form>
    </blockquote>
    </body>
    </html>
    This form will take the email address, and the message the user has for your website and send it to the page called "sendmail.php". The php script will know which piece of information being sent by the input name="" tag. So we know the email will be typed in the email box by the name tag: name="email".

    PHP's Sendmail Code

    Now we have set the form to POST the data to sendmail.php we need to create a new file named “sendmail.php". Once you have created the file you can enter the following code into your empty php file. Don’t worry I’ll explain each line in a moment.

    Code:
    <?php    //Document sendmail.php ?>
    <html>
    <head>
    <title>PHP Send Mail</title></head>
    <body>
    <blockquote>
    <?php
    $email = $_GET['email'] ;
    $message = $_GET['message'] ;
    mail( "yourname@example.com", "Email Subject", $message, "From: $email" );
    print "Congratulations your email has been sent";
    ?>
    </blockquote>
    </body>
    </html>
    Sendmail Function Explanation

    Okay this script will now send email out using php’s sendmail function ya!

    Line 2 - 3: These get the data from the form keeping in mind the email form box is named name="email" and the $_GET variable is also called email.

    Line 4: This is the clever part that sends the email, The mail() function allows to… specify the email address of the recipient, place the the subject of the email which will appear in the subject line, puts the message of the email which appears in the emails main body and the function allows you to specify the senders email so the recipient can then send a reply if required.

    The sendmail function is no way limited to this configuration but as a beginner tutorial this is the minimum information you need to make mail() work.

    For more information on the sendmail function then please go to php's sendmail function page.

    line 5 - Will display “Congratulations your email has been sent"

    I hope the tutorial helps any comments or question as always can be post below.
    Need Help? PM me for help.Thanks for visiting: http://www.webdevforum.com/

  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    3
    Feedback Score
    0
    Do I have to have a SMTP to use this?

  3. #3
    Senior Member ciullaanthony's Avatar
    Join Date
    Jan 2012
    Location
    Arlington, Texas
    Posts
    103
    Feedback Score
    0
    No, I have been using it without a SMTP, If I remember correctly most PHP servers have there own SMTP.
    Need Help? PM me for help.Thanks for visiting: http://www.webdevforum.com/

  4. #4
    Member Noble Genius's Avatar
    Join Date
    Jan 2012
    Location
    Northeast, U.S.A
    Posts
    42
    Feedback Score
    0
    Correct me if I am wrong, but I don't think that this can prevent injections and spam? It seems vulnerable.
    http://h5network.com/ - Design & Coding Forums

  5. #5
    staff Martin's Avatar
    Join Date
    Dec 2011
    Location
    Prague
    Posts
    348
    Feedback Score
    0
    and you have form method set to _POST, but in script is used _GET

  6. #6
    Senior Member ciullaanthony's Avatar
    Join Date
    Jan 2012
    Location
    Arlington, Texas
    Posts
    103
    Feedback Score
    0
    Are those predefined PHP variables?
    Need Help? PM me for help.Thanks for visiting: http://www.webdevforum.com/

  7. #7
    Member Noble Genius's Avatar
    Join Date
    Jan 2012
    Location
    Northeast, U.S.A
    Posts
    42
    Feedback Score
    0
    Quote Originally Posted by ciullaanthony View Post
    Are those predefined PHP variables?
    Yes. You can find a complete list on PHP's official website. PHP: Predefined Variables - Manual
    http://h5network.com/ - Design & Coding Forums

  8. #8
    Senior Member ciullaanthony's Avatar
    Join Date
    Jan 2012
    Location
    Arlington, Texas
    Posts
    103
    Feedback Score
    0
    Ok, thank you for the corrections. PHP Send Mail is a hard programming.
    Need Help? PM me for help.Thanks for visiting: http://www.webdevforum.com/

 

 

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