Sending a mail with an attachment using php function sendmail - php

i have a form like this :
this is 1.php
<form action="2.php" method="post">
Name: <input type="text" name="name" /></br>
Email: <input type="text" name="email" />
<input type="submit" value="Submit" />
than 2.php is like this :
<form action="3.php" method="post">
<input type="hidden" name="name" value="<?php echo $_POST['name'];?>"/>
<input type="hidden" name="email" value="<?php echo $_POST['email'];?>"/>
Telephone: <input type="text" name="telephone" /></br>
Location: <input type="text" name="location" />
<input type="submit" value="Submit" />
and the last one 3.php is like this:
<?php
$error = "";
if(isset($_POST['submit'])) {
if(empty($_POST['email']) || empty($_POST['name'])) {
$error .= 'There is an error. Retry. <br />';
}
if(!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)) {
$error .= 'E-mail is incorrect. <br />';
}
function sendmail() {
$to = 'user#domain.tld';
$name = "Name : " .$_POST['name'];
$telephone = "Telephone : " . $_POST['telephone']. "";
$location = "Location : " .$_POST['location'];
$subiect = "Form from webpage";
$body = 'form content: '. $name. "\n". $telephone. "\n\n" . $location. "\n\n". $from ;
$from = "From: " . $_POST['email']. "";
if(mail($to,$subiect,$body,$from)) {
echo '<p>Thank you !</p><br />';
}else{
echo '<p>Error with server</p>';
}
}
if($error == "") {
sendmail();
}
echo "<p>".$error."</p>";
}
?>
My question is how can i introduce a <input type="file" name="file" id="file" /> in the 1st page when the user will be able to upload a photo and then on the 3.php page when he will hit the submit button that picture to be in the email
can anyone give me an example please ? Thank you !

Related

Contact form keeps sending me blank emails

I keep receiving blank form submissions from my contact form. The emails are all coming from "CGI Mailer", and have the form labels, but all the inputs are blank.
The form does work as it should otherwise.
<?php
$userName = $_POST['name'];
$userPhone = $_POST['phone'];
$userEmail = $_POST['email'];
$userMessage = $_POST['text'];
$honeypot = $_POST['firstname'];
$to = "email address";
$subject = "New Email From ";
$body = "Information Submitted:";
$body .= "\r\n Name: " . $userName;
$body .= "\r\n Phone " .$userPhone;
$body .= "\r\n Email: " . $userEmail;
$body .= "\r\n Message: " . $userMessage;
if( ! empty( $honeypot ) ){
return;
}else{
mail($to, $subject, $body);
}
header('Location: thanks.html');
?>
<form method="post" name="myForm" action="sendform.php">
<input name="firstname" type="text" id="firstname" class="hide-robot">
<input id="name" class="form-control" name="name" placeholder="Name..." required /><br />
<input id="phone" class="form-control" name="phone" placeholder="Phone..." required /><br />
<input id="email" class="form-control" name="email" placeholder="E-mail..." required /><br />
<textarea id="text" class="form-control" name="text" placeholder="How can we help you?" style="height:150px;" required></textarea><br />
<input class="btn btn-primary" type="submit" value="submit" id="submit"/><br /><br />
</form>

Why is this not working?

