如何将发件人电子邮件地址添加到 PHP 表单?
我的电子邮件处理器正在工作...但是如何显示发件人电子邮件地址而不是“myhost”?我收到[电子邮件受保护]。因此,当有人填写表格时,我希望他的电子邮件作为回复地址。
<?php
if(!$_POST) exit;
$email = $_POST['email'];
//$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){
$error.="Invalid email address entered";
$errors=1;
}
if($errors==1) echo $error;
else{
$values = array ('name','email','telephone','company','message');
$required = array('name','email','telephone','message');
$your_email = "myemail@somehost";
$email_subject = "New Message: ".$_POST['subject'];
$email_content = "new message:\n";
foreach($values as $key => $value){
if(in_array($value,$required)){
if ($key != 'subject' && $key != 'company') { if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
}
$email_content .= $value.': '.$_POST[$value]."\n";
}
}
if(@mail($your_email,$email_subject,$email_content)) {
echo 'Message sent!';
} else {
echo 'ERROR!';
}
}
?>
My e-mail processor is working... but how do I get the From e-mail address to show up instead of "myhost"? I'm getting [email protected]. So, when someone fills in the form, I want his e-mail to be the reply address.
<?php
if(!$_POST) exit;
$email = $_POST['email'];
//$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){
$error.="Invalid email address entered";
$errors=1;
}
if($errors==1) echo $error;
else{
$values = array ('name','email','telephone','company','message');
$required = array('name','email','telephone','message');
$your_email = "myemail@somehost";
$email_subject = "New Message: ".$_POST['subject'];
$email_content = "new message:\n";
foreach($values as $key => $value){
if(in_array($value,$required)){
if ($key != 'subject' && $key != 'company') { if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
}
$email_content .= $value.': '.$_POST[$value]."\n";
}
}
if(@mail($your_email,$email_subject,$email_content)) {
echo 'Message sent!';
} else {
echo 'ERROR!';
}
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将其添加到您的文件中:
然后,当您调用邮件命令时,请使用以下命令:
Add this to your file:
Then when you call the mail command use this:
默认情况下,PHP [ mail() function ] 发送的电子邮件使用您的服务器作为发件人。
要更改发件人,您必须修改电子邮件的标头。
您可以这样做:
请注意,我们向 mail 函数添加了第四个参数。
By default, the email sent by PHP [ mail() function ] uses as sender your server.
To change the sender, you must modify the header of email.
You can do it this way:
Note that we added a fourth argument to the mail function.
好吧...我会说删除整个
$values = array....
行并替换为将所有 POST 数据添加到 $values 数组中
你的内容生成器应该看起来像这样
希望我能正确回答你的问题oO
okay... i'd say strip the whole
$values = array....
line and replace is withthat add's ALL your POST data to the $values array
and your content generator should look like this
hope i got your question right oO
嘿。顺便说一句:从我这里看到的,
没有意义。
你的键将始终是: 0 到 count($values) ...你没有关联数组。或者这个
$values
数组只是用于测试目的?Hey. Just by the way: from what i see here,
does not make sense.
your key's will always be: 0 to count($values)... you don't have an associative array. or is this
$values
array just for testing purposes?