PHP create Form to CSV and Send to email as attachment - php

I am creating a webpage that has a form, and in that form I need to save it as csv and I need it to be sent to an email as an attachment file, how do I do that?
First how can I output all of the inputs to csv, then save it as csv file and automatically attach it to email.
Below is the code I've tried :
PHP
<?php
// define variables and set to empty values
$nameErr = $emailErr = $countryErr = $confirmErr = "";
function clean_text($string)
{
$string = trim($string);
$string = stripslashes($string);
$string = htmlspecialchars($string);
return $string;
}
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "sample#email";
$email_subject = "Form Submission";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
exit();
}
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['country']) ||
!isset($_POST['confirm_email'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$country = $_POST['country']; // not required
$confirm_email = $_POST['confirm_email']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$name)) {
$error_message .= 'The Name you entered does not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Subsciber Details Information.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Full Name: ".clean_string($name)."\n";
$email_message .= "Country: ".clean_string($country)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Confirmed Email: ".clean_string($confirm_email)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
/*Autoreply Sender Name*/
$headerss = "FROM: APSR2020 <2020#coac.co.jp>\r\n";
/* Prepare autoresponder subject */
$respond_subject = "Sign-up for APSR 2020 Mailing List Completed";
/* Prepare autoresponder message */
$respond_message = "Thank you for your interest in sign-up for our mailing list.
We will keep you updated on APSR 2020.
In the case that this email is unexpected, it will be troubling, but please inquire through the given address.
Congress Secretariat of APSR 2020
subs-apsr 2020#coac.co.jp
Please keep get in touch with: https://apsr2020.jp
";
/* Send the response message using mail() function */
mail($email_from, $respond_subject, $respond_message, $headerss);
//redirect to the 'thank you' page
header('Location: thank-you-page.html');
?>
<!-- include your own success html here -->
<?php
}
?>
HTML form
<form name="contactform" method="post">
<div class="keep__me__posted">
<p>Please sign up NOW.</p>
<p class="margin__p">We will keep you updated on APSR 2020.</p>
<div class="input__form">
<label for="name">Name</label>
<input type="text" id="name" name="name" maxlength="50" size="30" value="" required>
</div>
<div class="input__form">
<label for="country">Country</label>
<input type="text" id="country" name="country" maxlength="30" size="30" value="" required>
</div>
<div class="input__form">
<label for="email">Email Address</label>
<input type="email" id="email" name="email" maxlength="80" size="30" value="" required>
</div>
<div class="input__form">
<label for="confirm_email">Confirm Email</label>
<input type="email" id="confirm_email" name="confirm_email" maxlength="30" size="30" required>
</div>
<div class="input__form submit">
<tr>
<td colspan="2" style="text-align:center">
<input type="submit" value="Subscribe" class="submit" onclick="checkEmail()">
</td>
</tr>
</div>
</div>
</form>

To write in your csv file, you can take a look at
this function :
$file = fopen('yourFile.csv', 'w');
fputcsv($file, array('this','is some', 'csv "stuff", you know.'));
fclose($file);
To send this file with the mail, I think the easier option is to use PHPMailer.
Don't really know how to use it, but you can easily find it out here. You firstly need to download and instal the package found on gitHub and then use it like this :
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$email = new PHPMailer();
$email->SetFrom('you#example.com', 'Your Name'); //Name is optional
$email->Subject = 'Message Subject';
$email->Body = $bodytext;
$email->AddAddress( 'destinationaddress#example.com' );
$file_to_attach = 'PATH_OF_YOUR_FILE_HERE';
$email->AddAttachment( $file_to_attach , 'NameOfFile.pdf' );
return $email->Send();

Related

$error_message isn't showing when checkbox is empty