I've this little script to send email! But it's not working... Firstly it says that my variables are not defined and then, they confirm me that the message has been sent, but it doensn't happen!
Name:
<form action="contactus.php" method="POST">
<input type="text" size="32" value="" name="name">
</form> <br><br>
E-Mail:
<form action="contactus.php" method="POST">
<input type="text" size="32" value="" name="header">
</form> <br><br>
Subject:
<form action="contactus.php" method="POST">
<input type="text" size="32" value="" name="subject">
</form> <br><br>
Message: <br>
<form action="contactus.php" method="POST">
<textarea rows="10" cols="40" name="message" value=""></textarea>
</form>
<br><br>
<form action="contactus.php" method="POST">
<input type="submit" value="Submit" name="submit">
</form>
<?php
// php script to send emails
$to = 'some#email.com';
if (isset ($_POST['message'])) {
$message = "$name" . "<br><br>" . $_POST['message'];
}
if (isset ($_POST['header'])) {
$header = "From:" . $_POST['header'];
}
if (isset ($_POST['subject'])) {
$subject = ($_POST['subject']);
}
if (isset ($_POST['name'])) {
$name = ($_POST['name']);
}
if (isset($_POST['submit'])){
mail($to, $subject, $message, $header);
echo "Your message has been sent!";
}
//end of the php script
?>
If some of you can help me would be great!
Thank you.
You can't use all those different form elements. You are using 5 separate forms and the only one that is being submitted is the one with the submit button.
Thus, when the form is being submitted there $_POST['submit'] is set, but none of the other ones exist.
So you need your HTML to be:
<form action="contactus.php" method="POST">
Name:
<input type="text" size="32" value="" name="name"><br><br>
E-Mail:
<input type="text" size="32" value="" name="header"><br><br>
Subject:
<input type="text" size="32" value="" name="subject"><br><br>
Message: <br>
<textarea rows="10" cols="40" name="message" value=""></textarea><br><br>
<input type="submit" value="Submit" name="submit">
</form>
and contactus.php:
<?php
$to = 'some#email.com';
if(isset($_POST['message']) && isset($_POST['header']) &&
isset($_POST['subject']) && isset($_POST['submit']) &&
#mail($to, $_POST['subject'], $_POST['message'], $_POST['header'])) {
echo "Your message has been sent!";
}else{
echo "There has been a problem.";
}
?>
Try:
if(mail($to, $subject, $message, $header)) {
echo "Mail sent successfully.";
} else {
echo "PHP's mail() function failed!";
}
Why you have a Form element for each input elements?
You should put all of your elements in a Form together.

Why is my form validation not working?

