-2

Users fill the feedback form and send mails to us. I wnat to ue php mail() function. please help how to use the php script? what are the things i need for that?

I am new to this, can you please help me?

My form code:

<form name="contact" method="post" action="sendmail.php" enctype="multipart/form-data" onsubmit="return checkForm(this);">
<table style="width:100%;height:40px;"><tr><td align="right"><span id="error" style="color:#FF0000;">&nbsp;</span></td></tr></table>
<label><span>First Name:</span><input type="text" name="fname" id="fname"/></label>
<label><span>Last Name:</span><input type="text" name="lname"/></label>
<label><span>Email Address:</span><input type="text" name="email"/></label>
<label><span>Phone No. :</span><input type="text" name="phone"/></label>
<label class="last"><span>Feedback :</span><textarea name="message" cols="" rows=""></textarea></label>
<label class="btnsub"><input name="" type="submit" title="submit" class="submission"/></label>
</form>
Charles Menguy
  • 40,830
  • 17
  • 95
  • 117
Ashish Mehta
  • 7,226
  • 4
  • 25
  • 52

1 Answers1

2

Sendmail.php

<?php
if (isset($_POST['sendmail'])) {
    //catch and validate user inputs
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $phone = $_POST['phone'];

    //send mail using php's mail function
    $to      = 'tomail@gmail.com';
    $subject = 'FeedBack';
    //appended extra information just to show that you can do it like this way
    $message = $fname. " " . $lname .", " . $_POST['message'] . $phone;
    $headers = 'From:' . $_POST['email']. "\r\n" .
        'Reply-To:' . $_POST['email'] ."\r\n" .
        'X-Mailer: PHP/' . phpversion();

    // Please specify your Mail Server - Example: mail.yourdomain.com.
    //If you are testing on localhost, then make sure that mailserver is running on localhost
    ini_set("SMTP","localhost");

    // Please specify an SMTP Number 25 and 8889 are valid SMTP Ports.
    ini_set("smtp_port","25");

    mail($to, $subject, $message, $headers);
    //refer this URL:http://php.net/manual/en/function.mail.php
}
?>

NOTE : You should never use users input directly in script. NEVER. I have just shown you an example of how you can use mail function and not the entire process. Please validate users input before using it in script. You can get better idea from the URL i have mentioned above.

If you are taking some information from user like Firstname and Lastname and Phone number then its better that you pass it somehow in email to you. So, i have updated answer to give you idea about that.

Ashish Mehta
  • 7,226
  • 4
  • 25
  • 52
Bhavik Shah
  • 2,300
  • 1
  • 17
  • 32
  • Now i give submit button name 'sendmail' – Ashish Mehta Jan 05 '13 at 06:55
  • @AshuMehta: I have updated my answer after you have specified the name. Check it. After looking at your comment, i would like to inform you again that please dont use users input directly in script. Filter it. Otherwise, your script will be vulnerable. – Bhavik Shah Jan 05 '13 at 07:00
  • Error IS Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\wamp\www\Orbit\sendmail.php on line 13 – Ashish Mehta Jan 05 '13 at 07:03
  • @AshuMehta: This error is related with SMTP server settings. Checkout this link:http://stackoverflow.com/questions/4532486/failed-to-connect-to-mailserver-at-localhost-port-25 – Bhavik Shah Jan 05 '13 at 07:06
  • so What do I do??Please Help I am new to this – Ashish Mehta Jan 05 '13 at 07:09
  • I am working in local host so is it not working? – Ashish Mehta Jan 05 '13 at 07:11
  • @AshuMehta: are you trying to send mail on localhost or is it live? I guess, it should be local. Just testing right now. Correct? – Bhavik Shah Jan 05 '13 at 07:13
  • @Bhavesh Shah I am working in local host so is it not working? – Ashish Mehta Jan 05 '13 at 07:14
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/22226/discussion-between-ashumehta-and-bhavik-shah) – Ashish Mehta Jan 05 '13 at 07:21
  • Be carful with SMTP Injection. This example IS A SECURITY PROBLEM as the author wrote! https://www.owasp.org/index.php/Testing_for_IMAP/SMTP_Injection_%28OWASP-DV-011%29 – Fabian Blechschmidt Jan 05 '13 at 08:04