Php mail Contact Form Error - php

I have bought hosting and created web mail.But mail function of contact form is not working in my hosting. It prints" Error".I didnt understand where I am wrong. I have searhed in google, looked the examples but I didnt solve my problem.Please help me. Thanks.
Edit info: When I bougt hosting, it was windows hosting. The maik function was working. But, I passed the linux hosting, then created again mail in cpanel. But, now the mail function is not working. Thanks again.
Edit2: Hi this code worked in windows hosting. I think problem is originated from linux hosting...
This is my code:
contact.html
<html>
<form action="contact_form.php" method="POST" enctype="multipart/form-data" id="contactform">
<fieldset class="row">
<legend>Contact me :)</legend>
<p>
<label for="your-name">Your Name</label>
<input type="text" name="name" id="your-name" class="input-xlarge">
</p>
<p>
<label for="your-email">Your Email <span class="required">(required)</span></label>
<input type="email" name="email" id="your-email" class="input-xlarge" required>
</p>
<p>
<label for="your-subject">Subject</label>
<input type="text" name="subject" id="your-subject" class="input-xlarge">
</p>
<p>
<label for="your-message">Your message <span class="required">(required)</span></label>
<textarea name="message" cols="50" rows="10" id="your-message" class="input-xxlarge" required placeholder="What do you want to say?"></textarea>
</p>
<!-- This is hidden for normal users -->
<div class="hidden">
<label>
Do not fill out this field
<input name="s_check">
</label>
</div>
<p>
<input type="submit" id="submit" name="submit" class="primary" value="Send Message">
</p>
<p hidden id="response"></p>
</fieldset>
</form>
</html>
contact_form.php
<?php
if(isset($_POST['submit'])) {
$to = 'info#xyz.com';
$name = stripslashes($_POST['name']); //sender's name
$email = stripslashes($_POST['email']); //sender's email
$subject = stripslashes($_POST['subject']); // the subject
echo $name."<br/>";
echo $email."<br/>";
echo $subject."<br/>";
//The message you will receive in your mailbox
$msg = "From : $name \r\n"; //add sender's name to the message
$msg .= "e-Mail : $email \r\n"; //add sender's email to the message
$msg .= "Subject : $subject \r\n\n";
$msg .= "---Message--- \r\n".stripslashes($_POST['message'])."\r\n\n"; $msg .= "---User information--- \r\n"; //Title
$msg .= "User IP : ".$_SERVER["REMOTE_ADDR"]."\r\n"; //Sender's IP
$msg .= "Browser info : ".$_SERVER["HTTP_USER_AGENT"]."\r\n"; //User agent
$msg .= "User come from : ".$_SERVER["HTTP_REFERER"]; //Referrer
if (mail($to, $subject, $msg, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n")){
//Message sent!
echo nl2br("
<div class=\"MsgSent\">
<h1>Congratulations!!</h1>
<p>Thank you <b><?=$name;?></b>, your message is sent!<br /> I will get back to you as soon as possible.</p>
</div>
");
exit;
}
else{
// Display error message if the message failed to send
echo "
<div class=\"MsgError\">
<h1>Error!!</h1>
<p>Sorry <b><?=$name;?></b>, your message failed to send. Try later!</p>
</div>";
exit;
}
}
?>

<p>Thank you <b><?=$name;?></b>, your message is sent!<br />
<p>Sorry <b><?=$name;?></b>, your message failed to send. Try later!</p>
Should be
<p>Thank you <b>{$name}</b>, your message is sent!<br />
<p>Sorry <b>{$name}</b>, your message failed to send. Try later!</p>
Also not sure why you are using nl2br a simple echo should suffice.

As you've said elsewhere, the mail() function is problematic -- it is very low level and not easy to work with.
You would be much better advised to not use it at all -- there are several very good mailer classes for PHP that are much easier to use, give much better results, and are significantly more powerful.
My suggestion is to use phpMailer; it's very easy to use -- as you can see from this example page. But there are several other alternatives libraries available as well.

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

PHP Contact Form doesn't deliver mails [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
My contact form won't work. The messages aren't sent to the given email address.
The form successes but the emails aren't delivered. The other question has another code using if tags, which won't work for my script.
The html code I used is:
<form class="form" id="form1" action="mail.php" method="post">
<p class="name">
<input name="name" type="text" class="validate[required,custom[onlyLetter],length[0,100]] feedback-input" placeholder="NAME" id="name" />
</p>
<p class="email">
<input name="email" type="text" class="validate[required,custom[email]] feedback-input" id="email" placeholder="E-MAIL" />
</p>
<p class="text">
<textarea name="text" class="validate[required,length[6,300]] feedback-input" id="comment" placeholder="WHAT'S UP?"></textarea>
</p>
<div class="submit">
<input type="submit" name="submit" value="SEND" id="button-blue"/>
<div class="ease"></div>
</div>
</form>
The PHP script I use is:
<?php
// We create a variable for name value
$name = $_POST['name'];
// We create a variable for email value
$email = $_POST['email'];
// We create a variable for message value
$message = $_POST['text'];
// We provide an e-mail address from which the email is sent
$from = "xyz#adress.com";
// Provide the e-mail address on which you want to receive messages
$to = "xyz#gmx.de";
// Provide the subject of the e-mail
$subject = "Contact form from xyz.com";
// We prepare the message body
$emailbody = "";
$emailbody .= "Message: " . $message . "\n";
// We add UTF-8 to the header of our message
$header = "";
$header .= "From:" . $from . " \n";
$header .= "Content-Type:text/plain;charset=utf-8";
// Sending message
$success = mail($subject, $emailbody, $header);
// Redirect after sending the message
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=de/confirmation_signup.html\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=de/error.html\">";
}
?>
Anybody has a clue what is going wrong inside the form?
You are passing vars to mail function wrong;
Change this:
mail($subject, $emailbody, $header);
To this:
mail($to,$subject, $emailbody, $header);
See: http://php.net/manual/en/function.mail.php

php script not working when sending from an external domain? [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I'm trying to run a php script on a website contact form. It's probably worth mentioning it's from a website template I bought and I have designed the website using this. My html/css/php knowledge is 'absolute beginner level' hence why I am on here...
Below is the php script (this came with the template). However it's not sending email through to the recipient email address. I've been told it's because the script is trying to send email from an external domain (ie the email address of the website visitor) through the domainname.co.uk mail server, and it’s going to reject it - how can I edit this script so that it works?
This is the PHP script:
<?php
session_start();
$email_to = 'enquiries#bonnelhomes.co.uk'; // change with your email
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = "From: $email\r\n";
$headers .= "Reply-To: $email\r\n";
if(mail($email_to, $subject, $message, $headers)){
echo "success";
}
else{
echo "failed";
}
This is the html for the contact form:
<form id="contact" class="row" name="form1" method="post" action="send.php" >
<div class="span4">
<label>Name</label>
<input type="text" class="full" name="name" id="name" />
</div>
<div class="span4">
<label>Email <span class="req">*</span></label>
<input type="text" class="full" name="email" id="email" />
<div id="error_email" class="error">Please check your email</div>
</div>
<div class="span8">
<label>Message <span class="req">*</span></label>
<textarea cols="10" rows="10" name="message" id="message" class="full"></textarea>
<div id="error_message" class="error">Please check your message</div>
<div id="mail_success" class="success">Thank you. Your message has been sent.</div>
<div id="mail_failed" class="error">Error, email not sent</div>
<p id="btnsubmit">
<input type="submit" id="send" value="Send" class="btn btn-large" />
</p>
</div>
</form>
Any help would be much appreciated. Many thanks in advance :o)
replace
$headers = "From: $email\r\n";
$headers .= "Reply-To: $email\r\n";
with
$headers = "From: mail#yourdomain.com\r\n";
UPDATE
since it is a contact form don't forget to add your user's details to $message
$message = $name. "<br>" .$email. "<br>" .$message;
security tip, on this line:
$email_to = 'enquiries#bonnelhomes.co.uk';
has security problem. An attacker can modify the headers, message and use your server to send unlimited spam messages to victims.
so add this line for more security:
if (strlen($email_to) > 30 || $email_to !== 'enquiries#bonnelhomes.co.uk') {
exit("Bye Hacker!");
}
If you still have problems sending "from your server" you might let some real email-server do the work. There is some Framework you can use which is called PHP-Mailer.
To use it, you have to download the framework and to place it into your server. Using this, you might wanna use SMTP (login-information from some real email-account).
it would look like:
require './PHPMailer/PHPMailerAutoload.php';
$mailer = new PHPMailer;
here you configure your email account to send the emails from. look on your hosters help-files to find out what you need to use to log in successfully:
$mailer->isSMTP();
$mailer->SMTPAuth = true;
$mailer->Host = 'smtp.strato.de';
$mailer->Username = 'your#sendingaccount.de';
$mailer->Password = 'xxxxxxx';
$mailer->SMTPSecure = 'ssl';
$mailer->Port = 465;
$mailer->From = 'your#sendingaccount.de';
$mailer->FromName = 'Mr Tester';
here you configure your actual email, you want to send:
$mailer->addAddress('enquiries#bonnelhomes.co.uk','Mr Admin');
$mailer->Subject = 'this is a contactform email';
$mailer->AltBody = 'your text with bla and request');
if($mailer->send()){
echo 'yeah man!';
}else{
echo 'some error occured';
}
Normally this is not absolutly necessary but it helps on servers with sendingproblems or if your emails get blockt by spam blockers.