I'm having troubles validating my form. How do I validate the form using PHP? I've tried lots of different methods and nothing has worked. I can get the inputs to display (although check-box doesn't always display) but it just won't validate.
I also want to display the user's inputs (after it has been validated) onto another page, how do I do that?
Here is my code;
Form:
<form action="<?php $_SERVER['PHP_SELF'];?>" method="post">
<label for="name">Your Name:</label>
<input type="text" name="name" id="name" value="" required>
<br><br>
<label for="email">Your Email:</label>
<input type="text" name="email" id="email" value="" required>
<br>
<br>
<label for="subject">Subject:</label>
<input type="text" name="subject" id="subject" value="" required>
<br>
<br>
Recipient:
<div>
<label for="admin">
<input type="checkbox" name="recipient[]" id="admin" value="Administrator">
Administrator</label>
<br>
<label for="editor">
<input type="checkbox" name="recipient[]" id="editor" value="Content Editor">
Content Editor</label>
<br>
</div>
<br>
<label for="message">Message:</label>
<br>
<textarea name="message" id="message" cols="45" rows="5" required></textarea>
<input type="hidden" name="submitted" value="1">
<br>
<input type="submit" name="button" id="button" value="Send">
<br>
</form>
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
if ($_POST['submitted']==1) {
if ($_POST['name']){
$name = $_POST['name'];
}
else{
echo "<p>Please enter a name.</p>" ;
}
if (preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email))
$email = $_POST['email'];
}
else{
echo "<p>Please enter a valid email.</p>";
}
if ($_POST['subject']){
$subject = $_POST['subject'];
}
else{
echo "<p>Please enter a subject.</p>";
if(empty($_POST['recipient'])){
echo "<p>Please select a recipient</p>";
}else{
for ($i=0; $i < count($_POST['recipient']);$i++) {
echo $_POST['recipient'][$i] . " ";
}
}
}
if ($_POST['message']){
$message = $_POST['message'];
}
/* go to form.php
display results
echo "<strong>Your Name:</strong> ".$name. "<br />";
echo "<strong>Your Email:</strong> ".$email. "<br />";
echo "<strong>Subject:</strong> ".$subject. "<br />";
echo "<strong>Recipient:</strong> ";
echo "<br />";
echo "<strong>Message:</strong> <br /> " .$message;
*/
?>
if (preg_match("/^[_a-z0-9-]+(.[_a-z0-9-]+)#[a-z0-9-]+(.[a-z0-9-]+)(.[a-z]{2,3})$/i", $email)
You can't use email in this condition because $email is empty!

Get Checkbox Values from Forms using an External PHP Processor

I have a form that sends its data to a process.php file for processing in that file I have the following code:
<?php
$name = $_GET['name'];
$number = $_GET['number'];
$email = $_GET['email'];
$comment = $_GET['message'];
$sales = $_POST['sales'];
$lettings = $_POST['lettings'];
$to = "me#me.com";
$subject = "New website registration";
$message = "Name: ".$name."\r\n";
if(isset($number) && $number!='')
$message.= "Number: ".$number."\r\n";
if(isset($email) && $email!='')
$message.= "Email: ".$email."\r\n";
if(isset($comment) && $comment!='')
$message.= "Comment: ".$comment."\r\n";
if(isset($sales))
{
$message.= "I am Interested in Sales" . "\r\n";
}else{
//
}
if(isset($lettings))
{
$message.= "I am Interested in Lettings";
}else{
//
}
$headers = "From: ".$name." <".$email.">";
$result = mail($to,$subject,$message,$headers);
return $result;
?>
The HTML for the form is as follows:
<form id="register_form" name="register" method="post" action="/content/contact/process.php">
<input type="text" id="name" name="name" class="regform" onFocus="if(this.value=='Name'){this.value='';}" onBlur="if(this.value==''){this.value='Name';}" value="Name" />
<input type="text" id="number" name="number" class="regform" onFocus="if(this.value=='Phone number'){this.value='';}" onBlur="if(this.value==''){this.value='Phone number';}" value="Phone number" />
<input type="text" id="email" name="email" class="regform" onFocus="if(this.value=='Email address'){this.value='';}" onBlur="if(this.value==''){this.value='Email address';}" value="Email address" />
<textarea id="message" name="message" class="regtext" onFocus="if(this.value=='Message'){this.value='';}" onBlur="if(this.value==''){this.value='Message';}">Message</textarea>
<label for"sales">Sales<input type="checkbox" name="sales" value="1" /></label>
<label for"sales">Lettings<input type="checkbox" name="lettings" value="1"/></label>
<input id="submit" type="submit" value="Submit" class="regsender" />
<div class="loading"></div>
</form>
When the checkboxes are checked, the expected messages do not come through in the email. Am I missing something here. I've tried several different approaches to getting this to work, none of which do. Any help would be appreciated. Thanks.
You are using method = POST and retrieving values with $_GET
I have updated your code check it
<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$number = $_POST['number'];
$email = $_POST['email'];
$comment = $_POST['message'];
if (isset($_POST['sales']))
{
$sales = $_POST['sales'];
}
else if(isset($_POST['lettings']))
{
$lettings =$_POST['lettings'];
}
$to = "me#me.com";
$subject = "New website registration";
$message = "Name: ".$name."\r\n";
if(isset($number) && $number!='')
$message.= "Number: ".$number."\r\n";
if(isset($email) && $email!='')
$message.= "Email: ".$email."\r\n";
if(isset($comment) && $comment!='')
$message.= "Comment: ".$comment."\r\n";
if(isset($_POST['sales']))
{
$message.= "I am Interested in Sales" . "\r\n";
}
else if(isset($_POST['lettings']))
{
$message.= "I am Interested in Lettings";
}
echo $message;
$headers = "From: ".$name." <".$email.">";
$result = mail($to,$subject,$message,$headers);
return $result;
}
?>
<form id="register_form" name="register" method="post" action="testing_page.php">
<input type="text" id="name" name="name" class="regform" onFocus="if(this.value=='Name'){this.value='';}" onBlur="if(this.value==''){this.value='Name';}" value="Name" />
<input type="text" id="number" name="number" class="regform" onFocus="if(this.value=='Phone number'){this.value='';}" onBlur="if(this.value==''){this.value='Phone number';}" value="Phone number" />
<input type="text" id="email" name="email" class="regform" onFocus="if(this.value=='Email address'){this.value='';}" onBlur="if(this.value==''){this.value='Email address';}" value="Email address" />
<textarea id="message" name="message" class="regtext" onFocus="if(this.value=='Message'){this.value='';}" onBlur="if(this.value==''){this.value='Message';}">Message</textarea>
<label for"sales">Sales<input type="checkbox" name="sales" value="1" /></label>
<label for"sales">Lettings<input type="checkbox" name="lettings" value="1"/></label>
<input id="submit" name="submit" type="submit" value="Submit" class="regsender" />
<div class="loading"></div>
</form>

