This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
Closed 4 years ago.
I want to make a website(small one) I want to make people able to send write their username and password to be sent to my email(because I am still developing a database and I want to make sure if it will work or not)
I tried to put php in the same file as html but when it processes the code it just shows the code but dooesn't run it
<html>
<head>
<?php
if(isset)($_POST['name']) && isset($_POST['email'])) {
$pass = $_POST['pass'];
$email = $_POST['email'];
$to = 'omar12haytham#gmail.com';
$subject = 'Password';
$body = '<html>
<body>
<h2>Hello</h2>
<hr>
<p>Email<br>' . $email . '</p>
<p>Password<br>' . $pass . '</p>
</body>
</html>';
//headers
$headers = "From: ".$name." <".$email.">\r\n";
$headers .= "Reply-To: ".$email."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=utf-8";
//send
$send = mail($to, $subject, $body, $headers);
if ($send) {
echo '<br>';
echo 'Your email or password is incorrect';
} else {
echo error;
}
}
</head>
?>
<body>
<form action="" method="post">
<input type="text" name="name" placeholder="Write your email"><br>
<input type="password" name="email" placeholder="Write your password"><br>
<button type="submit">Log in</button>
</form>
</body>
</html>
I expected it to send me an email but it doesn't must I launch it? I am testing it as .html file not as a working website.
This piece of code doesn't work: isset)($_POST['name']) use instead if(isset($_POST["name"]))
Related
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 3 years ago.
I have an HTML form that posts to a PHP mail function. For some reason it does not send the content anymore. The emails display "This email has no content." Am I missing something?
//HTML - Post method form.
<form class="form" id="form" action="submitform.php" method="post">
<h2 style="padding-top: 10px;">Contact Us</h2>
<input class="input" type="text" placeholder="Name" name="name"><br>
<input class="input" type="text" placeholder="E-Mail Address" name="email"><br>
<input class="input" type="text" placeholder="Phone #" name="phone"><br>
<textarea class="input" placeholder="Questions, Specifications, etc."
name="message</textarea>
<input class="inputButton" type="submit">
</form>
//PHP - Gets posted HTML input data and sends it to the email, formatted with \n
<?php
$to = 'example#example.com' ;
$subject = 'Inquiry' ;
$name = $_POST['name'] ;
$email = $_POST['email'] ;
$phone = $_POST['phone'] ;
$message = $_POST['message'] ;
$message = "From: $name \nEmail: $email \nPhone: $phone \nMessage: $message \n";
$sent = mail ($to, $subject, $message) ;
if($sent == true) {
echo "<h3>Thank you for your inquiry.</h3>";
}else{
echo "<h3>Sorry, your message wasn't sent.</h3>;
}
?>
I've found defining headers, even if I just want default values, helps me get my mail delivered. I can't say this is what you need to do because your code looks like it should run successfully as is.
An example of setting the headers to (I believe) default values:
$to = $to_email;
$subject = $your_subject;
$message = $your_message;
$headers = "From: S.O.<no-reply#dontusethis.email>\r\n" .
"Reply-To: [can be nice, not needed of course]\r\n" .
"X-Mailer: PHP/" . phpversion();
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to,$subject,$message,$headers);
This question already has answers here:
PHP E-Mail Encoding?
(7 answers)
Closed 5 years ago.
I have this web page.
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
<title>Send Email</title>
</head>
<body>
<form method="GET">
<input type="text" name="myEmail" placeholder="Имейл"/><br>
<input type="text" name="recieverEmail" placeholder="Получател"/><br>
<input type="text" name="subject" placeholder="Предмет"/><br>
<textarea rows="4" cols="50" name="message" placeholder="Съобщение"></textarea><br>
<input type="submit" name="button" value="send"/>
</form>
<?php
if(isset($_GET['button'])){
$sender = $_GET['myEmail'];
$reciever = $_GET['recieverEmail'];
$subject = $_GET['subject'];
$message = $_GET['message'];
$headers = "From: " . $sender . "\r\n";
mail($reciever, $subject, $message, $headers);
}
?>
</body>
</html>
I get data from the html form. I need a way to set the encoding of the email to UTF-8 because when I want to send email in Bulgarian, it is not translating into Bulgarian. Any ideas? Thank you in advance.
Add one more header attribute in your code like this:
if(isset($_GET['button'])){
$sender = $_GET['myEmail'];
$reciever = $_GET['recieverEmail'];
$subject = $_GET['subject'];
$message = $_GET['message'];
$headers = "From: " . $sender . "\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8"; //check this header attribute. It will append to the `From` header.
mail($reciever, $subject, $message, $headers, $encoding);
}
I have created a simple html/php form where visitors on my site can write their name, email and message and then send the message to my email. Problem is that when they submit the email, my site then performs a full refresh (it looks like) and therefore just reloads to the top of my site. I would like for the user to remain at the same scroll position after submit, so that they can instantly see whether the submit was succesful or not. So either a solution that prevents the refresh or some other solution that automatically scrolls down vertical to the form.
Can you tell me if this is possible using php? Or do I have to use some jquery/ajax solution?
Below is the code I am using. I am a complete novice, so please be gentle.
<form action="" method="post" id="form">
<div class="contact-info-group">
<label for="name"><span>Your name</span>
<input type="text" id="name" name="name" autocomplete="off" value="<?php echo $name; ?>"></label>
<label for="email"><span>Your email</span>
<input type="email" id="email" name="email" autocomplete="off"></label>
</div>
<label for="message"><span>Your message</span>
<textarea id="message" name="message"></textarea></label>
<input id="button1" type="submit" class="button next" name="contact_submit" value="Send message">
<?php
// Check for header injections
function has_header_injection($str) {
return preg_match("/[\r\n]/", $str);
}
if (isset ($_POST['contact_submit'])) {
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$msg = $_POST['message'];
// Check to see if $name or $email have header injections
if (has_header_injection($name) || has_header_injection($email)) {
die();
}
if (!$name || !$email || !$msg) {
echo '<div class="contact-warning"><h2>! Error - Please note that all of the above fields are required !</h2></div>';
exit;
}
// Add the recipient email to a variable
$to = "email#email.com";
// Create a subject
$subject = "Message via website.com - $name";
// Construct the message
$message = "Name: $name\r\n";
$message .= "Email: $email\r\n";
$message .= "Message: \r\n\r\n$msg";
// Clean up the message
$message = wordwrap($message, 72);
// Set the mail headers into a variable
$headers = "MIME-Version 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: $name <$email> \r\n";
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: High\r\n\r\n";
// Send the email
mail($to, $subject, $message, $headers);
echo '<div class="contact-warning"><h2>Thank you for your message. We will get back to you shortly.</h2></div>';
}
?>
</form>
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I'm learning php and I'm trying to send an email to myself using an html form but it doesn't works.
<form action="index.php" role="form" method="post" name="emailform">
<div class="form-group">
<label for="email">Email address:</label>
<input type="email" class="form-control" id="email" name="email">
</div>
<div class="form-group">
<label for="comment">Text:</label>
<textarea type="textarea" class="form-control" id="textarea" rows="5" name="textarea"></textarea>
</div>
<button type="submit" class="btn btn-default" id="submit" name="submit">Submit</button>
</form>
<?php
function email()
{
$to = 'my_mail';
$message = $_POST['textarea'];
$from = $_POST['email'];
$subject = 'Portfolio';
$mail_headers = "From: " . $from . " <" . $from . ">\r\n";
$mail_headers .= "Reply-To: " . $from . "\r\n";
$mail_headers .= "X-Mailer: PHP/" . phpversion();
echo $to . " " . $message . " " . $from;
if(#mail($to, $subject, $message, $mail_headers))
echo #mail($to, $subject, $message, $mail_headers);
else echo "ERROR";
}
if(isset($_POST['submit']))
email();
?>
my_mail is my mail (I replaced it here but in the code there is my real email).
The code seem to work, in fact it display the echo #mail but the mail doesn't appear in my inbox
The code looks fine, but I think because of # in the mail function you are not seeing any errors. If you don't want to display error you can use it like this:
<?php
if(#mail($to, $subject, $message, $mail_headers)){
echo "Mail Sent!";
}else{
print_r(error_get_last());
}
?>
In this way the error is not thrown but you can use error_get_last() to see the error and log it if you want.
PS you are using mail function 2 times, so mail would be sent two times when it is going to work.
here is the correct way.
<form action="contact.php" etc... etc..>
blah blah blah
</form>
Then create a new php file (contact.php) and use the following
<?php
$field_name = $_POST["cname"];/* change " " according to your form */
$field_email = $_POST["cmail"];
$field_sub = $_POST["csub"];
$field_message = $_POST["cmsg"];
$to = "mailid1#mail.com, mailid2#mail.com";
$subject = " give subject" ;
$message = "message";
if(mail($to,$subject,$message))
{
echo "<script>alert('Your Message was sent Successfully. Thank You For Your Time !');</script>";
}
else
{
echo "<script>alert('Something wrong happened. Please try again later. Sorry For The Trouble'); </script>";
}
?>
<meta http-equiv="refresh" content="2; url=contact.html">
<!-- for coming back to intial page -->
Basically I am trying to re work a plugin so what I need to do is post the form data on the Cart.php form on the same page. Below is the set up I have but the $_POST info is not returning anything when email is sent:
Cart.php
<form id='SimpleEcommCartCartForm' action="" method="post">
<div id="emailForm">
<p>Please fill out the form below and one of our associates will contact you with more information.</p>
<div class="col-xs-4">
Name: <input type="text" name="name" id="name">
</div>
<div class="col-xs-4">
E-mail: <input type="email" name="email" id="email">
</div>
<div class="col-xs-4">
Phone: <input type="tel" name="phone" id="phone">
</div>
</div>
</form>
<?php
//Send the email
$to = "test#gmail.com";
$name = $_POST['name'] ;
$from = $_POST['email'] ;
$phone = $_POST['phone'] ;
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "From: $from";
$subject = "Pump Part Inquiry";
$emailBody = "
<html>
<head>
<style>
</style>
</head>
<body>
<h1> Pump Inquiry</h1>
<h3>From:".$name."<h3>
<h3>Phone:".$phone."<h3>
<p>Minetuff Parts".$mine."</p>
<p>Flygt Parts".$flygt."</p>
</body>
</html>";
$send = mail($to, $subject, $emailBody, $headers);
?>
It sends the mail with nothing in it (or missing information) if you load it right away, and it's because of a specific reason.
As it stands, your code will send you an Email as soon as you load the page, so it's best to wrap your (PHP) code inside an isset() to work in conjunction with a (named) submit button, which seems to be missing in your originally posted question/code.
Plus, you have two undefined variables:
$mine and $flygt, so you'll need to define those to fit your needs.
I.e.: if(isset($_POST['submit'])) and <input type="submit" name="submit" value="Send">
Sidenote: It's best to check for empty fields, but that's another topic; see my footnotes.
Tested and working, and receiving all info and I've replaced your present mail function with if(mail($to, $subject, $emailBody, $headers)){...}
<form id='SimpleEcommCartCartForm' action="" method="post">
<div id="emailForm">
<p>Please fill out the form below and one of our associates will contact you with more information.</p>
<div class="col-xs-4">
Name: <input type="text" name="name" id="name">
</div>
<div class="col-xs-4">
E-mail: <input type="email" name="email" id="email">
</div>
<div class="col-xs-4">
Phone: <input type="tel" name="phone" id="phone">
<input type="submit" name="submit" value="Send">
</div>
</div>
</form>
<?php
//Send the email
if(isset($_POST['submit'])){
$to = "test#gmail.com";
$name = $_POST['name'] ;
$from = $_POST['email'] ;
$phone = $_POST['phone'] ;
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "From: $from";
$subject = "Pump Part Inquiry";
// $mine = " Mine variable"; // replace this
// $flygt = " Flygt variable"; // replace this
$emailBody = "
<html>
<head>
<style>
</style>
</head>
<body>
<h1> Pump Inquiry</h1>
<h3>From:".$name."<h3>
<h3>Phone:".$phone."<h3>
<p>Minetuff Parts".$mine."</p>
<p>Flygt Parts".$flygt."</p>
</body>
</html>";
// $send = mail($to, $subject, $emailBody, $headers);
if(mail($to, $subject, $emailBody, $headers)){
echo "Mail sent.";
}
else{
echo "Sorry, something went wrong.";
}
} // brace for if(isset($_POST['submit']))
?>
If you're still having problems:
Add error reporting to the top of your file(s) right after your opening <?php tag, which will help during production testing.
error_reporting(E_ALL);
ini_set('display_errors', 1);
Footnotes:
You are open to XSS attacks (Cross-site scripting).
Use the following (PHP) filter function: FILTER_SANITIZE_FULL_SPECIAL_CHARS
$name = filter_var($_POST['name'], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
Equivalent to calling htmlspecialchars() with ENT_QUOTES set. Encoding quotes can be disabled by setting.
To check for empty fields, you can add this below if(isset($_POST['submit'])){
if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['phone'])){
echo "Fill out all the fields";
exit;
}
|| means "OR", which could very well be replaced by OR if you want, however || has precedence over OR.
This is a very basic method; there are other ways of accomplishing this, but you get the gist of it.
The issue here is that the mail is sent before the submit since you didn't test of the existence of $_POST variables, try something like this:
<form id='SimpleEcommCartCartForm' action="" method="post">
<input type="hidden" name="action" value="mailing"/>
<div id="emailForm">
<p>Please fill out the form below and one of our associates will contact you with more information.</p>
<div class="col-xs-4">
Name: <input type="text" name="name" id="name">
</div>
<div class="col-xs-4">
E-mail: <input type="email" name="email" id="email">
</div>
<div class="col-xs-4">
Phone: <input type="tel" name="phone" id="phone">
</div>
<input name="submit" type="submit"/>
</div>
</form>
<?php
if(isset($_POST['mailing'])){
// Send the email
$to = "test#gmail.com";
$name = $_POST['name'] ;
$from = $_POST['email'] ;
$phone = $_POST['phone'] ;
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "From: $from";
$subject = "Pump Part Inquiry";
$emailBody = "
<html>
<head>
<style>
</style>
</head>
<body>
<h1> Pump Inquiry</h1>
<h3>From:".$name."<h3>
<h3>Phone:".$phone."<h3>
<p>Minetuff Parts".$mine."</p>
<p>Flygt Parts".$flygt."</p>
</body>
</html>";
$send = mail($to, $subject, $emailBody, $headers);
}
?>
Once the email is sent, you haven't told PHP to output anything to the page.
You could add something like echo 'Mail sent'; to the end of the PHP so if the script is being executed correctly then you'll know about it.
If the problem lies within the email not being sent at all, this may be a problem with your server and you should look for a tutorial on how to set up mail correctly. If you're using shared hosting, there are some companies that do not allow the use of the PHP mail() function so I would check with them first.
Amongst this I would recommend that you use something like PHPMailer, a full email library for PHP. Then you could configure for your emails to be sent from anywhere that supports SMTP (not sure if others are supported but there most likely is). PHPMailer will work with shared hosting.
Updated - Code that you can try
Try this out in replacement of $emailBody:
$emailBody = <<<EOT
<html>
<head>
<style type="text/css">
</style>
</head>
<body>
<h1>Pump Inquiry</h1>
<h3>From: $name<h3>
<h3>Phone: $phone<h3>
<p>Minetuff Parts $mine</p>
<p>Flygt Parts $flygt</p>
</body>
</html>
EOT;
NB: Some email clients only allow the use of inline CSS, so if you did decide to add anything to your <style> tag in the <head>, don't expect it to work very well or at all in some cases.
As someone has mentioned before, you'll also need to protect against cross-site scripting.