douju7765 2019-07-31 17:56
浏览 60

更改从我的网站发送的电子邮件“匿名”

I'm having an issue with a contact form. The email message is correctly sent but in my inbox appears as "From: anonymous@web.godns.net" and not "From: web@mywebsite.com", and goes directly to SPAM.

I've been looking for similar issues but no one gives the specific answer. I guess if the code is having a syntax mistake or it's a server problem.

This is the PHP file:

<?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.
"."Estos son los datos:

Nombre y Apellido: $name

Email: $email

Teléfono: $phone

Texto del mensaje:
$message";
$headers= "From: web@mywebsite.com" . "
" . 
    "Reply-To: $email 
" .
    'X-Mailer: PHP/' . phpversion(); 
if(!mail($to, $subject, $body, $headers))
  http_response_code(500);
?>

Does anyone figure out what's wrong here? Thank you,

Alejandra | aleare.design

  • 写回答

2条回答 默认 最新

  • dongxingguo1978 2019-07-31 18:24
    关注

    It may solve your problem or not but it's always a good idea to set a proper sender address. In good old mail() that needs to be done with the $additional_parameters parameter and the syntax is -f followed by an email address with no display name:

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

    Additionally, make sure that your local SMTP server allows sending messages in the name of web@mywebsite.com. If it doesn't, perhaps you need to get another server and make use of authentication, something that mail() doesn't allow.

    In any case, it's really hard to get email right with this function since you need to do everything by yourself and email protocol is not trivial. For instance, I think your code is vulnerable to email headers injection. It's easier to just use a third-party library like Swift Mailer or PHPMailer.

    P.S. What are you trying to accomplish with strip_tags(htmlspecialchars())? This will just make user input unreadable for not obvious gain.

    评论

报告相同问题?

悬赏问题

  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景