Form shows '/' in every field

My php script to send mail is as follows :
<?php
if (isset($_POST['submit'])) {
$to='info#animalswecare.com';
$fname=stripslashes($_POST['fname']);
$email=$_POST['email'];
if (get_magic_quotes_gpc())
$email = stripslashes($email);
//$email=trim($email, '/');
$msg=$_POST['msg'];
$msg=stripslashes($msg);
$message="Name: $fname\n" ."Message: $msg\n";
mail($to,$subject,$message,'From:'.$email) ;
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="contact us">
<label>Name:</label>
<input type="text" name="fname" value=<?php if(!empty($fname)) echo $fname; ?> /><br />
<label>Email:</label>
<input type="text" name="email" value=<?php if (!empty($email)) echo $email; ?> /><br />
<label>Message:</label>
<textarea name="msg" rows="5"><?php if (!empty($msg)) echo $msg; ?></textarea> <br />
<input type="submit" name="submit" value="Post" />
</form>
But when the form appears, there is a / added in every field.
I have tried using trim, rtim, get magic quotes and stripslashes but nothing is working.
It because your value='s are not being ended.
Try this: <input type="text" name="fname" value="<?php if(!empty($fname)) echo $fname; ?>" /> and apply the same learning's to all of the inputs.
Also I re-formatted your code and added label in the CSS..so you don't have use those ugly line-spaces.
<?php
if (isset($_POST['submit'])) {
$to = 'info#animalswecare.com';
$fname = stripslashes($_POST['fname']);
$email = $_POST['email'];
if (get_magic_quotes_gpc()) {
$email = stripslashes($email);
}
// $email = trim($email, '/');
$msg = $_POST['msg'];
$msg = stripslashes($msg);
$message = "Name: $fname\n" ."Message: $msg\n";
mail($to, $subject, $message, 'From:' . $email);
}
?>
<style>
label {width: 120px;}
</style>
<form action="" method="post">
<label>Name:</label>
<input type="text" name="fname" value="<?php if(!empty($fname)) echo $fname; ?>" /><br />
<label>Email:</label>
<input type="text" name="email" value="<?php if (!empty($email)) echo $email; ?>" /><br />
<label>Message:</label>
<textarea name="msg" rows="5"><?php if (!empty($msg)) echo $msg; ?></textarea><br />
<input type="submit" name="submit" value="Post" />
</form>
try adding " " around the value attribute
ie
<input type="text" name="email" value="<?php if (!empty($email)) echo $email; ?>" />
This will teach you to follow standards and always enclose tag parameter values in quotes
First of all, don't use stripslashes(). Simply disable magic_quotes_gpc completely, using the php.ini file or .htaccess. If you can't, view the accepted answer to this question: How to turn off magic quotes on shared hosting?
About your error...
value=<?php if (!empty($email)) echo $email; ?> />
If you look, you didn't put the quotes around the attribute "value".
value="<?php if (!empty($email)) echo $email; ?>" />
will fix it

Categories