【问题标题】:Change email "anonymous" sent from my website更改从我的网站发送的“匿名”电子邮件
【发布时间】:2019-07-31 17:56:27
【问题描述】:

我的联系表单有问题。电子邮件已正确发送,但在我的收件箱中显示为“发件人:anonymous@web.godns.net”而不是“发件人:web@mywebsite.com”,并直接进入垃圾邮件。

我一直在寻找类似的问题,但没有人给出具体的答案。我猜是代码有语法错误还是服务器问题。

这是 PHP 文件:

<?php
// Check for empty fields
if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['phone']) || empty($_POST['message']) || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
  http_response_code(500);
  exit();
}

$name = strip_tags(htmlspecialchars($_POST['name']));
$email = strip_tags(htmlspecialchars($_POST['email']));
$phone = strip_tags(htmlspecialchars($_POST['phone']));
$message = strip_tags(htmlspecialchars($_POST['message']));

// Create the email and send the message
$to = "myemail@gmail.com"; 
$subject = "Mensaje Web de >> $name";
$body = "Recibiste mensaje a través del formulario en la web.\n"."Estos son los datos:\n\nNombre y Apellido: $name\n\nEmail: $email\n\nTeléfono: $phone\n\nTexto del mensaje:\n$message";
$headers= "From: web@mywebsite.com" . "\r\n" . 
    "Reply-To: $email \r\n" .
    'X-Mailer: PHP/' . phpversion(); 
if(!mail($to, $subject, $body, $headers))
  http_response_code(500);
?>

有人知道这里出了什么问题吗? 谢谢,

亚历杭德拉 | aleare.design

【问题讨论】:

    标签: php email


    【解决方案1】:

    它可能会解决您的问题,但设置正确的发件人地址始终是一个好主意。在good old mail() 中,需要使用$additional_parameters 参数来完成,语法是-f,后跟一个没有显示名称的电子邮件地址:

    mail($to, $subject, $body, $headers, '-fweb@mywebsite.com')
    

    此外,请确保您的本地 SMTP 服务器允许以 web@mywebsite.com 的名义发送邮件。如果没有,也许您需要获取另一台服务器并使用身份验证,这是mail() 不允许的。

    无论如何,使用此功能很难正确接收电子邮件,因为您需要自己完成所有操作,并且电子邮件协议并非易事。例如,我认为您的代码容易受到email headers injection 的攻击。使用 Swift Mailer 或 PHPMailer 等第三方库会更容易。

    附:你想用strip_tags(htmlspecialchars()) 完成什么?这只会使用户输入不可读,因为没有明显的收益。

    【讨论】:

    • 基本上他正在尝试创建一个联系表格。联系表格包含客户姓名、电子邮件、电话和消息等。就像普通的联系表格一样。因此,他的管理员/PIC 可以跟进输入表单的任何人。所以strip_tags(htmlspecialchars()) 应该去掉所有的 html 属性,只留下用户输入的消息/文本。
    • @aleare.design 然后,除非我忽略了一个错字,否则这一切都表明您的托管服务施加了限制。也许您可以向他们的支持团队澄清这一点。
    【解决方案2】:

    这可能对你有帮助,我不明白为什么你在标题中放了这么多文字

    // Create the email and send the message $to = "youremail@yourdomain.com"; // Add your email address inbetween the " " replacing username@yourdomain.com - This is where the form will send a message to. $subject = "Website Contact Form: $name"; $body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email\n\nPhone: $phone\n\nMessage:\n$message"; $header = "From: noreply@adamar.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com. $header .= "Reply-To: $email";// this is to automatically reply to your correspondent email rather than your noreply email, I separate it from your header.

    还介意将变量 $headers 更改为 $header 吗?也许它可能会有所帮助。祝一切顺利。干杯!

    【讨论】:

    • 您是否尝试过复制和粘贴代码?尝试找出情况。如果没有解决问题。请尝试联系您的托管服务器服务。因为我刚刚尝试了代码,它可以工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-15
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-21
    相关资源
    最近更新 更多