Trouble getting emails to send via php script/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 have created a simple html form to send an email using php. Currently whenever I try to send the info I just get redirected to my process.php page with and browser error (myserver.com unable to handle this request).
I have already tried sending a test email by making my php page just the mail() function and it does indeed work so it has to do with my code somewhere. I'm sure it's something simple so here is my code.
HTML (contact.html):
<!-- Contact form -->
<form id="form" action="process.php" method="post">
<div>
<label for='name'><span class='required'></span></label>
<input id="Field1" type='text' name='name' placeholder='Type your Email Here' required/>
</div>
<div>
<label for='message'><span class='required'></span></label>
<textarea id="Field2" name='message' placeholder="Type a Message for us Here" required></textarea>
</div>
<div>
<button type='submit'>SEND MESSAGE</button>
</div>
</form>
PHP (process.php):
<?php
//if "email" variable is filled out, send email
if(isset($_POST['name']) && isset($_POST['message'])) {
//Email information
$admin_email = "test#mydomain.com";
$email = $_POST['name'];
$subject = "Email from contact form";
$comment = $_POST['message'];
//send email
if(mail($admin_email, $subject, $comment, "From:" . $email)) {
echo '<p>Success</p>';
header('Location: contact.html');
} else {
echo '<p>Error sending message</p>';
}
} else {
echo '<p>Please fully fill out the form</p>';
}
?>

You have syntax error in the PHP code you have shared - You are missing ; in the $subject = "Email from contact form" line.
Please see bellow -
<?php
//if "email" variable is filled out, send email
if (isset($_POST['name'], $_POST['message'])) {
//Email information
$admin_email = "test#mydomain.com";
$email = $_POST['name'];
$subject = "Email from contact form";
$comment = $_POST['message'];
// //send email
if(mail($admin_email, $subject, $comment, "From:" . $email)) {
echo '<p>Success</p>';
header('Location: contact.html');
} else {
echo '<p>Error sending message</p>';
}
} else {
echo '<p>Please fully fill out the form</p>';
}
?>

Check your php.ini file for mail configurations.
open php.ini file
search "mail function" there
set SMTP to your server
set sendmail_from (sending mail_ID)

Related

PHP form is not successfully submitting getting custom error

