Husna
Husna

Reputation: 1386

Mutiple forms using same php mailer script

I'm working on php mail here mail is working fine. I have multiple forms merge with one single php mail function and I have the same form fields. Now I'm trying for from which form mail is coming I want to add some string or heading in the Gmail so I can understand this mail is coming from which form. HTML

<form action="contact.php" method="POST" class="needs-validation form-horizontal" id="contact" novalidate>
    //here form fileds
</form>

php mail

<?php
// Receiver mail id 
$mail_to = '[email protected]';

// Mail Subject 
$subject = 'test';

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    if ( isset($_POST['first_name']) ) {
        $first_name = $_POST['first_name'];
    }
    // Message body

    $msg = '<html><body><p>';

    $msg .= '<b> First Name : </b>' . $first_name . '<br/>';

    $msg .= '</p>';
    $msg .= '</body></html>';

    // Mail headers
    $headers  = "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
    $headers .= 'From: [email protected]' . "\r\n";

    if( mail( $mail_to, $subject, $msg, $headers )) {
        echo "Thank You!";
    } else {
        die("Error!");
    }
   }
 ?>

Upvotes: 1

Views: 75

Answers (3)

Ali_k
Ali_k

Reputation: 1661

You could add hidden input to your form where you state form name:

<form action="contact.php" method="POST" class="needs-validation form-horizontal" id="contact" novalidate>
    //here form fileds
    <input id="source_form_name" name="source_form_name" type="hidden" value="Some name here">

</form>

Then send the input value with the email:

<?php
// Receiver mail id 
$mail_to = '[email protected]';

// Mail Subject 
$subject = 'test';

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    if ( isset($_POST['first_name']) ) {
        $first_name = $_POST['first_name'];
    }
    // Message body

    $msg = '<html><body><p>';

    $msg .= '<b> First Name : </b>' . $first_name . '<br/>';

    $msg .= '</p>';
    $msg .= '<p>';
    $msg .= '<strong>Form Name: </strong>' . $_POST['source_form_name'];
    $msg .= '</p>';
    $msg .= '</body></html>';

    // Mail headers
    $headers  = "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
    $headers .= 'From: [email protected]' . "\r\n";

    if( mail( $mail_to, $subject, $msg, $headers )) {
        echo "Thank You!";
    } else {
        die("Error!");
    }
   }
 ?>

Upvotes: 2

Andr&#233; Gava
Andr&#233; Gava

Reputation: 268

First create a hidden input in your form in its value you can set the form name, then when you submit your post you can get its value and create a swtich case to pass a value os string mail to each form.

If you need it more dinamicaly, you could create a table caled form, each form has an id, name, string_mail, than when a form is loaded and submited you could get the string mail to send, this way you just need to insert in a database table and dont need a switch case, for every new form you need to inser in database a new value. hope to help u.

Upvotes: 1

codeneuss
codeneuss

Reputation: 905

just add an hidden Field with the "name" of the form you want to submit like:

<form>
  <input type="hidden" name="formname" value="form1"/>
 </form>
 
 <form>
  <input type="hidden" name="formname" value="form2"/>
 </form>

Than you can access the name with $_POST['formname']

Upvotes: 1

Related Questions