PHP Form Submit Button Unclickable

SOLVED - permissions
I want to walk through my debug process so that it might help anyone else working through the same thing... 1) I wiped both pages and replaced with the code that I knew worked. 2) I then changed the form piece by piece until I got it how i wanted and continued testing 3) I then copied the current php file completely and redirected my form to it. 4) it failed... I changed the permissions to 655 and wallah it worked. Now I can go about hacking about the PHP code to get what I want. thanks for all of the suggestions, you definitely led me down the road to my solution
SOLVED
I have two separate intake forms on a site. Intake form 1 works perfectly. I takes, name, email and comment and sends it through a sendmail script.
I also wanted an intake form for lead capture to track those that want to access the demo videos so I modified the code from the form (for the new page) and then created an additional php file called videoform.php - which is basically just a modified version of my sendmail.php file.
When I fill out the form it does nothing when I click on submit. It validates, as it not let you enter a null value in any of the fields but I am not sure what I am missing. Is it something simple (I am by no means PHP reliable) or can I simply not do that?
Here is the form and the php:
<div class="message"></div>
<form action="./php/videoform.php" method="POST" id="contact-form">
<p class="column one-half">
<input name="name" type="text" placeholder="Your Name" required>
</p>
<p class="column one-half">
<input name="email" type="email" placeholder="Your Email" required>
</p>
<p class="column one-half">
<input name="phone" type="text" placeholder="Your Phone" required>
</p>
<p>
<input name="submit" type="submit" value="Submit">
</p>
</form>
</div>
This is the PHP
<?php if(!$_POST) exit;
$to = "xxxxx#example.com";
$email = $_POST['email'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$content = $_POST['content'];
$subject = "You've been contacted by $name";
$content = "$name filled out a request to view the online videos:\r\n\n";
$content .= "Phone: $phone \n\nEmail: $email \n\n";
if ($success) {
header("Location: /videos.html");
exit;
} else {
header("Location: /video-form.html");
exit;
}
?>
I am comfortable with a number of coding formats but I am so weak when it comes to PHP. Any insight would be both appreciated and get me on the road to understanding PHP better.
Working scripts for comparison
Form
Send us a message
<p class="column one-half last">
<input name="email" type="email" placeholder="Your Email" required>
</p>
<p class="clear">
<textarea name="comment" placeholder="Your Message" cols="5" rows="3" required></textarea>
</p>
<p>
<input name="submit" type="submit" value="Comment">
</p>
</form>
</div>
PHP sendmail.php file
<?php if(!$_POST) exit;
$to = "xxxxx#example.com";
$email = $_POST['email'];
$name = $_POST['name'];
$comment = $_POST['comment'];
$subject = "You've been contacted by $name";
$content = "$name sent you a message from your enquiry form:\r\n\n";
$content .= "Contact Reason: $comment \n\nEmail: $email \n\n";
if(#mail($to, $subject, $content, "Reply-To: $email \r\n")) {
echo "<h5 class='success'>Message Sent</h5>";
echo "<br/><p class='success'>Thank you <strong>$name</strong>, your message has been submitted and someone will contact you shortly.</p>";
}else{
echo "<h5 class='failure'>Sorry, Try again Later.</h5>";
}?>
From your php:
//...
$content = "$name filled out a request to view the online videos:\r\n\n";
$content .= "Phone: $phone \n\nEmail: $email \n\n";
if ($success) {
header("Location: /videos.html");
exit;
} else {
//...
You never define $success. Since it doesn't have a value, if ($success) fails, and it always enters the else portion of the statement. It looks like you're missing a line that's something like $success = mail($to, $subject, $content);

Can't send mail over php

I have a form where an user enters into a message, and the message gets sent to the recipient on the other end. I have tried this script multiple times, scoured tutorials, yet I can't seem to find what's wrong. Any ideas?
HTML Form:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<?php
if(isset($sent))
echo 'Your message has been sent. '; ?>
<label for="Name">Name</label><br />
<input type="text" class="textbox" size="35" id="Name" name="Name" <?php if(isset($name)) echo "value=\"$name\"";?> /><br />
<label for="Service">Service</label><br />
<input type="text" size="35" class="textbox" id="Service" name="Service" <?php if(isset($subject)) echo"value=\"$subject\"";?> /><br />
<label for="Email">Email</label><br />
<input type="text" size="35" class="textbox" id="Email" name="Email" <?php if(isset($from)) echo"value=\"$from\""; ?> /><br />
<label for="message">Message</label><br />
<textarea rows="95" cols="100" id="message" name="message"><?php if(isset($message)) echo"$message"; ?></textarea><br />
<button type="submit">Send Message</button>
</form>
PHP:
if(isset($_POST['Name']) && isset($_POST['Email']) && isset($_POST['Service']) && isset($_POST['message'])) {
$name = $_POST['Name'];
$from = $_POST['Email'];
$subject = $_POST['Service'];
$to = "emailtestertora#gmail.com";
$message = $_POST['message'];
mysql_query("INSERT INTO `Contact`(`Name`, `Email`, `Message`, `Service`) VALUES('$name', '$from', '$message', '$subject')");
$headers = "From:".$from;
if(mail($to,$subject,$message,$headers))
$msgsent = true;
}
Thanks!
(Apologies as this should go as a comment, but it'll be easier layed out in the textbox)
Firstly, debug with the following code:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'on');
if (mail('emailtestertora#gmail.com', 'test', 'test')) {
echo 'Mail Sent';
} else {
echo 'Mail Failed';
}
?>
This script will give you an error message when sending the e-mail that will help you debug.
BUT, the important part of this comment, is that the e-mail script above is open to spam (as well as SQL injection). I would strongly encourage you to use a one of the functions/classes that are available that will help you cut out the security holes holes in your mail script.
If you are determined to roll-your-own then great, but please read up about e-mail spam header injection before letting this script on a server. Spammers can send thousands of e-mails very quickly when they find an open script like this, they regularly test automatically so you must clamp down.
(And read up about PHP Database object - PDO - at the same time to save the MySQL injection.)
Your service provider may block your mail.
If you are in dedicated server you may need to configure a mail server
Thanks
Sreeraj
Use this method
this is a php email script which dose not need any smpt connection
<?php
$to = "someone#example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse#example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>

Categories