根据发送到不同电子邮件的答案

时间:2013-08-26 17:02:22

标签: php email conditional-statements

我需要添加一个条件。如果$contact_subject大于50000,它应该转到一个电子邮件地址,如果它更小,它应该转到另一个电子邮件地址。

if( $contact_name == true )
{
    $sender = $contact_email;
    $receiver = "email@email.com";
    $client_ip = $_SERVER['REMOTE_ADDR'];
    $email_body = "Name: $contact_name \nEmail: $sender \nAccount Number: $contact_subject \nMessage: $contact_message \nIP: $client_ip \nEmail Sent from website http://www.website.com";      
    $extra = "From: $sender\r\n" . "Reply-To: $sender \r\n" . "X-Mailer: PHP/" . phpversion();

    if( mail( $receiver, "Email From Website - $subject", $email_body, $extra ) ) 
    {
        echo "success=yes";
    }
    else
    {
        echo "success=no";
    }
}

2 个答案:

答案 0 :(得分:1)

使用三元语句检查$contact_subject是否大于50000,并根据条件设置$receiver电子邮件地址。

$receiver = ($contact_subject > 50000) ? 'abc@example.com' : 'xyz@example.com';

同样可以使用if-else块来完成。

if ($contact_subject > 50000) {
    $receiver = 'abc@example.com';
}
else {
    $receiver = 'xyz@example.com';
}

答案 1 :(得分:0)

也许更好的方法是将大小放在可变数字中。

-> config.php
    $size_mail = '5000';


-> mail.php
include "config.php";
if( $contact_name == true )
    {

    if($contact_subject > $size_mail)
    {$receiver = 'mailone@nobody.com';}else{$receiver == 'mailtwo@nobody.com';}

        $sender = $contact_email;
        $client_ip = $_SERVER['REMOTE_ADDR'];
        $email_body = "Name: $contact_name \nEmail: $sender \nAccount Number: $contact_subject \nMessage: $contact_message \nIP: $client_ip \nEmail Sent from website http://www.website.com";      
        $extra = "From: $sender\r\n" . "Reply-To: $sender \r\n" . "X-Mailer: PHP/" . phpversion();

        if( mail( $receiver, "Email From Website - $subject", $email_body, $extra ) ) 
        {
            echo "success=yes";
        }
        else
        {
            echo "success=no";
        }
    }