Send email with contents of multiple fields

I have this as a file called: sendform.php:
if($_POST[“job_title”]) {
mail(“[email protected]”, “THIS is the subject line”,
$_POST[“job_title”]. “\nsome text here”);

The following page ‘contact.php’ is successful in sending an email to ‘[email protected]’ containing the contents of field ‘job_title’ and (on a new line) the words ‘some text here’.

What I need is a version of sendform.php which includes in the email body:
– every formfield in contact.php
– each one preceded by the name of the field (eg ‘job_title: HEAD OF OPS’ where HEAD OF OPS is what the user has typed into that field.

<form method="post" action="sendform.php">           
<div class="w3-container pnc-label"><label for="job_title"><b>JOB TITLE:</b></label><br>
  <input style="margin-left:1em;background: #fff;padding-left:.4em;" type="text" id="job_title" name="job_title" placeholder="click and type here the JOB TITLE...this message will then disappear">

<div class="w3-container pnc-label"><label for="employer_name"><b>Employer Name:</b></label><br>
  <input style="margin-left:1em;background: #fff;padding-left:.4em;" type="text" id="employer_name" name="employer_name" placeholder="Name of Employing Organisation...">

<div class="w3-container pnc-label"><label for="employment_location"><b>Location of Employment:</b></label><br>
  <input style="margin-left:1em;background: #fff;padding-left:.4em;" type="text" id="employment_location" name="employment_location" placeholder="(optional) Location of employment...">
        <textarea rows="5" cols="20" name="message"></textarea>

        <div class="w3-center">
             <button type="submit" 
                 class="w3-teal w3-hover-green 
                 w3-round w3-button">

Well, this is easy to do, but, here is a tutorial on how it all works. Once you get some code set up, post it here and we can help you fix it all.

Thanks, ErnieAlex.
Following that tutorial helped me, and I now have code that sends the email the way I want it to. Yay!

One further problem has shown up…
I am using the following code:

$email_body = "
Email is: $email\n
Job Title is: $job_title\n
UID is: $pnc_uid";
    $to = "[email protected]";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
//done. redirect to thank-you page.
header('Location: success.php');

This works OK, and sends an email with the contents of 3 fields.
PROBLEM: I can include ‘plain text’ such as "Email is: " before the $email value, but I also want to include some other plain text after the variable.
AND I CANNOT get it right!
Could you please rewrite this line:

to add “hooray, you’ve done it!!” after $email in $email_body and before the \n.
Thank you

This is PHP 101 stuff… (concatentaing strings & variables)

Some info:
Strings between double quotes "" interpolate, meaning they convert escaped characters to printable characters.

Strings between single quotes '' are literal, meaning they are treated exactly as the characters are typed in.

Email is: $email\n
$email_body =  "Email is: $email hooray, you’ve done it!!\n";
$email_body .=  "Job Title is: $job_title\n";
$email_body .=  "UID is: $pnc_uid";

Alternate approach:

$email_body =  'Email is: ' . $email .' hooray, you’ve done it!! \r\n';
$email_body .= 'Job Title is: ' . $job_title . ' \r\n';
$email_body .=  'UID is: $pnc_uid';

@whispers, he IS in the beginners-learning section, so yes, this is PHP 101 here !

Stowman, Whispers is correct about the quotes and double-quotes. I also prefer to do it his way.
It is easier to read, too. Now, with that said, you must be sending out TEXT emails? Normally most sites send out HTML emails these days. They let you add pictures, use CSS and overall make it a much more “pretty” email. How many “text” emails do you get in your in box? Just a thought.

Thanks, Whispers.
I’ve used your first approach, and not only does it work for me, I think I understand why it works!
I knew about “.” for concatenation, but your alternate approach seems easier for me to NOT get mixed up. The following allows me to combine variables and text into each line inside one set of double-quotes, with (I am assuming) the use of .= to “build up” the one variable using multiple lines.

$email_body =  "Email is: $email hooray, you’ve done it!!\n";
$email_body .=  "Job Title is: $job_title\n";
$email_body .=  "UID is: $pnc_uid";
1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service