php index problems, doens't send email [duplicate] - php

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
so I'm making a website for a customer and he wants me to make a contact us page, now I'm trying to make it say in the email message: "You have been contacted by $name $name2 and his email is $from. \n The message he wanted to say was in the subject of $message and the message is $message2" help?
my html code:
<form action="contactus.php" method="post">
<div class="form-group">
<label for="Subject">Subject*</label>
<input name="subject" class="form-control" id="subject1" placeholder="Subject">
</div>
<label for="sabject">Why do you want to contact us?</label>
<br>
<select name="select">
<option value="General Inquiry">General Inquiry</option>
<option value="hampion Guide Request">Champion Guide Request</option>
<option value="League of Legends Show(s) Request">League of Legends Show(s) Request</option>
<option value="Podcast - League of Legends">Podcast - League of Legends</option>
</select>
<label for="fname">First Name*</label>
<input type="text" name="firstname" class="form-control" id="namef" placeholder="First Name">
<label for="lname">Last Name</label>
<input type="text" name="lastname" class="form-control" id="namel" placeholder="Last Name">
<label for="email_adress">Email Adress*</label>
<input type="email" name="email_adress" class="form-control" id="email_adress" placeholder="Email Adress">
<label for="message">Message*</label>
<input name="message2" class="form-control" id="message" placeholder="message">
<input style="margin-top: 50px;" value="Send Form" name="submit" type="submit">Submit</button>
</form>
php:
<html>
<head>
<title> PHP script</title>
</head>
<body>
<?php
$to ="sudaiguy1#gmail.com";
$from = isset($_POST['email_adress']) ? $_POST['email_adress'] : '';
$email_subject = isset($_POST['subject']) ? $_POST['subject'] : '';
$message = isset($_POST['select']) ? $_POST['select'] : '';
$message2 = isset($_POST['message2']) ? $_POST['message2'] : '';
$name = isset($_POST['firstname']) ? $_POST['firstname'] : '';
if (empty($name)||empty($from))
{
echo "Name and email are mandatory!";
exit;
}
elseif (empty($email_subject)||empty($message)) {
echo "Your email or subject are blank, please write something in them";
exit;
}
elseif (empty($name)||empty($message2)) {
echo "Your name or message are empty";
exit;
}
$name2 = isset($_POST['lastname']) ? $_POST['lastname'] : '';
$email_from = "sudaiguy1#gmail.com";
$body = $_POST['You have been contacted by $name $name2 and his email is $from. \n The message he wanted to say was in the subject of $message and the message is $message2'];
$headers = "From: $body";
mail($to , $email_subject , $body ,$headers);
?>
</body>
</html>
My problem is that it says unidentified variables index or something like that

