【问题标题】:How to set sender's email send from specific domain only?如何设置仅从特定域发送的发件人电子邮件?
【发布时间】:2018-05-21 02:36:52
【问题描述】:

例如,我只希望发件人的电子邮件来自“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.";

【问题讨论】:

    标签: php html email


    【解决方案1】:

    您应该能够将其包装在 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.";
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用 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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-09-05
        • 1970-01-01
        • 2017-01-08
        • 1970-01-01
        • 2016-03-10
        • 1970-01-01
        • 2017-05-25
        相关资源
        最近更新 更多