Retrieve values from HTML form
I have a mail sending functionality in php code, I want to use define to set to address and from address.How can this be done.
The code is
<?php
$subject = 'Test email';
$message = "Hello World!\n\nThis is my first mail.";
$headers = "From: $from\r\nReply-To: webmaster@example.com";
//send the email
$mail = @mai开发者_运维技巧l( $to, $subject, $message, $headers );
?>
How to define $to and $from. Thanks in Advance for help
Unless it is absolutely imperative, I would recommend using a good old fashioned variable for this particular task, not a constant.
If you do want to use a constant:
define('MAIL_TO', 'mailto@gmail.com');
define('MAIL_FROM', 'mailfrom@gmail.com');
$subject = 'Test email';
$message = "Hello World!\n\nThis is my first mail.";
$headers = "From: " . MAIL_FROM . "\r\nReply-To: webmaster@example.com";
$mailResult = mail(MAIL_TO, $subject, $message, $headers);
FYI:
// Constants can also be retrieved with the constant() function
$mailTo = constant('MAIL_TO');
// ...which is the same as...
$mailTo = MAIL_TO;
With use of constants:
$mailTo = 'mailto@gmail.com';
$mailFrom = 'mailfrom@gmail.com';
$subject = 'Test email';
$message = "Hello World!\n\nThis is my first mail.";
$headers = "From: " . $mailFrom . "\r\nReply-To: webmaster@example.com";
$mailResult = mail($mailTo, $subject, $message, $headers);
Here's a very basic example. I'll leave it up to you to do validation.
HTML:
<form action="path/to/your/script.php" method="post">
<input type="text" name="from" />
<input type="submit" value="Send" />
</form>
PHP:
In PHP, you'll want to use $_REQUEST
, $_POST
or $_GET
depending on the action
parameter of the form
in your HTML. If you're not sure, use $_REQUEST
. The value that goes in the square brackets is the name
of attribute of the input
from your HTML.
define('TO_ADDRESS', 'user@example.org');
$headers = "From: " . $_REQUEST['from'] . "\r\nReply-To: webmaster@example.com";
$mail = mail(TO_ADDRESS, $subject, $message, $headers);
精彩评论