The undefined index comes from $body = $_POST['You have be..]
Try this for $body:
$body = 'You have been contacted by ' . $name . ' ' . $name2 . ', and his email is ' . $from . 'The message he wanted to say was in the subject of' . $message . ' and the message is ' . $message2;

Related

How to add HTML attach-file to PHP?

I'm trying to create contact form that would let me attach file. I'm very new to PHP. I have tried looking for youtube videos but was not able to find any ussefull information. I'm hoping that you could help me with my PHP.
Also if there is better alternataive or the way of doing it , please share it with me.
HTML
<form style="display:flex; flex-direction: column;" action="../mail/mail.php" method="post">
<input type="text" id="name" name="name" placeholder="Your full name.." required="required" />
<label for="email">Email</label>
<input type="email" id="email" name="email" placeholder="Your e-mail.." required="required" />
<label for="country">Country</label>
<select id="country" name="country" required="required">
<option value="Netherlands">Netherlands</option>
<option value="Saint Kitts and Nevis">Saint Kitts and Nevis</option <option value="Tajikistan">Tajikistan</option>
</select>
<label for="subject">Subject</label>
<select id="subject" name="subject" required="required">
<option value="Choose">Click here to select..</option>
<option value="Choose1">Click here to select1..</option>
<option value="Choose2">Click here to select.2.</option></select>
<div class="attachment-row">
<input id="attachment-file" type="file" class="input-field" name="attachment[]">
</div>
<label for="message">Message</label>
<textarea id="message" name="message" placeholder="Write your message here.."style="height:200px"></textarea>
<input type="submit" class="btn-send-message" name="submit" value="Send" /></form>
PHP
<?php
ob_start();
if(isset($_POST['submit'])){
$to = "info#example.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$name = $_POST['name'];
$country = $_POST['country'];
$subject = $_POST['subject'];
$message = "From: ". $from . "\n\n" . "Subject: ". $subject . "\n\n" . "Country: ". $country ."\n\n"."Name: ". $name ."\n\n". "Wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $name . "\n\n" . $_POST['message'];
mail($to,$subject,$message);
mail($from,$subject2,$message2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $name . ", we will contact you shortly.";
header("Location: ../thank-you-for-contacting.php");
}
ob_end_flush();
?>
First of all you don't need the output buffer at all. What you're doing here is simply loading a single echo line into memory and dumping it on the end.
Have a read on:
https://www.php.net/manual/en/function.ob-start.php
Your current code doesn't even look for attachments, you'd need something along the lines of:
if (count($_POST['attachment'])) {...}
Next thing you need to figure out is the structure of the email itself. For basics have a quick read on:
https://www.php.net/manual/en/function.mail
Keeping the headers and message itself separate is always a good idea, creating variables only used once is a bad one. SO you might try something like:
if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$headers[] = 'To: '.filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
}
// They're both really bad filters, for application security You should do your research and define your own. Character encoding is a big deal btw.
Once you have a email formatted properly you should look at the attachments mentioned earlier. To understand how those are sent have a read on:
https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html
TLDR; // If you want to code things, nothing is too long... but this should provide some clarity
header[] = "Content-Type: multipart/mixed; boundary=$UNIQUE_string";
$message = "--$UNIQUE_string".PHP_EOL;
$message .= "Content-type: text/plain;".PHP_EOL;
$message .= $content.PHP_EOL;
$message .= "--$UNIQUE_string".PHP_EOL;
$message .= 'Content-Disposition: form-data; name="myFile"; filename="foo.txt"'.PHP_EOL;
$message .= "Content-Type: text/plain".PHP_EOL;
$message .= file_get_contents('foo.txt').PHP_EOL;
$message .= "--$UNIQUE_string--".PHP_EOL;

How to get multi field form data in email body

Complete newbie at PHP. help.
I have written a large "join the club" form with 15 fields. the first 10 fields are required. last 5 are optional.
Each field has ID and names. name="fname" name="lname", for each field. (see below)___
<!-- FORM STARTS HERE -->
<form id="appliform" name="applform" method="post"
action="joinformscripts/test11.php"
enctype="text/plain" >
<!-- php code still in progress. test memb.php --->
<label for="fname">*First Name:</label><br>
<input type="text" id="fname" name="fname" required><br>
<label for="lname">*Last Name:</label><br>
<input type="text" id="lname" name="lname" required><br>
<label for="bizname">*Business Name:</label><br>
<input type="text" id="bizname" name="biz" required><br>
<label for="bizadd">*Business Mailing Address:</label><br>
<input type="text" id="bizadd" name="bizadd" required> <br>
<label for="city">*City:</label><br>
<input type="text" id="city" name="city" required> <br>
<label for="state">*State:</label><br>
<input type="text" id="state" name="state" required> <br>
<label for="zip">Zip:</label><br>
<input type="text" id="zip" name="zip" required> <br>
<label for="bizphone">*Business Phone:</label><br>
<input type="text" id="bizphone" name="bizphone" required> <br>
<label for="celphone">Personal Phone:</label><br>
<input type="text" id="celphone" name="celphone"> <br>
<label for="email">*Email:</label><br>
<input type="email" id="email" name="email" required><br>
<hr>
<h5>Complete this information if you are also paying for an Associate Membership. </h5>
<label for="fname">Associate First Name:</label><br>
<input type="text" id="afname" name="afname"><br>
<label for="lname">Associate Last Name:</label><br>
<input type="text" id="alname" name="alname"><br>
<label for="bizphone">Associate Business Phone:</label><br>
<input type="text" id="abizphone" name="abizphone" > <br>
<label for="celphone">Associate Personal Phone:</label><br>
<input type="text" id="acelphone" name="acelphone"> <br>
<label for="aemail">Associate Email:</label><br>
<input type="aemail" id="aemail" name="aemail"><br>
<hr>
<p>test 11 # 6.00p</p>
<!-- SEND BUTTON Submit using secure POST DATA button
when sent, opens remit payment page (in script)-->
<input type="submit" value="submit application" ><br>
<input type="reset" value="clear form">
<br>
<br>
<p>Please remit payment on the next page.</p>
</form> <!-- END MEMBERSHIP FORM with script action -->
PHP script.
<?php
// test 11
// after submit, goes to Remit page for payment: yes. //
// receive email with content in body: NO //
$email_to = "membership#xxxx.org";
$email_from = "membership#xxxx.org";
$reply_to = $_POST["email"];
$headers = "submitted by: .$mailfrom .$fname .$lname ";
$email_subject = "New Member Application Form (phptest8) revised 6.00pm thursday dec 10";
$headers = "From: " . $email_from . "\n";
$headers .= "Reply-To: " . $reply_to . "\n";
// NOT CAPTURING form variables.
//This needs to send all the fields in the form on Join page.
if(isset($_POST['submit'])){
$to = "membership#efwba.org"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['fname'];
$last_name = $_POST['lname'];
$subject = "Membership Form submission";
$message = $first_name . " " . $last_name . "
wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "my information " . $fname . "\n\n" . $fname . $lname . $biz . $bizadd . $city . $state . $zip . $bizphone . $celphone . $email . $afname . $alname . $abizphone . $acelphone. $aemail. $_POST['message'];
}
//Send the email. If the email is sent we will go to a REMIT PAYMENT page, if there is an error we will display a brief message.
ini_set("sendmail_from", $email_from);
$sent = mail($email_to, $email_subject, $email_body, $headers, $email_from);
if ($sent)
{
header("Location:/joinformscripts/thank-you-for-joining.html");
} else {
echo "There has been an error submitting your application. Please go back one page to verify all data has been entered correctly.";
}
// Function to validate against any email injection attempts
function IsInjected($str)
{
___
The host server -goDaddy, Cpanel- processes the php file and sends the email to the "Membership#" account.
After the form submits, it redirects to the Payment page, that part works!
However, I am NOT getting the field data inside the body of the email.
I see the starter line, but not what the applicant entered for contact info.
I want the body/ data to be a simple format, so I can copy it all and drop into a spreadsheet (for club mail merge printing)
It does not need to show the data labels.
I want to see this data in the email body:
"I want to join your organization!"
fname lname
biz
bizaddress
city state zip
bizphone
celphone
bizemail
QUESTION: what is the simplest method to capture each field into email_body?
Thank you!

Contact form script not running, white page

Getting a white screen when trying to submit my contact form which has these entries:
- Name
- Email
- Subject
- Message
Im attempting to recieve emails through my website. Ive checked all variable names and such and it seems that everything is correct. Im new to PHP so im a little cloudy on what to try next.
Thanks
<form method"POST" action="action/form-submit.php"> <!--NO FOR ATTRIBUTE, NOT ADDING FUNCTIONALITY-->
<h2>Contact Me:</h2>
<label>Your Name:</label>
<input name="name" type="text" placeholder="Your Name..." required/>
<label>Email:</label>
<input name="email" type="email" placeholder="Email..." required/>
<label>Query Type:</label>
<select id="qry" name="query" required>
<option value="" disabled selected>Please Select:</option>
<option value="jobs">Jobs</option>
<option value="website">Website Issues</option>
<option value="info">Information</option>
</select>
<label>Your Message:</label>
<textarea name="info" placeholder="Your Message..." required></textarea>
<input type="submit" value="Submit">
</form>
Then the PHP code:
<?php
$vname = $_POST['name'];
$vemail = $_POST['email'];
$vquery = $_POST['query'];
$vmessage = $_POST['info'];
$email_from = "test#gmail.com";
$email_subject = "New Website Submission";
$email_body = "Visitor Name: $vname.\n".
"Visitor Email: $vemail.\n".
"Visitor Subject: $vquery.\n".
"Visitor Message: $vmessage.\n";
$to = "bradleyarcher98#gmail.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $vemail \r\n";
mail($to,$email_subject,$email_body,$headers);
header("location: contact.html");
?>
In order to see what is wrong, you need to turn on the PHP error handling.
When this is on, you won't just see a white page anymore, an error message with filename, line number and a message describing the error is sent to the browser.
<?php
session_start();
$vname = $_POST['name'];
$vemail = $_POST['email'];
$vquery = $_POST['query'];
$vmessage = $_POST['info'];
$subject = " YOUR MESSAGE Title ";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "From: "YOUR SITE NAME" . "\r\n";
$message = "
<!-- you can add CSS Aswell -->
<div>
<p>Visitor Name: ".$_POST['name']."<p>
<p>Visitor Email: ".$vemail."<p>
<p>Subject: ".$vquery."<p>
<p>Message :".$vmessage."<p>
</div>
";
mail($to,$subject,$message,$headers);
?>

How to apply Google reCAPTCHA (2.0) to my current HTML/PHP contact form

I am trying to apply Google reCAPTCHA to my HTML/PHP contact form. The reCAPTCHA appears on the page and works, but I cannot get it to work with the form.
Can somebody please tell me how to add the appropiate PHP code to integrate the recaptcha into my contact form?
<form action="submit.php" method="POST">
<input class="textbox" type="text" name="fullname" placeholder="Your Name">
<input class="textbox" type="email" name="email" placeholder="E-Mail">
<input class="textbox" type="tel" name="telephone" placeholder="Telephone">
<input class="textbox" type="text" name="business" placeholder="Organization">
<textarea class="comments_box" name="visitor_message" placeholder="Message"></textarea>
<div class="g-recaptcha" data-sitekey="6LddnxAUAAAAAAi6jmmUcX8pPMfSELRgpNUAI2Ra"></div>
<br>
<input class="submitBtn" type="submit" value="SUBMIT">
</form>
<?php
if (isset($_POST['fullname']) && isset($_POST['email']) && isset($_POST['telephone']) && isset($_POST['business']) && isset($_POST['visitor_message'])) {
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$telephone= $_POST['telephone'];
$business = $_POST['business'];
$visitor_message = $_POST['visitor_message'];
if (!empty($fullname) && !empty($email) && !empty($telephone) && !empty($business) && !empty($visitor_message)) {
$to = 'acramirez38#gmail.com';
$subject = 'Trucker Radio Talk Visitor Message';
$body = "Full Name: " . $fullname ."\n". "E-Mail: " . $email ."\n". "Telephone: " . $telephone ."\n". "Business: " . $business ."\n". "Comments: " . $visitor_message;
$headers = 'From: ' .$email;
if (mail($to, $subject, $body, $headers))
echo 'Thank you for contacting us! Your message has been successfully delivered.';
} else {
echo 'All fields are required! Please return to the previous page and revise.';
}
}
?>

PHP contact form seems to work, not receiving email

Not a PHP expert. With that said, I've used this system before and it seemed to stop working one day. I can't seem to get emails sent anymore even if I'm not getting any error messages. I don't know what's wrong.
Form page:
<form method="POST" name="contactform" action="contact-form-handler.php">
<label for='name'>Your Name:</label> <br>
<input type="text" name="name">
<label for='email'>Email Address:</label> <br>
<input type="text" name="email"> <br>
What does this concern?<br>
<select name="options">
<option value="null">--</option>
<option value="IEP">IEP</option>
<option value="Section 504">Section 504</option>
<option value="Other">Other</option>
</select>
<p style="width: 50%">Please include in your message what type of service you are inquiring about. Please be as descriptive as possible so we can help you more efficiently. Thank you.</p>
<label for='message'>Message:</label> <br>
<textarea name="message" cols="45" rows="7"></textarea>
<input type="image" src="img/submit.png" alt="Submit"> <br>
</form>
Handling:
<?php
$errors = '';
$myemail = 'louie540x#gmail.com;';
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['options']) ||
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
$options = $_POST['options'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "\n Error: Invalid email address";
}
if( empty($errors))
{
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n Email: $email_address \n Regarding a(n): $options \n \n Message: \n \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.php');
}
?>
<?php include 'template/top.php'; ?>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>
<?php include 'template/bottom.php'; ?>
$myemail = 'louie540x#gmail.com;';
Should be:
$myemail = 'louie540x#gmail.com';
However that may not be your only problem...

Categories