My PHP form isn't submitting successfully. I keep getting the custom error that I wrote ('Oops there was a problem. Please try again").
any help would be greatly appreciated. I'm totally new to PHP so I'm thinking maybe some of my php variables are linked wrong and arent connecting with my mailer-new.php file?
Thanks in advance,
<section class="form-body">
<form method="post" action="mailer-new.php" class="contact-form" >
<div class="row">
<?php
if ($_GET['success']== 1){
echo " <div class=\"form-messages success\"> Thank you!
your message has been sent. </div>";
}
if ($_GET['success']== -1){
echo " <div class=\"form-messages error\"> Opps there was a
problem. Please try again </div>";
};
?>
<div class="field name-box">
<input type="text" name="name" id="name" placeholder="Who
Are You?" required/>
<label for="name">Name</label>
<span class="ss-icon">check</span>
</div>
<div class="field email-box">
<input type="text" name="email" id="email"
placeholder="name#email.com" required/>
<label for="email">Email</label>
<span class="ss-icon">check</span>
</div>
<div class="field msg-box">
<textarea name="message" id="msg" rows="4"
placeholder="Your message goes here..."/></textarea>
<label for="message">Msg</label>
<span class="ss-icon">check</span>
</div>
<input class="button" type="submit" value="Send"/>
</div>
</form>
</section>
MAILER.PHP
<?php
// Get the form fields, removes html tags and whitespace.
$name = strip_tags(trim($_POST["name"]));
$name = str_replace(array("\r","\n"), array(" ", " " ), $name);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$message = trim($_POST["message"]);
// Check the data.
if (empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
header("Location: http://www.conallen.ie/index.php?
success=-1#form");
exit;
}
// Set the recipient email address. Update this to YOUR desired email address.
$recipient = "allenconallen46#gmail.com";
// Set the email subject.
$subject = "New contact from $name";
// Build the email content.
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Message:\n$message\n";
// Build the email headers.
$email_headers = "From: $name <$email>";
// Send the email.
mail($recipient, $subject, $email_content, $email_headers);
// Redirect to the index.html page with success code
header("Location: http://www.conallen.ie/index.php?success=1#form");
?>
This code works correctly in my local machine, even though email is not sent response messages are coming correctly. The changes I have done is changed the host name and made the two lined header redirect into one line in the failed response. Also you have mentioned in the HTML the file name as action="mailer-new.php" and the file name mentioned in the question as MAILER.PHP. Are they same in the code?
To check whether the mail is sent or not remove the header redirect and update like this. If you are getting a failed response means mail is not configured in your server.
if(mail($recipient, $subject, $email_content, $email_headers)) {
echo "mail sent";
} else {
echo "mail sent failed";
}
Alternatively you can use the SMTP for sending email. You can use a library called PHP Mailer and can use any of the valid email address like a gmail account. Please have look at this question if that's the case
Sending email with PHP from an SMTP server

Contact form doesn't send the mail when greek characters are used in the name field

I have a very simple html contact form that asks from the user a name, an email and then the message area. The problem is that when the user enters their name in greek characters (as the site is in greek language), the message never gets delivered. I tested it thoroughly and I found out that there is no problem if in the textarea there are greek characters, the problem appears only in the name field.
The code for my contact form is this one:
<form id="contact" method="post" action="mailer-backup.php" enctype="multipart/form-data" accept-charset="UTF-8">
<input type="text" id="name" name="name" required placeholder="Όνομα">
<input type="email" id="email" name="email" required placeholder="Email">
<textarea id="message" name="message" required placeholder="Μήνυμα"></textarea>
<button id="submit" type="submit">Αποστολή</button>
</form>
As you can see, it calls an external php script, which after messing with for a whole day but without a positive result looks like this:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = strip_tags(trim($_POST["name"]));
$name = str_replace(array("\r","\n"),array(" "," "),$name);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$message = trim($_POST["message"]);
$options="-f contact#my-website.gr";
if ( empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
http_response_code(400);
echo "All fields are required, please fill the form again.";
exit;
}
$recipient = "contact#my-website.gr";
$name = '=?utf-8?b?' . base64_encode($_POST['name']) . '?=';
$from="From: $name<$email>\r\nReturn-path: $email";
$subject = "New contact from $name - my-website.gr";
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Message:\n$message\n";
if (mail($recipient, '=?UTF-8?B?'.base64_encode($from).'?=', $subject, $email_content, $from, $options)) {
http_response_code(200);
echo "Thank You! Your message has been sent.";
} else {
http_response_code(500);
echo "Tragic! Something went wrong and we couldn't send your message.";
}
} else {
echo "There was a problem with your submission, please try again.";
}
?>
I spent my whole day doing all sorts of experiments but as I am not a programmer, I failed to make it work. For any kind people that will respond with a possible solution, please remember, I am NOT a programmer.
I think you actually have a problem with the mail command call arguments - looks like you put your sender's info as your second argument while it should be subject line.
So, when I replaced your call with
mail($recipient, $subject, $email_content, $from, $options)
it worked just fine for me with UTF in the name field.

Unable to send email using form

