Sending email with php and html form [duplicate] - php

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 -->

Related

How to send email by php from html from? [duplicate]

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"]))

Not getting any email from PHP contact form [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 4 years ago.
So I am creating a simple website and on the "contact us" page I have put a contact form that is supposed to mail the message there. The form actually gives a success message that the email is sent but I'm not getting any. I have checked the spam/junk folder as well. I tested this on apache local server. (My email is correct. I've stated it like that on the code here for privacy issues.)
Can I please have some insight on what I am missing since I am fairly new to coding?
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Mesazhi u dergua!'
);
$name = #trim(stripslashes($_POST['emri']));
$email = #trim(stripslashes($_POST['email']));
$phoneno = #trim(stripslashes($_POST['telefon']));
$message = #trim(stripslashes($_POST['mesazhi']));
$email_from = $email;
$email_to = 'name.surname#mail.net';
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Phone NO: ' . $phoneno . "\n\n" . 'Message: ' . $message;
$success = #mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
echo json_encode($status);
die;
?>
<div class="contact-form">
<form action="sendemail.php" name="contact-form" method="post">
<input type="text" name="emri" placeholder="Emri" />
<input type="email" name="email" placeholder="Email" />
<input type="tel" name="telefon" placeholder="Nr Telefoni"/>
<textarea name="text" id="text" rows="8" placeholder="Teksti" name="mesazhi"></textarea>
<input type="submit" class="btn-send" value="Dergo">
</form>
</div>
Use this code mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
you are using a # on each function call. Remove them and you will maybe see where the problem is.

Trouble with PHP

I'm new to PHP. Basically I'm a graphic designer turned frontend designer. I know html css and simple jquery. I need to include this contact form in a website, but doesn't seem to work. Plz help. If someone could point out if anything is wrong with the code, it would be great help.
Thanks in advance.
This is the html part of the contact form :
<div class="form">
<form action="mail.php" method="POST">
<label>Name</label>
<br>
<input type="text" name="name" placeholder="Type Here">
<br><br><br>
<label>Email</label>
<br>
<input type="email" name="email" placeholder="Type Here">
<br><br><br>
<label>Message</label>
<br>
<textarea name="message" rows="20" cols="20" placeholder="Type Here"> </textarea>
<br><br><br>
<label>*What is 2+2? (Anti-spam)</label>
<br>
<input type="text" name="human" placeholder="Type
<br><br><br><br>
<input id="submit" name="submit" type="submit" value="SUBMIT">
</form>
This is the php :
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent=" From: $name \n Message: $message";
$recipient = "saurabhdey84#gmail.com";
$subject = "Vibhor Sogani Website Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You! Your message has been sent." . "-" . <a href='home.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
?>
Change your echo like this,
echo "Thank You! Your message has been sent." . "- <a href='home.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
^// no need of contenation here
If you're calling php page, it is just taking blank values, try these changes and let me know,
include_once("home.html");
if(isset($_POST['submit']))
{
**Your php code here**
}
HTML:
Change your text control to
<input type="text" name="human" placeholder="Type the answer"/>
Formatting mistake detected:
<input type="text" name="human" placeholder="Type
<br><br><br><br>
<input id="submit" name="submit" type="submit" value="SUBMIT">
PHP:
Last echo statement should be like this: (missed double-quotes)
echo "Thank You! Your message has been sent." . "-" . "<a href='home.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
not like this:
echo "Thank You! Your message has been sent." . "-" . <a href='home.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
To be better:
echo "Thank You! Your message has been sent. - <a href='home.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
No need any concatenation here!
Check if your POSTS are getting sent to the server script by echoing $_POST['name']. If it is , then it is a problem with the mail() function.
You are using the default php mail() function.It doesnt work without the correct headers. I see that you have used just one 'reply From' header.So you need to look deeper into using mail function(). An alternate for sending emails could be the phpmailer library which is used widely
try this, and put required at closing tags of input
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = "maild#gmail.com";
$subject = " contact form" ;
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$mesg = '$name $email $message';
$kk=mail($to,$subject,$mesg,$headers);
if($kk){
?>
<script>
window.location.href='thankyou.html';
</script>
}
}
?>
This is how it is usually
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <webmaster#example.com>' . "\r\n";
$headers .= 'Cc: myboss#example.com' . "\r\n";
Like Ive said before , phpmailer library is a more reliable alternate

