Right, i'm new to PHP and mySQL, but i've got a few questions that i hope some of you guys can answer because this is confusing me right now.
(I'll still try to read up more on google to see if i can find any answers whilst this is going on, so if i find any, i'll post my own answers up here as well)
- I want a feedback form on my site. I'm looking at the PHP Email Forms but i'm kind of confused here --
On the API, it says that for someone to send an email to me, the php script needs to enter and send this code:
mail(to,subject,message,headers,parameters)
However, i realise there is no "from" argument inside this function. Why is this so? How will i be able to find out the email of the other person and be able to reply to him/her via my own email after receiving such a feedback request?
I'm also reading on parts where people include the sender's email into the "headers" argument itself. How will a mail system gather that there is a sender's email if i include this inside the "header" argument? (I'm asking this part because my webhost provides an auto-reply before i reply, and i want it to be able to recognize that the sender's email is the email that the system should auto-reply to)
Would it be easier to just put a mailto:[email protected] link on the webpage?
I think that's pretty much all i'm trying to find out for now, i have more complex questions but i think i'll try to solve the easier ones first.
5 Answers 5
Check out php mail in the manual
additional_headers (optional)
String to be inserted at the end of the email header.
This is typically used to add extra headers (From, Cc, and Bcc). Multiple extra headers should be separated with a CRLF (\r\n).
Example from the manual
$to = '[email protected]';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
Update:
You also asked about putting the name in the from address. To do this just change the From: in the headers to WEBMASTER NAME <[email protected]>
1 Comment
If you put a mailto link on your page, when a user clicks on that, then it will open up the user's email client, and they can send you an email. The downside is that the email address that's on the page is easily accessible to spammers.
If you use a PHP form, then your email address will stay hidden - it's buried inside the code. The downside there is that there is that setting it up is more involved.
As Set Sail Media mentions above, you can specify a From address inside the headers parameter. If it's not set, then it'll use the details of the process that the webserver is running as ([email protected], for example.) Not the most useful, but it's generally not a problem.
If you want to know what the email address of the user is, the only way is to ask them as part of the form. You can't pick it up automatically.
2 Comments
The mail function is used merely to send mail "to" people via the locally configured or specified remote mail server only, it cannot "receive" mail, to do that you will need other functionality, such as reading the data from the mail server, which is dependant on that mail server.
It is easier to put a "mailto" link on a web page, but basically with "mail()", you can make a whole little form they use which makes your web server act as an e-mail client that can only send mail (unless you put in advanced functionality).
3 Comments
Here is a rudimentary form processing script that you might find helpful.
<?php
if ( isset( $_POST ) && count( $_POST ) > 0 ){
// Form was submitted, process it
foreach ( $_REQUEST as $k=>$v ){ // Loop through values submitted and add them to a message to send to form owner
$k = stripslashes( $k );
$v = stripslashes( $v );
$message .= "$k: $v\r\n";
}
$headers = "From: Name <[email protected]>\r\n"; // This is the FROM address
// Uncomment the following line to set From using the value passed to the form
// $headers = "From: {$_POST['your_name']} <{$_POST['email']}>\r\n"; // This is the FROM address
$ab = mail( '[email protected]', 'Contact Submission Form', $message, $headers ); // Send the email
if ( $ab == true ){
// Message sent!
echo '<strong>Thank you, your message was received.</strong>';
}
}
?>
<form action="" method="post" id="contactform">
<label for="your_name">Name:</label>
<input type="text" name="your_name" id="your_name" class="field" value="" />
<label for="email">Email:</label>
<input type="text" name="email" id="email" class="field" value="[email protected]" />
<label for="phone">Phone:<span>(optional)</span></label>
<input type="text" name="phone" id="phone" class="field" value="(000) 000-0000" />
<label for="message">Message:</label>
<textarea name="message" id="message" class="field textarea" cols="20" rows="5"></textarea>
<input type="submit" value="Send Message" />
</form>
Comments
The SMTP server that is sending the email is yours (your website) (it can be configured to be a remote server). That's why From field is taken in general from a text field in your form.
mailto:, please don't feed the spambots