Why does this only work once? - php

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

Related

PHP Email form bold for headings on email message items

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);
}

PHP Post email Sending email multiple times

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.

PHP Mail Form Language Issues

Hello guyz and thank you in advance. I have made an email form using php to send an email through a form from my website
Although the form works fine and the emails are sent I have a problem when I sent a message in my native language (Greek). If I write the message in English everything is fine and the email is view-able. If I send a message in Greek then the email is sent , but looks like this :
³Î¾ÏƒÎ»Î´Î³Î·ÎºÎ»ÏƒÎ´ ασλαηκσλκαςξδςακσ³Î¾ÏƒÎ»Î´Î³Î·ÎºÎ»ÏƒÎ´ ασλαηκσλκαςξδςακσ
Here is the code I use in my php form :
<?php
if(isset($_POST['email'])) {
$email_to = "info#something.gr";
$email_subject = "Email Form - WebSite";
function died($error) {
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();
}
$first_name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$comments = $_POST['message']; // 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 .= "Message: ".clean_string($comments)."\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 -->
<center>
Thank you for contacting us. We will be in touch with you very soon.</br>
You will be redirected to our homepage in 5 seconds </center>
<script type="text/javascript">
setTimeout("window.location='/'",5000);
</script>
<?php
}
?>
The problem is with character encoding.
By default, PHP E-mails are sent with ISO-8859-1.
You'll need to swap to UTF-8 for languages like Greek :)
Fortunately, you can set this in the headers:
$headers = "Content-Type: text/html; charset=UTF-8";
Combined with your existing headers:
$headers = 'From: ' . $email_from . "\r\n" .
'Reply-To: ' . $email_from . "\r\n" .
'Content-Type: text/html; charset=UTF-8' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
Hope this helps!

Simple PHP form doesn't seem to be working

I was trying to put a simple contact form on my site. I used a template I made on a earlier project, but for some reason it doesn't seem to be working?
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$msg = $_POST['msg'];
$name = strip_tags($name);
$email = strip_tags($email);
$msg = strip_tags($msg);
$email_to = "my.email#email.com";
$email_subject = "Uusi yhteydenotto";
$email_message = "Uusi yhteydenotto: ";
$email_message = "\nName:" .$name.;
$email_message = "\nE-mail:" .$email.;
$email_message = "\n\nMessage:" .$msg.;
$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 think this contains all the necessary things for it to work, but it doesn't seem to work?
Keeps on giving me the error 500. The server should be PHP5 -approved so that shouldn't be the problem
should use concatenation operator ., your code should now look like below
$email_message = "Uusi yhteydenotto: ";
$email_message .= "\nName:" . $name;
$email_message .= "\nE-mail:" . $email;
$email_message .= "\n\nMessage:" . $msg;
Try
$name = $_POST['name'];
$email = $_POST['email'];
$msg = $_POST['msg'];
$name = strip_tags($name);
$email = strip_tags($email);
$msg = strip_tags($msg);
$email_to = "my.email#email.com";
$email_subject = "Uusi yhteydenotto";
$email_message = "Uusi yhteydenotto: ";
$email_message .= "\nName:" .$name; /* removed .; */
$email_message .= "\nE-mail:" .$email; /* removed .; */
$email_message .= "\n\nMessage:" .$msg; /* removed .; */
/* change `$email_from` in $headers = 'From: '.$email_from."\r\n". */
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);

PHP Contact form in website not sending Japanese characters

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 -->

Categories