send-mail.php only send field if the field exists - php

I have a form being posted to send-mail.php The form field is generated dynamically, sometimes it will exist, sometimes it will not exist. I want the email received to only contain the form field if it exists, if the form field does not exist there should be nothing in the email about. The code below always indicates "null" whether the field exists or not. Any ideas?
$myBlueTextArea = isset($_POST['sender_BlueText']);
if($myBlueTextArea) print 'not null';
if(!$myBlueTextArea) print 'null';

How I usually do this is with a generic processor for forms. Basically, name the form fields what you would like them to display as in the email and then create an email based on that. Usually I separate words with dashes so I use something like the following:
$body = 'You got a form submission! <br />';
foreach($_POST as $name => $value)
{
$name = preg_replace('/[^a-zA-z0-9]/', '', $name);
$body .= "<strong> $name </strong>: $value <br />";
}
This way all posted fields are stored in the $body variable in a somewhat formatted way, change the markup to suit your needs.

The code below is how I solved the problem. You can test it by filling out the form as-is and sending/emailing it to yourself, then comment out the Message textarea field and re-submit the form. When that field does not exist, the email that the form sends will omit that field instead of sending the message field as null or empty, etc.
<?php
if(isset($_POST['submit'])){
$to = "johndoe#xmail.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
if(isset($_POST['message'])){
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
}
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
}
?>
<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>
<form action="" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

Related

html form in php not sending to email from server

i have a form in my php page like the following:
<form action="" method="post">
<h1>Sign Up Now!
<!-- <span>Sign up and tell us what you think of the site!</span> --></h1>
<div class="inner-wrap">
<label>Your Full Name <input type="text" name="field1" /></label>
<label>Address <textarea name="field2"></textarea></label>
</div>
<div class="button-section">
<input type="submit" value="Submit" name="Sign Up" />
</div>
</form>
the code for it is below:
<?php
if(isset($_POST['submit'])){
$to = "zubairking#gmail.com"; // this is your Email address
$from = $_POST['field1']; // this is the sender's Email address
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
// You can also use header('Location: thank_you.php'); to redirect to another page.
}
?>
my server is also mail enabled, but this code of mine is not sending values to my mail, all the code is in one page, can anyone please tell me whats wrong with my code? thanks in advance
You need the submit button to have name="submit" for the condition if(isset($_POST['submit'])) to be met. You also need to add elements with names='first_name', 'last_name' and 'message' in the form to apply them in your email messages as you are trying to do.
You also need your <input type="text" name="field1" /> to be an email instead a name, given that you are using it to set the sender of the email.

Adding more boxes to fill

<?php
if(isset($_POST['submit'])) {
$to = "receiver#domain.com"; // this is IT Support Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name']; // sender's first name
$last_name = $_POST['last_name']; // sender's last name
$subject = "Form submission"; // Email confirmation submission
from sender
$subject2 = "Copy of your form submission"; // Email confirmation submission
from receiver
$message = $first_name . " " . $last_name . " requested:" . "\n\n" . $_POST['message']; // Email header
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to, $subject, $message, $headers);
mail($from, $subject2, $message2, $headers2); // sends a copy of the message to the sender
echo "Request Submitted Succesfully. Thank you " . $first_name . ", we will
fulfill your request shortly.";
// You can also use header('Location: thank_you.php'); to redirect to
another page.
}
?>
<!DOCTYPE html>
<head>
<title>Device and Set Up Request Form</title>
</head>
<body>
<form action="" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
How do I add more labels below the message for the users to fill? For example, right after "Message:," I want to add "Supervisor name" and more labels. In addition, those labels not only appear on the website, but also is wrapped and get emailed to the receiver.

Nothing happens when i click on submit button in my contact form

When I click on the submit button, nothing happens, even though I do have PHP set up. I thought at first that my code is obviously wrong. But now I even copy/pasted the code from here, that is checked as correct, yet nothing happens.
Here is that code:
<?php
if(isset($_POST['submit'])){
$to = "zezesurla#gmail.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
// You can also use header('Location: thank_you.php'); to redirect to another page.
}
?>
<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>
<form action="" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
</div>
Could this be a problem that is in the server and not the actual code? Because I tried several other examples that I found on various tutorials and the same issue occurs.
Or am I still doing something wrong?
As a minimum, I would make the following changes to your code:
<?php
$msg='';
if (isset($_POST['submit']))
{
...
if (!mail($to,$subject,$message,$headers))
error_log(($msg='Send form submission to "'.$to.'" failed'));
else if (!mail($from,$subject2,$message2,$headers2)) // sends a copy of the message to the sender
error_log(($msg='Send copy of form submission to user "'.$from.'" failed'));
else $msg='Mail Sent. Thank you ' . $first_name . ', we will contact you shortly.';
...
<body>
<?php if ($msg)
{ ?>
<p><?php echo $msg ?></p>
<?php
} ?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
These changes will not address the problem of the form being vulnerable to sending SPAM, but it will address these issues:
if a mail() call error occurs, it will be logged, and a message will be displayed (which will also confirm the email address involved)
single-quoted strings don't have to be parsed by the PHP interpreter and should be used unless you are embedding variables or character sequences (such as "\n") that need to be interpreted
you won't be generating bad HTML by having text written to the browser before the <head> section of the document
you will be assured the correct action is being used to handle the form

how to send a form to an email [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
hey guys i'm trying to send a form to an email and that's my form :
<?php
error_reporting(0);
if(isset($_POST['submit'])){
$to = "elbiheiry2#gmail.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
// You can also use header('Location: thank_you.php'); to redirect to another page.
}
?>
<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>
<form action="form.php" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="email"><br>
Image: <input type="file" name="image_name"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
every time i click submit it tells me the mail is sent but when i check my inbox i find nothing can anyone help ???
I would suggest you switch to PHPMailer, it will take care of email configurations for you.

How to make an array of files with .php / HTML?

[TLDR] Big Newbie needs to make a array of files, holding data by user input from code below, that is viewable?[TLDR]
If the title is a misnomer sorry, but I am a very very green newbie trying to do a coding project with no experience in dealing with FTP or HTML or php.
How do i make a "array" of files , each element of the array being generated upon the entry of user data(first name, last name, email, comments) in main page of a website? These files in the array would hold the user's name, email, and comments, and be able to be removed.
I am using FileZilla Client to connect to my FTP server greenhousebra.comli.com/index.php .
On that page is the forum input for:
First Name [ text field]
Last Name [text field ]
Email[text field ]
Message[Text box]
[submit button]
When the text fields are entered, and submit is hit, it should send a email to the email address entered that I want to function like a ticket, and that works so far. But then I want another option to be able to go to another page and see the array of other people who entered their data as well.
the code for this was obtained from another stackexchange post, since i dont know any .php or HTML, and is posted below, it was held in my index.php file for greenhousebra.comli.com
The HTML code, in index.php file :
<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>
<form action="mail_handler.php" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
The mail_handler.php file
`
<?php
if(isset($_POST['submit'])){
$to = "email#example.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
// You can also use header('Location: thank_you.php'); to redirect to another page.
// You cannot use header and echo together. It's one or the other.
}
?>

Categories