<form name="contactform" method="post" action="send_form_email.php">
<!--Name input-->
<input placeholder="Full Name *" type="text" name="name" maxlength="50">
<br />
<!--Email input-->
<input placeholder="Email Address *" type="email" name="email" maxlength="80">
<br />
<!--Phone number input (not required)-->
<input placeholder="Telephone Number" type="tel" name="telephone" maxlength="15">
<br />
<!--Company name input (not required)-->
<input placeholder="Company" type="text" name="company" maxlength="50">
<br />
<!--Comments & messege input-->
<textarea placeholder="Please include as much information as possible *" name="comments" maxlength="500" cols="25" rows="5"></textarea>
<br />
<!--Check for privacy policy-->
<label class="GDRP">
I consent to having this website store my submitted information so they can respond to my inquiry.
<input type="checkbox" name="GDRP">
</label>
<!--Submit button-->
<input type="submit" value="SUBMIT">
</form>
This is my HTML - I don't know how important it is to helping me find a solution but I figured, why not. I tried cleaning it up so you can get through with helping me as quick as possible. The only important fields are the 'name', 'email', 'comments' and the question at matter 'GDRP'.
<?php if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "email#adress.com";
$email_subject = "www.Adress.com - CONTACT FORM";
function died($error) {
// error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted: ";
echo $error."<br />";
echo "Please fix these errors.";
die();
}
// validation expected data exists
if(
!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['company']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments']) ||
!isset($_POST['GDRP'])
) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$company = $_POST['company']; // not required
$comments = $_POST['comments']; // required
$GDRP = $_POST['GDRP']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$name)) {
$error_message .= '<br /><br /><font color="red">Full Name</font><br /><br />';
}
if(!preg_match($email_exp,$email_from)) {
$error_message .= '<font color="red">Email Address</font><br /><br />';
}
if(strlen($comments) < 2) {
$error_message .= '<font color="red">Comments</font><br /><br />';
}
if(!empty($GDRP)) {
$error_message .= '<font color="red">GDRP Agreement</font><br /><br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Company: ".clean_string($company)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<body>
Thank you for contacting us. We will be in touch with you as soon as possible.
</body>
<?php
}
?>
My problem is that the checkbox ('GDRP') isn't showing $error_message when it's not filled in. In fact, none show when 'GDRP' is empty. If you fill 'GDRP' but leave the others [required fields] empty all of their $error_message except for 'GDRP' will show.
if(!empty($GDRP)) { // if not empty
$error_message .= '<font color="red">GDRP Agreement</font><br /><br />';
}
looks like you are checking that $GDPR isn't empty, this should be the other way around.
change if(!empty($GDRP)) { to if(empty($GDRP)) {.
Only checked checkboxes send and available in POST. So, looks like in the following lines your script finished and other errors not shown:
!isset($_POST['GDRP'])
) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}

Sending an html form email with PHP [duplicate]

