Trying to display two concatenated variables in email subject line using PHP - php

I am trying to display two concatenated variables in a subject line through an mailer.php page, but the subject line in the email always comes in blank. Below is the pertinent code.
/* Subject and To */
$to = 'nnorman#dimplexthermal.com';
$subject = $company . ' ' . $name;
/* Gathering Data Variables */
$name = $_POST['name'];
$email = $_POST['email'];
$company = $_POST['company'];
$body = <<<EOD
<br><hr><br>
Email: $email <br>
Name: $name <br>
Company: $company <br>
EOD;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($to, $subject, $body, $headers);

$to = 'nnorman#dimplexthermal.com';
$subject = $company . ' ' . $name;
/* Gathering Data Variables */
$name = $_POST['name'];
$email = $_POST['email'];
$company = $_POST['company'];
You're not setting $company and $name until after you use them in $subject
Try switching the lines round:
/* Gathering Data Variables */
$name = $_POST['name'];
$email = $_POST['email'];
$company = $_POST['company'];
$to = 'nnorman#dimplexthermal.com';
$subject = $company . ' ' . $name;

Related

recaptcha not sending email after verify in php

<?php
if(isset($_POST['submit']))
{
$email=$_POST['email'];
$comment=$_POST['comment'];
$captcha=$_POST['g-recaptcha-response'];
if(!$captcha)
{
echo 'Please check the the captcha form.';
}
$response=file_get_contents("https://www.google.com/recaptcha/api/sitev erify?secret="secretkey" &response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
if($response.success==false)
{
header('Location:http://mywebsite.com/.errordocs/404.html');
}
else
{
header('Location:http://mywebsite.com/thankyou.php');
}
}
if (isset ($_POST['Side_Form'])){
$name = $_POST['name'];
$last = $_POST['last'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$comments = $_POST['comments'];
// Build the email (replace the address in the $to section with Enjoy's contact e-mail)
$to = 'somebody#somewhere.com';
$subject = "Main Contact Form";
$message = "Name: $name
Email: $email
Phone: $phone
Comments: $comments";
$headers = "From: $email \r\n";
mail($to, $subject, $message, $headers);
}
?>
The problem is when the recaptcha is verified on the form, it displays the checkmark, user can click submit, but I get a blank screen and email doesn't send. Any help is Greatly appreciated, have been working on this for about 3 weeks.
your currently redirecting before actually sending the email. here is your code restructured. also fixed the recaptcha response processing
<?php
if (isset($_POST['submit'])){
$email = $_POST['email'];
$comment = $_POST['comment'];
$captcha = $_POST['g-recaptcha-response'];
if (! $captcha){
echo 'Please check the the captcha form.';
exit();
}else{
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=" . $secretkey . "&response=" . $captcha . "&remoteip=" . $_SERVER['REMOTE_ADDR']);
$responseKeys = json_decode($response,true);
if (intval($responseKeys["success"]) !== 1){
header('Location:http://mywebsite.com/.errordocs/404.html');
exit();
}else{
if (isset($_POST['Side_Form'])){//not sure what this is, hopefully you do :-)
$name = $_POST['name'];
$last = $_POST['last'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$comments = $_POST['comments'];
// Build the email (replace the address in the $to section with Enjoy's contact e-mail)
$to = 'somebody#somewhere.com';
$subject = "Main Contact Form";
$message = "Name: $name
Email: $email
Phone: $phone
Comments: $comments";
$headers = "From: $email \r\n";
mail($to,$subject,$message,$headers);
header('Location:http://mywebsite.com/thankyou.php');
exit();
}
}
}
}
?>

Reply-to email in PHP error

When a user inputs their email on a form on my site, I get an email with their info. I'd like to be able to reply to that email and have their email autofill in "To:" but I'm having trouble. I found this question and tried the solution: reply-to address in php contact form but it's not working for me, and I'm not sure why.
Here is my PHP:
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
$to = 'amanda#myemail.com';
$headers = "BCC: clients#myemail.com\r\n";
$headers .= 'Reply-To: ' . $email . "\r\n";
$subject = '*** Quote Request';
$name = $_POST['name'];
$phone = $_POST['phone'];
$date = $_POST['date'];
$time = $_POST['time'];
$pickup = $_POST['pickup'];
$dropoff = $_POST['dropoff'];
$passengers = $_POST['passengers'];
$service = $_POST['service'];
$email = $_POST['email'];
$message = <<<EMAIL
Quote submission from: $name
Name: $name
Phone Number: $phone
Date: $date
Time: $time
Pickup Location: $pickup
Drop Off Location: $dropoff
Total Passengers: $passengers
Service needed: $service
Email: $email
EMAIL;
if($_POST) {
mail($to, $subject, $message, $headers);
}
header('Location: thankyou.html');
exit();
?>
And this is the error message I'm getting, summed up:
Undefined variable: email in /contact-form-handler.php on line 9 Warning: Cannot modify header information - headers already sent by (output started at /contact-form-handler.php:9) in /contact-form-handler.php on line 44
The problem is the variable $email because if I put a Reply-To and specify an email, it works. I thought maybe it was because the variable is defined after I call it in the header, but adding it to the bottom didn't work. I'm a rookie with PHP so I'm not sure why this variable isn't working.
I also tried:
$headers = "BCC: clients#myemail.com\r\n" .
'Reply-To: ' . $email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
Any help would be appreciated!
You use a variable before you define it!
Move this block to the top of your script:
$name = $_POST['name'];
$phone = $_POST['phone'];
$date = $_POST['date'];
$time = $_POST['time'];
$pickup = $_POST['pickup'];
$dropoff = $_POST['dropoff'];
$passengers = $_POST['passengers'];
$service = $_POST['service'];
$email = $_POST['email'];
Like that:
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
$name = $_POST['name'];
$phone = $_POST['phone'];
$date = $_POST['date'];
$time = $_POST['time'];
$pickup = $_POST['pickup'];
$dropoff = $_POST['dropoff'];
$passengers = $_POST['passengers'];
$service = $_POST['service'];
$email = $_POST['email'];
$to = 'amanda#myemail.com';
$headers = "BCC: clients#myemail.com\r\n";
$headers .= 'Reply-To: ' . $email . "\r\n";
$subject = '*** Quote Request';
$message = <<<EMAIL
Quote submission from: $name
Name: $name
Phone Number: $phone
Date: $date
Time: $time
Pickup Location: $pickup
Drop Off Location: $dropoff
Total Passengers: $passengers
Service needed: $service
Email: $email
EMAIL;
if($_POST) {
mail($to, $subject, $message, $headers);
}
header('Location: thankyou.html');
exit();
?>
seems like you don't receive $_POST['email']; from your contact form (re: "Undefined variable: email ")
check the name of your mail input field...

JQuery Ajax PHP form submit

I'm using Ajax contact form where my contactform.php is something like this :
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = "Contact Form";
$message .= $_POST['name'] . "\n\r";
$message .= $_POST['email'] . "\n\r";
$message .= $_POST['message'];
mail("your#domain.com", $subject, $message, "From: ".$email);
?>
This form script works perfect But My problem is that, My value for "your#domain.com" is dynamic.. So I modified contactform.php to this :
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$emailto = $qc2_options['qc2_form_heading'];
$subject = "Contact Form";
$message .= $_POST['name'] . "\n\r";
$message .= $_POST['email'] . "\n\r";
$message .= $_POST['message'];
mail($emailto, $subject, $message, "From: ".$email);
?>
But this modified script isn't working.. I do get correct value for $qc2_options['qc2_form_heading']; on echo But this form dosn't work.
Where I'm going wrong ?

PHP - Mail Form Content - how to add CC field?

I have used this PHP mail script for a while now and it served me well... I need to ask a few things.
Right now, it just sends an email to me with the information posted.
then it just Echo's back to the user if it was submitted successfully or not.
How can I add a CC field to send the user some direction on what to do next?
thanks
if (isset($_POST['submit'])) {
if (!$_POST['name'] | !$_POST['email'])
{
echo"<div class='error'>Error<br />You did not fill in a required field, please review your form and correct the missing information.</div>";
}
else
{
$name = $_POST['name'];
$email = $_POST['email'];
$email2 = $_POST['email2'];
$legal = $_POST['legal'];
$legal2 = $_POST['legal2'];
$address = $_POST['address'];
$address2 = $_POST['address2'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];
$comments = $_POST['comments'];
$yoursite = "See Me Shine Models";
$youremail = $email;
$subject = "Website Model Form";
$message = "$name would like you to contact them about See Me Shine Models.
Contact PH: $phone
Email: $email
Email2: $email2
Legal: $legal
Legal2: $legal2
Address: $address
Address2: $address2
City: $city
State: $state
Zip: $zip
Phone: $phone
Comments: $comments";
$email3 = "myaddress#me.com";
mail($email3, $subject, $message, "From: $email");
echo"<div class='thankyou'>Thank you for contacting us,<br /> we will respond as soon as we can.</div>";
}
}
You need to specify fourth argument of headers:
$xheaders = "";
$xheaders .= "From: Name-Here. <$email>\n";
$xheaders .= "X-Sender: <$email>\n";
$xheaders .= "X-Mailer: PHP\n"; // mailer
$xheaders .= "X-Priority: 1\n"; //1 Urgent Message, 3 Normal
$xheaders .= "Content-Type:text/html; charset=\"iso-8859-1\"\n";
$xheaders .= "Cc: cc_email#example.com\n";
.................
mail($email3, $subject, $message, $xheaders);
Or see this tutorial:
Send email with CC(Carbon Copy) & BCC(Blind Carbon Copy)

How to add a simple IF statement in this PHP file?

Question by a novice, here:
I have a simple form returning a person's name, company, and address among other things.
Right now, these are returned on separate lines. If there is no entry in the "company" field, there is a blank line in the email that is sent.
I'd like to make an IF statement so that if there is no entry in the "company" field, that "address" will come back right after "name" without an extra space, but will include the "company" info if that field is filled-in.
The relevant part of the PHP code looks like this:
$name = $_POST['name'];
$company = $_POST['company'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$email = $_POST['email'];
$optin = $_POST['optin'];
$comments = ($_POST['comments']);
$body = <<<EOD
Please send samples to:
$name
$company
$address
$city, $state $zip
Email: $email
Opt-In to occasional email list?: $optin
Comments: $comments
EOD;
$headers = "From: $email\r\n";
$success = mail($webMaster, $emailSubject, $body,
$headers);
I will really appreciate your help!
You'll have to break out of HEREDOC for this, or prepare the value beforehand, but this is nice and short:
echo join("\n", array_filter(array($name, $company, $address)));
What about this?
$name = $_POST['name'];
//...
if(empty($company)){
$nameAndCompany = $name;
}else{
$nameAndCompany = $name."\n".$company;
}
// or using the ternary operator, because it's a rather simple if
$nameAndCompany = (empty($company))?$name:$name."\n".$company;
$body = <<<EOD
...
$nameAndCompany
$address
$city, $state $zip
...
\n in double quotes means newline. See the php.net documentation for more info on the ternary operator.
You are using heredoc syntax in your PHP script. You cannot use IF statements in there. But you can use other methods :
close your PHP tag, and use <?= $var ?> in your code
use templates and replace keywords with preg_replace(), etc.
put all your strings into an array and use implode("\n", $arr) to construct a nice string out of it
concatenate your string directly
The easiest way is the first one (EDITED with array_filter from another answer):
$name = $_POST['name'];
$company = $_POST['company'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$email = $_POST['email'];
$optin = $_POST['optin'];
$comments = ($_POST['comments']);
ob_start();
?>
Please send samples to:
<?= implode("\n", array_filter($name, $company, $address)) ?>
<?= $city ?>, <?= $state ?> <?= $zip ?>
Email: <?= $email ?>
Opt-In to occasional email list?: <?= $optin ?>
Comments: <?= $comments ?>
?>
$body = ob_get_clean(); // return the output since ob_start()
$headers = "From: $email\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);
I edited this to use \n instead of <br />
$optin = $_POST['optin'];
$comments = ($_POST['comments']);
$body = "Please send samples to:";
if($_POST['name'] != '') $body .= "\n".$_POST['name'];
if($_POST['company'] != '') $body .= "\n".$_POST['company'];
if($_POST['address'] != '') $body .= "\n".$_POST['address'];
if($_POST['city'] != '') $body .= "\n".$_POST['city'];
if($_POST['state'] != '') $body .= ", ".$_POST['state'];
if($_POST['zip'] != '') $body .= " ".$_POST['zip'];
if($_POST['email'] != '') $body .= "\n\nEmail: ".$_POST['email'];
$body .= "Opt-In to occasional email list?: $optin";
$body .= "\n\nComments: $comments";
$headers = "From: $email\r\n";
$success = mail($webMaster, $emailSubject, $body,
$headers);

Categories