【问题标题】:php mailheaders vs header on website email fromphp mailheaders 与网站电子邮件上的标头来自
【发布时间】:2014-02-24 22:47:02
【问题描述】:

我是 php 和 stackoverflow 的新手,我正在尝试找出一个简单的网站联系表。我的表单和电子邮件功能正常,但我有一个难以解决的问题。

发送电子邮件时,发件人电子邮件显示 myusername@p3pxxxxxx.com 这是我的服务器。当我将电子邮件定向到基于域的电子邮件帐户时,他们不会进入我的收件箱,我敢打赌垃圾邮件过滤器正在阻止奇怪的电子邮件地址。所以我尝试将它发送到我的 gmail 收件箱,这很有效,但我不定期查看该电子邮件。我宁愿把它转到我基于域的电子邮件帐户。

所以,我正在寻找一种方法来编辑“发件人”电子邮件地址。而不是用户/服务器,我希望它使用真实的电子邮件地址,我的或发送它的人会更好。这是我目前的一些尝试,但都没有奏效。

几次尝试:

试图提取我的发件人输入的电子邮件地址。

//$mailheader = "From: ".$_POST["email"]."\r\n"; 
//$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";

这是另一次尝试。

//$mailheaders = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";

我目前拥有的:

<?
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$security = $_POST['security'];

$to = "myemailaddress@gmail.com";
$subject = "Contact Message from Website";
$mailheaders = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
$message = "A visitor of exampledomain.com has submitted the following message.\n\nName: $name\n\nEmail: $email\n\nPhone: $phone\n\nMessage: $message";


if ($security=="10") {
    mail($to,$subject,$message,$mailheaders);
    header("Location:contact.php?s=1");
}
else {
    header("Location:contact.php?s=2");
}
?>

听从了建议。最后一次尝试仍然没有成功...

<?
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$security = $_POST['security'];

$to = "mail@example.com";
$subject = "Contact Message from Website";
$message = "A visitor of exampledomain.com has submitted the following message.\n\nName: $name\n\nEmail: $email\n\nPhone: $phone\n\nMessage: $message";
$mailheaders = "From: webmaster@example.com\r\nReply-To: website@example.com";

if ($security=="10") {
    mail($to,$subject,$message,$mailheaders);
    header("Location:contact.php?s=1");
}
else {
    header("Location:contact.php?s=2");
}
?>

【问题讨论】:

  • 第二次尝试应该可行,但您必须将$mailheader 切换为$message,正如Documentation 指出的那样!
  • 我尝试了这个建议,但结果没有改变。
  • 我在上面的窗口中添加了代码。
  • 为正确性编辑了代码。这两个例子现在都做了你想要的吗?
  • 感谢您的 cmets 我想通了!

标签: php forms email contact


【解决方案1】:

Alastair 的修正应该有效。

当您调用 mail 函数时,您没有按正确的顺序传入变量。

你怎么称呼它: mail($to, $subject, $mailheaders, $message)

但实际顺序是: mail($to, $subject, $message, $mailheaders)

注意我如何切换 $message 和 $mailheaders

你可以查看PHP的文档,例如:

http://us3.php.net/manual/en/function.mail.php

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-18
    • 1970-01-01
    • 2011-04-19
    • 2011-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多