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?
Related
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
Hello I am trying to add a html email form to my website but cannot get it to work. The following is the code I currently have but when I try the form I don't receive an email.
<?php
$action=$_REQUEST['action'];
if ($action=="") /* display the contact form */
{
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
Your name:<br>
<input name="name" type="text" value="" size="30"/><br>
Your email:<br>
<input name="email" type="text" value="" size="30"/><br>
Your message:<br>
<textarea name="message" rows="7" cols="30"></textarea><br>
<input type="submit" value="Send email"/>
</form>
<?php
}
else /* send the submitted data */
{
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$message=$_REQUEST['message'];
if (($name=="")||($email=="")||($message==""))
{
echo "All fields are required, please fill the form again.";
}
else{
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="Message sent using your contact form";
mail("****************", $subject, $message, $from);
echo "Email sent!";
}
}
?>
I recommend using PHPMailer.
It will save you a lot of trouble and there are plenty of guides, with freely available default configs where all you need to do is modify the contents, sender and recipient of the email.
first - use $_POST, not $_REQUEST;
second - is there right settings on your server? maybe there is no mail support;
and the last - 4th argument of mail function is not "from", it's headers where you can also set mime-type and others..
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
the error is in this line :
mail("****************", $subject, $message, $from);
replace the asterisks with your email
mail function returns boolean, check whether it is sending or not sending.
Change your mail code like below and try to know whats happening.
`
if (mail("himmerz#gmail.com", $subject, $message, $from)) {
echo 'send';
} else {
echo 'not send';
}
`
mail("****************", $subject, $message, $from);
change to:
mail($email, $subject, $message, $from);
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.
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";
}
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Mail not being received by hotmail.com
I have this simple form on my site and I do not receive emails when it sends into my Hotmail account, not even the Junk folder.
Here is the form code:
<form action="mail.php" method="POST">
<p><label title="Name">Name:</label><br />
<input type="text" name="name" autocomplete="on" required="required"></p>
<p><label title="Email">Email:</label><br />
<input type="text" name="email" autocomplete="on" required="required"></p>
<p><label title="About">My message is about...</label><br />
<select name="about">
<option value="general">General Query</option>
<option value="wedding">Wedding</option>
<option value="corporate">Corporate Event or Trade Show</option>
<option value="other">Other Event</option>
</select>
<p><label title="Message">Message:</label><br />
<textarea name="message" rows="6" cols="25" required="required"></textarea></p>
<input type="submit" value="Send">
</form>
And the mail.php file:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$about = $_POST['about'];
$formcontent="From: $name \n About: $about \n Message: $message";
$recipient = "MyEmailAddress#Live.co.uk";
$subject = "Contact Form";
$mailheader = "Reply-To: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>
I do end up seeing a page with "Thank you!" displayed but no email is received.
Mail delivery is a tricky business... just because you send mail does not mean that anyone will receive it. Many receiving servers will simply ignore the incoming message if it does not meet certain criteria (in my experience Gmail and Hotmail are particularly prone to just silently denying delivery, so it doesn't even end up in SPAM). There are a few things to make sure you've done:
1) You've set up PTR/SPF (reverse lookup) entries in your DNS records
2) Make sure that you're not on any blacklists (http://www.mxtoolbox.com/blacklists.aspx)
3) Expand your headers
$headers = "MIME-Version: 1.0\r\n"
."Content-Type: $contentType; charset=utf-8\r\n"
."Content-Transfer-Encoding: 8bit\r\n"
."From: =?UTF-8?B?". base64_encode("Your sending display name") ."?= <$from>\r\n"
."Reply-To: $replyTo\r\n"
."X-Mailer: PHP/". phpversion();
However, if you really want to ensure that mail gets through, send mail via SMTP. You can never guarantee mail delivery, but it will be much more reliable. If you're not sending a large volume of mail, you might try using Mandrill or similar service to relay emails for you.
You can use the following method. returns true on success.
function sendMail($email, $subject, $message)
{
$supportEmail = 'info#abc.com';
$from = 'Abc';
$msg = $message;
$from = str_replace(' ', '-', $from);
$frm = $from.' <'.$supportEmail.'>';
preg_match("<(.*)#(.*\..*)>", $frm, $match);
///////////////////Headers/////////////////
$hdr='';
$hdr.='MIME-Version: 1.0'."\n";
$hdr.='content-type: text/html; charset=iso-8859-1'."\n";
$hdr.="From: {$frm}\n";
$hdr.="Reply-To: {$frm}\n";
$hdr.="Message-ID: <".time()."#{$match[2]}>\n";
$hdr.='X-Mailer: PHP v'.phpversion();
$x=#mail($email, $subject, $msg, $hdr);
if($x==0)
{
$email=str_replace('#','\#', $email);
$hdr=str_replace('#','\#',$hdr);
$x=#mail($email, $subject, $msg, $hdr);
}
return $x;
}