简体   繁体   中英

How to set sender's email send from specific domain only?

For example, i only want the sender's email is from "anything@abc.com", if other email like "anything@cde.com" is not allow, only @abc.com is allowed. How should i do that?

let say $_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."; 

you should be able to just wrap it within an if statement:

$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.";
}

You can use PHP's strstr function to strip the first part of the email off (everything before @) like so:

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

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

See http://php.net/manual/en/function.strstr.php for more information on the strstr function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM