2011-06-30 59 views
-1

我在php代码中有一个邮件发送功能,我想使用define来设置地址和地址。如何做到这一点。从HTML表格检索值

的代码是

<?php 

$subject = 'Test email'; 

$message = "Hello World!\n\nThis is my first mail."; 

$headers = "From: $from\r\nReply-To: [email protected]"; 
    //send the email 
    $mail = @mail($to, $subject, $message, $headers); 

?> 

如何定义从$至$和。在此先感谢帮助

+0

你为什么要使用定义为'到'和'从'地址? – GWW

+1

你的问题有点含糊。 “定义”是什么意思?你想让人们填写一个表格并让PHP检索这些值吗? –

+0

知道如何做到这一点。 – work

回答

0

这是一个非常基本的例子。我会留给你做验证。

HTML:

<form action="path/to/your/script.php" method="post"> 
    <input type="text" name="from" /> 
    <input type="submit" value="Send" /> 
</form> 

PHP:

在PHP中,你需要使用$_REQUEST$_POST$_GET取决于你的HTML的action参数form的。如果您不确定,请使用$_REQUEST。在方括号中的值是来自HTML的input属性的name

define('TO_ADDRESS', '[email protected]'); 

$headers = "From: " . $_REQUEST['from'] . "\r\nReply-To: [email protected]"; 
$mail = mail(TO_ADDRESS, $subject, $message, $headers); 
+0

我也想知道是否有任何使用DEFINE用于任何目的@Francois – work

+0

@work - 不是在这种情况下。 define函数用于常量(值不变)。如果用户将输入数据,它将会改变,因此你不想使用'define'。有关更多信息,请参见[http://ca.php.net/define](http://ca.php.net/define)。 –

+0

@ work,您可以将其用于固定值例如例如“to”地址的不变值。 ''定义(“TO_ADDRESS”,“[email protected]”);' – tradyblix

0

除非绝对必要,否则我会建议使用一个很好的老式变量来完成这个特定的任务,而不是一个常量。

如果你想用一个常数:

define('MAIL_TO', '[email protected]'); 
define('MAIL_FROM', '[email protected]'); 

$subject = 'Test email'; 
$message = "Hello World!\n\nThis is my first mail."; 
$headers = "From: " . MAIL_FROM . "\r\nReply-To: [email protected]"; 

$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; 

随着使用常量:

$mailTo = '[email protected]'; 
$mailFrom = '[email protected]'; 
$subject = 'Test email'; 
$message = "Hello World!\n\nThis is my first mail."; 
$headers = "From: " . $mailFrom . "\r\nReply-To: [email protected]"; 

$mailResult = mail($mailTo, $subject, $message, $headers);