如何将发件人电子邮件地址添加到 PHP 表单?

发布于 2024-10-16 20:14:39 字数 1329 浏览 2 评论 0原文

我的电子邮件处理器正在工作...但是如何显示发件人电子邮件地址而不是“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

看春风乍起 2024-10-23 20:14:39

将其添加到您的文件中:

$headers = 'From: ' . $your_email . "\r\n" .
'Reply-To: ' . $your_email . "\r\n" .  
'X-Mailer: PHP/' . phpversion();

然后,当您调用邮件命令时,请使用以下命令:

mail($your_email,$email_subject,$email_content,$headers);

Add this to your file:

$headers = 'From: ' . $your_email . "\r\n" .
'Reply-To: ' . $your_email . "\r\n" .  
'X-Mailer: PHP/' . phpversion();

Then when you call the mail command use this:

mail($your_email,$email_subject,$email_content,$headers);
浮世清欢 2024-10-23 20:14:39

默认情况下,PHP [ mail() function ] 发送的电子邮件使用您的服务器作为发件人。
要更改发件人,您必须修改电子邮件的标头。
您可以这样做:

$emailHeader = "From: [email protected]" . "\r\n" . "Reply-To: [email protected]" . "\r\n";
// add the header argument to mail function
mail($your_email,$email_subject,$email_content,$emailHeader);

请注意,我们向 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:

$emailHeader = "From: [email protected]" . "\r\n" . "Reply-To: [email protected]" . "\r\n";
// add the header argument to mail function
mail($your_email,$email_subject,$email_content,$emailHeader);

Note that we added a fourth argument to the mail function.

极度宠爱 2024-10-23 20:14:39

好吧...我会说删除整个 $values = array.... 行并替换为

$values = array();
foreach($_POST as $k => $v) if(strtolower($k) != 'submit') $values[$k] = htmlspecialchars($v);

将所有 POST 数据添加到 $values 数组中
你的内容生成器应该看起来像这样

foreach($values as $key => $value){
    if(in_array($key,$required) && trim($value)==''){
        echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; 
    }
    $email_content .= $key.': '.$value."\n"; // makes more sense
}

希望我能正确回答你的问题oO

okay... i'd say strip the whole $values = array.... line and replace is with

$values = array();
foreach($_POST as $k => $v) if(strtolower($k) != 'submit') $values[$k] = htmlspecialchars($v);

that add's ALL your POST data to the $values array
and your content generator should look like this

foreach($values as $key => $value){
    if(in_array($key,$required) && trim($value)==''){
        echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; 
    }
    $email_content .= $key.': '.$value."\n"; // makes more sense
}

hope i got your question right oO

烟燃烟灭 2024-10-23 20:14:39

嘿。顺便说一句:从我这里看到的,

        if ($key != 'subject' && $key != 'company') { if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }

没有意义。
你的键将始终是: 0 到 count($values) ...你没有关联数组。或者这个 $values 数组只是用于测试目的?

Hey. Just by the way: from what i see here,

        if ($key != 'subject' && $key != 'company') { if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文