Simple subscriber email form - PHP/HTML [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I am trying to create a simples subscriber email form , where one user inserts his email, and me and the client should receive a e-mail.
Email to me saying that got a new subscription.
Email to client saying that we will contact him shortly.
I know very little about php, i ask for help about what is wrong on this code and how to make it better in order to make what i want.
HTML:
<div class="mail2">
<!-- Subscription Form -->
<form action="form/form.php" method="post">
<h1>Try Now!</h1>
<input name="email" class="email" type="text" placeholder="Enter your email address ...">
Get started for free
<a class="top" href="#top">Top</a>
</form>
</div>
PHP:
<?php
$to = "office#site.com";
$from = "no-reply#site.com";
$headers = "From: " . $from . "\r\n";
$subject = "New subscription";
$body = "New user subscription: " . $_POST['email'];
if( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) )
{
if (mail($to, $subject, $body, $headers, "-f " . $from))
{
echo 'Your e-mail (' . $_POST['email'] . ') has been added to our mailing list!';
}
else
{
echo 'There was a problem with your e-mail (' . $_POST['email'] . ')';
}
}
else
{
echo 'There was a problem with your e-mail (' . $_POST['email'] . ')';
}
Edit your html code..
<div class="mail2">
<!-- Subscription Form -->
<form action="form/form.php" method="post">
<h1>Try Now!</h1>
<input name="email" class="email" type="text" placeholder="Enter your email address ...">
<input type="submit" value="Get started for free">
<a class="top" href="#top">Top</a>
</form>
</div>
I would have to say first your HTML has an issue. With the Get started now. You're not actually submitting the form, you're just linking back to the page you're on.
So first change the HTML:
<div class="mail2">
<!-- Subscription Form -->
<form action="form/form.php" method="post">
<h1>Try Now!</h1>
<input name="email" class="email" type="text" placeholder="Enter your email address ...">
Get started for free
<a class="top" href="#top">Top</a>
</form>
</div>
Secondly, here's a neater version of the PHP with some improvements.
$to = "office#site.com";
$from = "no-reply#site.com";
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: " . $from;
$headers[] = "Reply-To: " . $from;
$headers[] = "X-Mailer: PHP/".phpversion();
$subject = "New subscription";
$body = "New user subscription: " . $_POST['email'];
if( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) )
{
if (mail($to, $subject, $body, implode("\r\n", $headers)))
{
echo 'Your e-mail (' . $_POST['email'] . ') has been added to our mailing list!';
}
else
{
echo 'There was a problem with your e-mail (' . $_POST['email'] . ')';
}
}
else
{
echo 'There was a problem with your e-mail (' . $_POST['email'] . ')';
}
If the rest doesn't work, make sure that sendmail is installed and setup correctly on the server.
Just to point out that if the mail check returns false it might not specifically be the e-mail that's at fault as your output error message suggests.

PHP mail form is not working

I m having a problem with my email php form, when I click in the submit button an other page shows up saying There was a problem with your e-mail ()
I don't no what I am doing wrong?
here is my code:
html code
<!-- Subscription Form -->
<form class="email" action="form.php" method="post">
<input class="get_notified" type="text" placeholder="Enter your email address ..."/>
<button type="submit" class="go" /></form>
<!-- End Subscription Form -->
</div>
</div>
</body>
php code
<?php
$to = "email#mydomain.com";
$from = "email#mydomain.com";
$headers = "From: " . $from . "\r\n";
$subject = "New subscription";
$body = "New user subscription: " . $_POST['email'];
if( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) )
{
if (mail($to, $subject, $body, $headers, "-f " . $from))
{
echo 'Your e-mail (' . $_POST['email'] . ') has been added to our mailing list!';
}
else
{
echo 'There was a problem with your e-mail (' . $_POST['email'] . ')';
}
}
else
{
echo 'There was a problem with your e-mail (' . $_POST['email'] . ')';
}
You'll need to add a name="email" field to your HTML form in order for PHP to be able to fetch it using $_POST['email']
<input class="get_notified" name="email" type="text" placeholder="Enter your email address ..."/>

Categories