This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
Closed 5 years ago.
When I click my submit button, It pulls up my form-to-email.php source code. This is my first time using any PHP. How do i get it to send the email? It could just be linked incorrectly or formatted incorrectly.
<form method="post" name="myemailform" action="form-to-email.php">
<div>
<label for="name">Name:</label><br>
<input type="text" id="name" name="user_name" size="45"><br>
</div>
<div>
<label for="email">Email:</label><br>
<input type="text" id="email" name="user_email" size="45"><br>
</div>
<div>
<label for="message">Message:</label><br>
<textarea type="text" id="message" name="user_message" size="600"></textarea><br>
</div>
<div>
<input type = "submit" value = "Send Form">
<input type = "reset" value = "reset">
</div>
</form>
here is my PHP code
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "codymaheu#yahoo.com";
$email_subject = "Testing";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['user_name']) ||
!isset($_POST['user_email']) ||
!isset($_POST['user_message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['user_name']; // required
$email_from = $_POST['user_email']; // required
$comments = $_POST['user_message']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Message you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Message: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
add name into the button like this then try again
<input type = "submit" value = "Send Form" name="email">

PHP form with checkbox and verification does't work

i"m new here and i don't understand php at all, but i need to do a contact form with checkbox and verification, this is my code ( is a frankenstein code).
I need that my form send an email with the information avoiding spam, and missing information, and when mail arrive has all the details in header like a mail to reply, subject and all.
if you can post an correct code will be wonderful.
thanks in advance
<form name="htmlform" method="post" action="app.php">
<label for="name">*Nombre Completo</label>
<input type="text" name="name" maxlength="150" size="50">
<label for="company">*Empresa</label>
<input type="text" name="company" maxlength="150" size="50">
<label for="telephone">Teléfono</label>
<input type="text" name="telephone" maxlength="150" size="50">
<label for="email">*Email</label>
<input type="text" name="email" maxlength="180" size="50">
<label for="services">*Servicios de Interes</label>
<input type="checkbox" name="services[]" value="apps" />apps<br />
<input type="checkbox" name="services[]" value="Marketing Movil" />Marketing Móvil<br />
<input type="checkbox" name="services[]" value="video juegos" />video Juegos<br />
<label for="comments">*Comentarios</label>
<textarea name="comments" maxlength="1000" cols="40" rows="6"></textarea>
<div class="hide">Leave this empty:<input name="url" /></div>
<center><input class="button large" type="submit" value="Submit Form"></center>
</form>
<?php
// if the url field is empty
if(isset($_POST['url']) && $_POST['url'] == '')
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "my#mail.com";
$email_subject = "Contact from web.com";
function died($error) {
// your error code can go here
echo "There were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['full_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['services']) ||
!isset($_POST['comments']))
{
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$full_name = $_POST['full_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // required
$services = $_POST['services']; // required
$comments = $_POST['comments']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$full_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Full Name: ".clean_string($full_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Services: ".clean_string($services)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
// hacker defense
function clean($var){//request string cleaner
if(get_magic_quotes_gpc()) $var=stripslashes($var); //clean
$var=mysql_real_escape_string($var); //clean
return strip_tags($var, '<b><a>');//returning clean var
}
function hackerDefense(){//thanks to Allen Sanford
// begin hacker defense
foreach ($_POST as &$postvalue){ //checking posts
$postvalue = clean($postvalue);//cleaning the value
}
} // end hacker defense
?>
<!-- include your own success html here -->
<center>
<img src="images/logo.png" />
<br>
<br>
Thank you for contacting us. We will be in touch.<br>
<br>
HOME</center>
<?php
}
?>

html php send form doesn't send email

My form doesn't generate an E-Mail it just redirects me to a blank page.
i have my .php form in a folder named php on my server
thank you for your help.
here is my html code
<form id="form" method="post" action="php/send_form_email.php">
<fieldset>
<label><strong>Name:</strong>
<input type="text" value="">
</label>
<label><strong>Email:</strong>
<input type="text" value="">
</label>
<label><strong>Phone:</strong>
<input type="text" value="">
</label>
<label><strong>Message:</strong>
<textarea></textarea>
</label>
<div class="btns">ClearSend</div>
</fieldset>
</form>
here is the php code i'm using:
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "###.com";
$email_subject = "havok security contact form";
function died($error) {
// your error code can go here
echo "We are very sorry, but there was an error found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['phone']) ||
!isset($_POST['message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['phone']; // not required
$comments = $_POST['message']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$name)) {
$error_message .= 'The Name you entered does not appear to be valid.<br />';
}
if(strlen($message) < 2) {
$error_message .= 'The Message you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($first_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Phone: ".clean_string($telephone)."\n";
$email_message .= "Message: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
The input type under email needs to have a name attribute called "email". Your PHP script is looking to see if $_POST["email"] is set, and it is not. So, this should work:
<form id="form" method="post" action="php/send_form_email.php">
<fieldset>
<label><strong>Name:</strong>
<input type="text" name="name" value="">
</label>
<label><strong>Email:</strong>
<input type="text" name="email" value="">
</label>
<label><strong>Phone:</strong>
<input type="text" name="phone" value="">
</label>
<label><strong>Message:</strong>
<textarea name = "message"></textarea>
</label>
<div class="btns">ClearSend</div>
</fieldset>
</form>

Email form from a website via PHP issues

I am coding a personal website and having an issue with my contact from. If you can help me find what's wrong I would really appreciate it.
The link to the website is www.tiryakicreative.com and the code for the php form is given below:
<div id="form">
<form id="ajax-contact-form" action="contact_form/send_form_email.php…
<fieldset class="info_fieldset">
<div id="note"></div>
<div id="fields">
<label>Name</label>
<input class="textbox" type="text" name="name" value="" />
<label>E-Mail</label><input class="textbox" type="text" name="email" value="" />
<label>Subject</label>
<input class="textbox" type="text" name="subject" value="" />
<label>Message</label>
<textarea class="textbox2" name="message" rows="5" cols="25"></textarea>
<label> </label><input class="button" type="image" src="send2.gif" id="submit" Value="Send Message" />
</div>
</fieldset>
</form>
</div>
</div>
Here is the php code for the given html code:
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "ian_tiryaki#hotmail.com";
$email_subject = "New Email from Website";
function died($error) {
// ERROR CODE GOES HERE
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.";
echo $error."";
echo "Please go back and fix these errors.";
die();
}
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['subject']) ||
!isset($_POST['message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['subject']; // not required
$comments = $_POST['message']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Z…
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The Name you entered does not appear to be valid.';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The subject you entered does not appear to be valid.';
}
if(strlen($comments) < 2) {
$error_message .= 'The message you entered do not appear to be valid.';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:",…
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Subject: ".clean_string($telephone)."\n";
$email_message .= "Message: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include success html here -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
Are you receiving the emails after the user sends the email?
Try adding
error_reporting(E_ALL);
to the top of your script.
You could also try removing the # from the mail command
mail($email_to, $email_subject, $email_message, $headers);
As this will be suppressing any errors that is being generated.
It could be something as simple as the PHP mail function having additional headers disabled (some hosts do this for security reasons) in which case the mail function will fail.
set this is in your action form ...
and you will definately get mail from here..
and although there is an error you may be use use below code for send mail using php without declare a variable...
like
$email_to=$_POST['email'];
$email_subject=$_POST['subject'];
$email_message=$_POST['message'];
$headers=$_POST['title'];
mail('$email_to', '$email_subject', '$email_message', '$headers');
otherwise
use below code for send mail
mail('$_POST['email']','$_POST['subject']','$_POST['message']','$_POST['title']');

Categories