I'am having trouble with the form. It goes through but I never receive an email. I have checked in spam but nothings showed up. Could someone give me hint. Not able to identify error.
<div class="contact-form">
<form class="email" action="mailer.php" method="post">
<h3>Kontaktirajte nas!</h3>
<div>
<p>Ime:</p>
input type="text" name="name" />
<p>E-mail:</p>
<input type="text" name="email" />
<p>Naslov :</p>
<input type="text" name="subject" />
<p>Poruka:</p>
<textarea name="message"></textarea></p>
<input class="send" type="submit" value="Send">
</form>
</div>
</div>
Below is the php code (mailer.php).
$myemail = "example#gmail.com";
$name = check_input($_POST['name'], "Enter your name");
$subject = check_input($_POST['subject'], "Enter a subject");
$email = check_input($_POST['email']);
$message = check_input($_POST['message'], "Write your message");
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
$message = "
Name: $name
E-mail: $email
Subject: $subject
Message:
$message
";
mail($myemail, $subject, $message);
header('Location: thanks.html');
exit();
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>
</body>
</html>
<?php
exit();
}
?>
The code looks fine and you can reference PHP mail here # http://uk1.php.net/manual/en/function.mail.php
You need to check a FROM address is defined in your code or is set in your php.ini (this could be causing it to get bounced back and not hit your emails)
You don't need a smtp server running if you send out via PHP mail but it's worth testing on a SMTP server to see if this is your issue.
Try W3schools code and see if that sends for you and let us know the results from that...
Link can be found here # http://www.w3schools.com/php/php_mail.asp
Sendin emails can be a very thicky thing.
The first thing to check is that you have a valid sender address defined. Some hosts simply refuce sending emails without valid email.
$additional_headers = "From: from#example.com\r\n";
mail($myemail, $subject, $message, $additional_headers);
Check mail error logs (if you access them), it should give you more insight on what is the problem.

php mail form error

I am trying to send an email through php using this:
<?php
mail("my_email", "Test Message", "welcome to the test message") or die("Error!");
?>
But when i run this in php the email doesnt come through and no error message is created and the die message does not appear anywhere.
I have got this information from http://www.php.net/manual/en/function.mail.php
What have i done wrong? I have been looking but i am unable to find out if its a problem with php or my server and everything i have followed has failed.
can someone clarify this?
----EDIT----
By the looks of it i need to do some more research in this matter, thanks for all your help and ill do some more work
Basic implementation, however, if the above doesn't work then I'm sure you need to set up a MTA
HTML Code
<form action="mail.php" method="post">
<input type="text" name="email" />
<input type="submit" value="submit mail" />
</form>
PHP Code:
if (isset($_POST['email']) && !empty($_POST['email'])) {
$userEmail = $_POST['email'];
$to = strip_tags($userEmail);
$subject = "email subject";
$message= 'email body message goes here';
$headers = "From: anotheremail#test.com";
if (mail($to,$subject,$message,$headers)) {
echo "mail sent";
} else {
echo "error sending mail";
}
}

PHP Email Form Not working

I am using the form below. I am using the page, contact.php to display the form and submit the form. The form validates and says email sent but when i check the email it doesn't show.
PHP:
<?php
function spamcheck($field)
{
//filter_var() sanitizes the e-mail
//address using FILTER_SANITIZE_EMAIL
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
//filter_var() validates the e-mail
//address using FILTER_VALIDATE_EMAIL
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return TRUE;
}
else
{
return FALSE;
}
}
if (isset($_REQUEST['email']))
{//if "email" is filled out, proceed
//check if the email address is invalid
$mailcheck = spamcheck($_REQUEST['email']);
if ($mailcheck==FALSE)
{
echo "Invalid input";
}
else
{//send email
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
mail("me#mysite.com", "Subject: $subject",
$message, "From: $email" );
echo "Thank you for using our mail form";
}
}
else
{//if "email" is not filled out, display the form
echo "<form method='post' action='contact.php'>
Email: <input name='email' type='text' /><br />
Subject: <input name='subject' type='text' /><br />
Message:<br />
<textarea name='message' rows='15' cols='40'>
</textarea><br />
<input type='submit' />
</form>";
}
?>
In your form when the 'thank you' message displays it means the mail function has been run, not that it succeeded.
the php mail function returns true when it succeeds and othwerwise false.
Could you please try something like:
if(mail("me#mysite.com", "Subject: $subject",$message, "From: $email")){
echo "Thank you for using our mail form";
}else{
echo "hmm... seems the mail cannot be sent";
}
also is the mail function allowed in the php.ini?
Check your php log. Maybe it does fail because it does not have a SMTP client configured neither in your app or in PHP to be used when trying to send mails.
Try to send mail "from" an address you have on your contacts. Are you sure your email account accepts these spoofed emails? Gmail sends those php emails from addresses not in your contact list to spam.
Try to test on a different email account. It's worth the shot.
Another test you can run is using an alert to retrieve the mail() return, to check whether it comes true or false..
alert(mail(hey#joe.jh, subject, message, email));

Categories