gables20
gables20

Reputation: 496

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 = '[email protected]'; 
$body = "Name: $name \n\nEmail: $email \n\nSubject: $enquiry";
$headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

mail($emailTo, $subject, $body, $headers);
$emailSent = true;

Upvotes: 0

Views: 33

Answers (1)

Jeff Lambert
Jeff Lambert

Reputation: 24671

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 = "[email protected]";
    else if($_POST['formID'] == 2)
        $emailTo = "[email protected]";

   // ...

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

Upvotes: 2

Related Questions