I have a script that sends an email with information stored in sessions - the info is collected from a form a user fills out. (the form 'action' is pointed to the code below)
However, when the submit button is clicked twice on the form for example, 2 emails are sent and when clicked 3 times, 3 emails are sent.
I want to make sure that only 1 email is sent and if any session is empty no email is to be sent:
<?php
session_start();
if(isset($_POST['email'])) {
$email_to = "recipient#emailaddress.com";
$email_subject = "My Subject";
$machine = implode(",", $_SESSION['machinesesh']); //required
$machine_type = implode(" ", $_SESSION['typesesh']);; // required
$address = $_SESSION['addresssesh']; //required
$county = $_SESSION['countysesh']; //required
$postcode = $_SESSION['postcodesesh']; //required
$workplace = implode(', ', $_SESSION['worksesh']); //required
$serving = implode(', ', $_SESSION['peoplesesh']);
$company_name = $_SESSION['namesesh']; // required
$visitorname = $_POST['yourname']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$email_message = "New email alert .\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($visitorname)."\n";
$email_message .= "Company: ".clean_string($company_name)."\n";
$email_message .= "Address Line 1: ".clean_string($address)."\n";
$email_message .= "County: ".clean_string($county)."\n";
$email_message .= "Postcode: ".clean_string($postcode)."\n";
$email_message .= "Machine(s) Wanted: ".clean_string($machine)."\n";
$email_message .= "Environment: ".clean_string($workplace)."\n";
$email_message .= "Serving: ".clean_string($serving)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
// create email headers
$headers = 'From: sendaddress#email.com' . "\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
header("Location: http://www.example.com?tag=$machine");
?>
How about settings an $_SESSION["emailsent"] = 1; ? Before you send the email, check if the variable exists AND if it is set to 1! In this case you know, that in this session the email is only sent once!
You can try to erase the session after sending the email. The next time the form is submitted, it should check if session value is set or not. If not set, then it should not send email again.
Related
I have a php form that sends an email, however I would like to put the headings of each section in bold but can't seem to get it to work from everywhere I've searched.
The form:
$email_message .= "Name: ".clean_string($firstlastname)."\n\n";
$email_message .= "Email: ".clean_string($email_from)."\n\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n\n";
$email_message .= "Property Type: ".clean_string($ptype)."\n\n";
$email_message .= "From Address: ".clean_string($faddress)."\n\n";
$email_message .= "To Address: ".clean_string($taddress)."\n";
$email_message .= "Moving Items Description: ".clean_string($itemsdescription)."\n\n";
$email_message .= "Dissasembling Requirements: ".clean_string($disassemble)."\n\n";
$email_message .= "Packaging Requirements: ".clean_string($packaging)."\n\n";
$email_message .= "Preferred Date and Time: ".clean_string($dateandtime)."\n\n";
$email_message .= "Property Access: ".clean_string($paccess)."\n\n";
$email_message .= "Stair Count: ".clean_string($staircount)."\n\n";
$email_message .= "Message: ".clean_string($message)."\n\n";
// create email headers
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
I've tried adding the bold and strong tags to the headings;
$email_message .= "<b>Name:</b> ".clean_string($firstlastname)."\n\n";
Still not working, including setting it outside of the heading of the text.
Edit: this is the full form
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "email#email.com";
$email_subject = "Contact 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 />";
die();
}
// validation expected data exists
if(!isset($_POST['firstlastname']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['ptype']) ||
!isset($_POST['faddress']) ||
!isset($_POST['taddress']) ||
!isset($_POST['itemsdescription']) ||
!isset($_POST['disassemble']) ||
!isset($_POST['packaging']) ||
!isset($_POST['dateandtime']) ||
!isset($_POST['paccess']) ||
!isset($_POST['staircount']) ||
!isset($_POST['message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$firstlastname = $_POST['firstlastname'];
$email_from = $_POST['email'];
$telephone = $_POST['telephone'];
$ptype = $_POST['ptype'];
$faddress = $_POST['faddress'];
$taddress = $_POST['taddress'];
$itemsdescription = $_POST['itemsdescription'];
$disassemble = $_POST['disassemble'];
$packaging = $_POST['packaging'];
$dateandtime = $_POST['dateandtime'];
$paccess = $_POST['paccess'];
$staircount = $_POST['staircount'];
$message = $_POST['message'];
$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,$firstlastname)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(strlen($message) < 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 .= "Name: ".clean_string($firstlastname)."\n\n";
$email_message .= "Email: ".clean_string($email_from)."\n\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n\n";
$email_message .= "Property Type: ".clean_string($ptype)."\n\n";
$email_message .= "From Address: ".clean_string($faddress)."\n\n";
$email_message .= "To Address: ".clean_string($taddress)."\n";
$email_message .= "Moving Items Description: ".clean_string($itemsdescription)."\n\n";
$email_message .= "Dissasembling Requirements: ".clean_string($disassemble)."\n\n";
$email_message .= "Packaging Requirements: ".clean_string($packaging)."\n\n";
$email_message .= "Preferred Date and Time: ".clean_string($dateandtime)."\n\n";
$email_message .= "Property Access: ".clean_string($paccess)."\n\n";
$email_message .= "Stair Count: ".clean_string($staircount)."\n\n";
$email_message .= "Message: ".clean_string($message)."\n\n";
// create email headers
$headers = "Content-Type: text/html; charset=ISO-8859-1\r\n";
$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 -->
<script>window.location.replace("index.html");</script>
<?php
}
?>
You missed a dot to append more headers. This overwrites your Content-Type and that is why it is parsing the email as text and not has HTML
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
Could you try :
$email_message .= "<strong>Name:</strong> ".clean_string($firstlastname)."\n\n";
or
$email_message .= "<b>Name:</b> ".clean_string($firstlastname)."\n\n";
or
$email_message .= "<p style="font-weight: bold;">Name:</b> ".clean_string($firstlastname)."\n\n";
Use PHP Mailer plugin.
Download Plugin in following link:
https://github.com/PHPMailer/PHPMailer/releases/tag/v5.2.26
$mail_message='Dear '.$row[0]['branch_name'].','. "\r\n";
$mail_message.='Thanks for contacting regarding to forgot password,<br> Your <b>Password</b> is <b>'.$passwordplain.'</b>'."\r\n";
$mail_message.='<br>Please Update your password.';
$mail_message.='<br>Thanks & Regards';
$mail_message.='<br>Test group of company';
date_default_timezone_set('Etc/UTC');
require FCPATH.'application/libraries/PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
// $mail->isSMTP();
$mail->SMTPSecure = "ssl";
$mail->Debugoutput = 'html';
$mail->Host = "(HOST SERVER)";
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = "(USERNAME)";
$mail->Password = "(PASSWORD)";
$mail->setFrom('(FROM MAIL ADDRESS)', 'Test group of company');
$mail->IsHTML(true);
$mail->addAddress($email);
$mail->Subject = 'New password for login';
$mail->Body = $mail_message;
if (!$mail->send()) {
$data['message_display'] = 'Failed to send password, please try again!';
$this->load->view('branch/forgotPassword', $data);
} else {
$data['message_display'] = 'Password sent to your email!';
$this->load->view('branch/forgotPassword', $data);
}
I'm trying to put together a simple feedback form which uses PHP to email the results to us. The script works once, we get the email as intended.. but every time afterwards, there's no email and no error. Anyone have any idea why?
<?php
$email_to = "admin#urbansushi.com";
$name = $_POST['name']; // required
$email = $_POST['email']; // required
$date = $_POST['date']; // required
$email_subject = "New feedback from CUSTOMER";
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Date of Visit: ".clean_string($date)."\n";
$email_message .= "Email: ".clean_string($email)."\n";
// create email headers
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
You are concatenating to $email_message but are not declaring it initially:
$email_message .= "Name: ".clean_string($name)."\n";
Try adding this
$email_message = "";
$email_message .= "Name: ".clean_string($name)."\n";
also you have whitespace before your php declaration - try removing it so that the php declaration is at the start of the file
<?php
I am using a simple php contact form that sends to an email, but when I want it to send Japanese characters, the email I receive from the form displays those characters in strange Greek-looking code. I assume there is some way to specify in the php form that I am using UTF-8, but I am not sure where or what to put to let the characters display as Japanese. Below is the code.
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "abc#abc.com";
$email_subject = "Contact Form";
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required
$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($first_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\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);
?>
<!-- include your own success html here -->
All of a sudden contact forms filled in on my website are coming into my inbox from the date 1/1/1970???
They are ending up at the bottom of my inbox and I have missed a few leads...
Any ideas how this can start happening all of a sudden?
The code I am using on my contact page is : -
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "my email address";
$email_subject = "Website Contact Enquiry";
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['name']) ||
!isset($_POST['email']) ||
!isset($_POST['tel']) ||
!isset($_POST['message'])||
!isset($_POST['formtype'])
) {
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
$tel = $_POST['tel']; // required
$message = $_POST['message']; // required
$formtype = $_POST['formtype'];
$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($name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Tel: ".clean_string($tel)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
$email_message .= "formtype: ".clean_string($formtype)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion().date();
#mail($email_to, $email_subject, $email_message, $headers);
?>
Add this to your headers:
'Date: ' . date('r'),
Also, make sure to sanitize $email_from. Right now, you are allowing spammers to send E-Mail to other recipients and change the header. Read more here: http://www.securephpwiki.com/index.php/Email_Injection
i have a script that i have modified to meet my requirements however i now need to send the email to more than one person, could someone point me in the right direction as to how i could modify the script to send to more than one person.
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "emailremoved#sample.com";
$email_subject = "Kro Catering Website Enquiry";
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['your_name']) ||
!isset($_POST['type']) ||
!isset($_POST['guests']) ||
!isset($_POST['date']) ||
!isset($_POST['phone']) ||
!isset($_POST['email'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$your_name = $_POST['your_name']; // required
$type = $_POST['type']; // required
$guests = $_POST['guests']; // required
$date = $_POST['date']; // not required
$phone = $_POST['phone']; // required
$email_from = $_POST['email']; // required
$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 .= "Your Name: ".clean_string($your_name)."\n";
$email_message .= "Type: ".clean_string($type)."\n";
$email_message .= "Guests: ".clean_string($guests)."\n";
$email_message .= "Date: ".clean_string($date)."\n";
$email_message .= "Phone: ".clean_string($phone)."\n";
$email_message .= "Email: ".clean_string($email_from)."\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 -->
<?php
header( 'Location: /thanks.aspx' ) ;
?>
<?php
}
?>
Search for the line:
$email_to = "emailremoved#sample.com";
And keep adding e-mails with a comma separating them:
$email_to = "emailremoved#sample.com,emailremoved#sample.com,emailremoved#sample.com";
PHP's mail() function is quite versatile when it comes to the "to" field. See the documentation here. Any one of the listed examples would be fine:
user#example.com
user#example.com, anotheruser#example.com
User <user#example.com>
User <user#example.com>, Another User <anotheruser#example.com>
So since your $email_to variable is not cleaned or otherwise modified after you set it on line 5, you should be able to just put 2 there separated by a comma (as in the examples above that I copied from the documentation I linked to.)
Try this!
It was the only code that worked for me.
$header .= 'Bcc: someaddress#email.com';