如何设置发件人的电子邮件仅从特定域发送?

时间:2018-05-21 02:36:53

标签: php html email

例如,我只希望发件人的电子邮件来自“anything@abc.com”,如果不允许其他电子邮件如“anything@cde.com”,则只允许使用@ abc.com。我该怎么做?

  

让我们说$ _POST ['Sender'] =“anything@abc.com”;

$to = "someone@example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = $_POST['Sender'];
$headers = "From:" . $from;

mail($to,$subject,$message,$headers);
echo "Mail Sent.";

2 个答案:

答案 0 :(得分:0)

你应该能够将它包装在if语句中:

$to = "someone@example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = $_POST['Sender'];
$headers = "From:" . $from;

if ($from === "anything@abc.com") {
    mail($to,$subject,$message,$headers);
    echo "Mail Sent.";
}

答案 1 :(得分:0)

您可以使用PHP的strstr功能删除电子邮件的第一部分(@之前的所有内容),如下所示:

$from = strstr($_POST["Sender"], "@");

if($from == "@abc.com") {
    // send mail
    mail($to,$subject,$message,$headers);
    echo "Mail Sent.";
}

有关strstr功能

的更多信息,请参阅http://php.net/manual/en/function.strstr.php