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);
Related
Error:
the code:
<?php
$name = $_POST['name'];
$mail = $_POST['mail'];
$message = $_POST['message'];
$to ="bejidev27#gmail.com";
$subject = "New contact from " .$name;
$body = "";
$body .= "Name : " .$name. "\r\n";
$body .= "Email : " .$mail. "\r\n";
$body .= $message. "\r\n";
if($mail !=NULL){
mail($to, $subject, $body);
header("Location: index.html") ;
header("Location: done.html") ;
}
?>
It's not sending the mail nor the page is working i need help ,thanks in advance :D
First, check if your servers are running properly. Then, for the following
$name = $_POST['name'];
$mail = $_POST['mail'];
$message = $_POST['message'];
There need to be values for each you. If you are getting the values from a form, then you have to handle the form first using the 'isset' function, to be able to pass the values to each variables.
I'm just starting to learn php, so I suspect this is going to be an easy one. I want to create a form that has one required field (name) and requires at least one of two other fields (email or phone number). This is what I came up with:
<?php
if (isset($_POST["submit"])) {
if (empty($_POST["name"]) or {empty($_POST["email"]) and empty($_POST["number"])}) {
$error = "Your name and a contact method are required fields";}
else {
$name = $_POST["name"];
$email = $_POST["email"];
$number = $_POST["number"];
$contactmethod = $_POST["contactmethod"];
$subject = $_POST["subject"];
$location = $_POST["location"];
$message = $_POST["message"];
$subjectline = "You have a message from $name!";
$header = "From: $email";
$body = <<<EMAIL
Name: $name,
Number: $number,
Email: $email,
Subject: $subject,
Location: $location,
Contact method: $contactmethod.
$message
EMAIL;
mail("MYEMAILADDRESS", $subjectline, $body, $header);
}
}
?>
I have no clue what's wrong.
Thanks
Your problem: You used { and } in a if-statement.
Next time you can use a PHP code checker like http://phpcodechecker.com/
Correct code:
if (empty($_POST["name"]) or (empty($_POST["email"]) and empty($_POST["number"]))) {
$error = "Your name and a contact method are required fields";}
else {
$name = $_POST["name"];
$email = $_POST["email"];
$number = $_POST["number"];
$contactmethod = $_POST["contactmethod"];
$subject = $_POST["subject"];
$location = $_POST["location"];
$message = $_POST["message"];
$subjectline = "You have a message from $name!";
$header = "From: $email";
$body = <<<EMAIL
Name: $name,
Number: $number,
Email: $email,
Subject: $subject,
Location: $location,
Contact method: $contactmethod.
$message
EMAIL;
mail("MYEMAILADDRESS", $subjectline, $body, $header);
}
}
?>
You can't use braces interchangeably with parentheses.
if (empty($_POST["name"]) or {empty($_POST["email"]) and empty($_POST["number"])}) {
it needs to use parentheses
if (empty($_POST["name"]) or (empty($_POST["email"]) and empty($_POST["number"]))) {
How to add the Turkish characters to this form:
in the $mailed = mail($to, '=?utf-8?B?'.base64_encode($message).'?=', $message, $headers);
I already added '=?utf-8?B?'.base64_encode which only fixed the subject. but all the body still appear something like this: ĞÜLŞÖÇI ğüşiöçı if typed ĞÜLŞÖÇI ğüşiöçı for example.
<?php
if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['message']) && isset($_POST['subject'])) {
$companyname = $_POST['company-name'];
$name = $_POST['name'];
$email = $_POST['email'];
$areacode = $_POST['areacode'];
$phone = $_POST['phone'];
$city = $_POST['city'];
$state = $_POST['state'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = "";
$subject = "$subject";
$message = "Name: $name\nPhone Number: $areacode $phone\nCity: $city\nState: $state\n\n$message";
$headers = "From: $email";
$mailed = mail($to, '=?utf-8?B?'.base64_encode($message).'?=', $message, $headers);
if (isset($_POST['ajax'])) {
$response = ($mailed) ? "1" : "0";
} else {
$response = ($mailed) ? "<h2>Success!</h2>" : "<h2>Error! There was a problem with sending.</h2>";
}
echo $response;
} else {
echo "Form data error!";
}
You should also encode your message (UTF8).
You can do it using the headers parameter of the mail function.
additional_headers
String to be inserted at the end of the email header. This is
typically used to add extra headers (From, Cc, and Bcc). Multiple
extra headers should be separated with a CRLF (\r\n). Validate
parameter not to be injected unwanted headers by attackers.
Replace the next code:
$headers = "From: $email";
With:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'From: '.$email . "\r\n";
Should work.
Update me if not.
I have an ajax post coming to my PHP file for sending an email.
The issue is, my html contact form is always changing, one user might use 10 text-inputs, another user might use 5, etc.
The data is coming to the PHP file like this :
txt1=John&txt2=aol#aol.com&txt3=5550123944&dropDown1=Sales&question=How+are+you
The PHP file is like this :
<?php
$emailSubject = 'Customer Has a Question!';
$webMaster = 'giberisk#yahoo.com';
$name = $_POST ['name'];
$email = $_POST['email'];
$phone = $_POST ['phone'];
$question = $_POST ['question'];
$body = <<<EOD
<br><hr><br>
Name: $name <br>
Email: $email <br>
Phone: $phone <br>
Question: $question <br>
EOD;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);
$theResults = <<<EOD
EOD;
echo "$theResults";
?>
What do I have to do, so that the PHP file finds out how many arguments were passed, and to name the variables after the name of those arguments ?
I hope I was clear enough,
Thanks in advance
you could do foreach, like
$count = count($_POST); //shows number of variables POST'ed
foreach($_POST as $key => $val ) {
echo $key . "==>" .$val;
//or assign values to key variables
${$key} = $value; //${$key} will be variable holding $value as value
}
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;