php mail form error - php

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";
}
}

Related

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.

Trouble getting emails to send via php script/html form [duplicate]

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)

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.

Using HTML and PHP to send form data to an email

Like the title says, sending a form to my email. I get no errors, but the email never comes. Here's my HTML form (I don't think you'll need anything else, but there is also some formatting and Java verification in the file).
<form method="POST" name="contactform" action="contact-form-handler.php">
<p>
<label for='name'>Your Name:</label>
<br>
<input type="text" name="name">
</p>
<p>
<label for='email'>Email Address:</label>
<br>
<input type="text" name="email">
<br>
</p>
<p>
<label for='message'>Message:</label>
<br>
<textarea name="message"></textarea>
</p>
<input type="submit" value="Submit">
<br>
</form>
And here's my PHP. Obviously I took my email out and put in EMAIL instead, but other than that this is my complete PHP file. The thank you PHP file pulls up the submitted page just fine, and I get no errors. Just no email either.
<?php
$errors = '';
$myemail = 'EMAIL#gmail.com';
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
if (!preg_match(
"/ ^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "\n Error: Invalid email address";
}
if( empty($errors))
{
$to = '$myemail';
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $email_address\n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}
?>
Thanks ahead of time for any help you can provide! I can give the rest of my HTML file or my other PHP file if you need it, this is where all the real functionality lies though.
*PHP to send form data to an email i have used this code as well as its work for me .you can try *
<?PHP
$email = $_POST["emailaddress"];
$to = "you#youremail.com";
$subject = "New Email Address for Mailing List";
$headers = "From: $email\n";
$message = "A visitor to your site has sent the following email address to be added to your mailing list.\n
Email Address: $email";
$user = "$email";
$usersubject = "Thank You";
$userheaders = "From: you#youremailaddress.com\n";
$usermessage = "Thank you for subscribing to our mailing list.";
mail($to,$subject,$message,$headers);
mail($user,$usersubject,$usermessage,$userheaders);
?>
after that you can addded your futher code
Try add in top file:
error_reporting(E_ALL);
and edit your code, see:
if(mail($to,$email_subject,$email_body,$headers)){
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
} else {
echo 'Error!';
}
Read this:
http://www.php.net/manual/en/function.error-reporting.php
http://www.php.net/errorfunc
http://php.net/manual/pt_BR/function.set-error-handler.php
http://www.php.net/register_shutdown_function
you have the variable $myemail as a string value after $to = Remove the parentheses and your code will work
The problem is with your From field in your $headers variable.
you can't just put any email address in there. For example: you are supposed to put an existing email address of your server that you create. Or if you want to use a gmail account in from field, you need to configure your gmail username and password with your server first before using that email.
The simplest solution is to create a new email address on your hosting server and then put that email address in your from field in $headers variable.
I've already mentioned it in details here.

PHP Form doesn't work

My contact form doesn't work. Emails are not being sent.
Webpage Code :
<!--Form Start-->
<form method="post" action="sendmail.php">
Email: <input name="email" type="text"><br>
Message:<br>
<textarea name="message" rows="15" cols="40"></textarea><br>
<input type="submit">
</form>
<!--Form End-->
PHP Script :
<?php
$to = "oxydyzestudios#gmail.com";
$subject = "Comment";
$email = $_REQUEST['email'];
$message = $_REQUEST['message'];
$headers = "From: $email";
$sent = mail($to, $subject, $message, $headers);
if($sent) {
print "Your mail was sent successfully";
} else {
print "We encountered an error sending your mail";
}
?>
Yes, the email is correct.
Live Demo : http://unitedasone.web1337.net/form.html
Any help would be appreciated.
There's nothing wrong with your code and it reports success.
This is likely a problem with your mail set up on your server, if you're seeing Your mail was sent successfully then php's mail function has successfully passed the mail to the server for delivery.
From php.net:
It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.
How is mail sent on your server?

Categories