如何在php中向管理员和用户发送不同的消息

时间:2017-10-30 05:58:43

标签: php forms email phpmailer

我想向管理员和用户发送不同的电子邮件。我不知道该怎么做。 我与您分享的代码正在向管理员和用户发送相同的电子邮件。请帮助我..

这是我的代码;

$car = $_POST['category'];
$pick = $_POST['text1'];
$drop = $_POST['text2'];
$source = $_POST['text3'];
$email= $_POST['text4'];

$to="$email";
$subject="Web Enquiry";
$message="Hi,". "\r\n" . "\r\n" .
"You've received an email with following details, from the inquiry made at the website- mail@silvertaxi.com" ."\r\n"."\r\n".
"Car Category:"." "."$car"."\r\n".
"Source Location:"." "."$pick"."\r\n".
"Destination Location:"." "."$drop"."\r\n".
"Day and Time.:"." "."$source". "\r\n".
"Email:"." "."$email". "\r\n" ."\r\n". 
"Thanks & Regards,". "\r\n" .
"Web Admin"."\r\n" ;

$headers ="From:$email\n";
$headers .= 'Cc: admin@email.com' . "\r\n";
$headers.="MIME-Version: 1.0\n";
$headers.="Content-type: text/html; charset=iso 8859-1";
if(mail($to, $subject, $message,$headers))
{
    echo "Your Message has been sent." ;
} else {
    echo "";
} 

2 个答案:

答案 0 :(得分:2)

你应该两次调用mail()函数;一个用户,另一个用于管理员。删除行

    $headers .= 'Cc: admin@email.com' . "\r\n";

来自您的代码。 然后,根据需要为用户和管理员定义不同的消息。

    $to="$email";
    $subject="Web Enquiry";
    $message="....." //your message to user goes here

    $to_admin = "admin@email.com";
    $subject_admin = "...."; //subject for mail to admin
    $message_admin = "....." //your message to admin goes here

使用mail()函数两次发送不同的电子邮件。

    if((mail($to, $subject, $message,$headers) && mail($to_admin, $subject_admin, $message_admin, $headers))
     {
      echo "Your Message has been sent." ;
     } else {
      echo "";
     } 

答案 1 :(得分:0)

首先,您需要检查他是用户还是管理员。如果他是用户,您可以为管理员添加另一个条件语句。像这样:

if($user_role == "user"){
   //mail for user
   .....
}
if($user_role == "admin"){
   //mail for admin
   .....
}

如果您有任何问题,请随时提出。