开发者

Can one processing file be used for three forms of varying destination addresses?

I'm working on a site which has three contact forms. All forms collect basic information name, email, comment..

My question is that can i use one php file (e.g. send-mail.php) to handle the sending of mail for all three forms even though the $emailto address of each form is different? If yes how do i achievce this? Or do i need one separate file for each form?

$emailTo = 'youremail@yoursite.com'; 
$body = "Name: $name \n\nEmail: $email \n\nSubject: $enquiry";
$headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

mail($emailTo, $subje开发者_高级运维ct, $body, $headers);
$emailSent = true;


Yes, you can. You need to somehow be able to differentiate the forms, and you can do so with a hidden form field on each contact page:

<form method = 'post' action = 'send-mail.php'>
    <input type = 'hidden' name = 'formID' id = 'formID' value = '1'>

Then just check against this type to determine what the to email address should be.

<?php
    if($_POST['formID'] == 1)
        $emailTo = "me@me.com";
    else if($_POST['formID'] == 2)
        $emailTo = "you@you.com";

   // ...

    mail($emailTo, $subject, $body, $headers);
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