Can't send mail over php - 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.";
?>

Related

Simple error check in html form with php?

Newb here, sorry in advance. I want to check for errors in a simple html form. I want the form to email me the customers information. I have my contact.php and errorcheck.php uploaded to my hosting provider. i actually made a working script contactform.php. (working) So I have a clue that my host can process the php code, but my errorcheck.php script is not working. Can anyone check my errorcheck.php script and see what may be causing the problem. all I want errorcheck.php to do so far, is display the error in the web address bar, just so I know that its working. Thanks in advance for helping a newb. I hope I explained my self and code well enough.
contactform.php (working script will connect and send me an email of form information)
errorcheck.php (will not connect or check the form for errors, I want to display the error in web address bar)
contact.php (this is the html form)
errorcheck.php code below
<?php
if (isset($_POST['submit'])) {
$first = $_POST['name'];
$mail = $_POST['mail'];
$subject = $_POST['subject'];
$message = $_POST['message'];
//check if inputs are empty
if (empty($first) || empty($mail) || empty($subject) || empty($message)) {
header("Location:contact.php?signup=empty");
exit();
} else {
if (!preg_match("/^[a-zA-Z]*$/", $first)) {
header("Location:contact.php?signup=char");
exit();
} else {
//check if email is valid
if (!fiter_var($mail, FILTER_VALIDATE_EMAIL)) {
header("Location:contact.php?signup=invalid-email");
exit();
} else {
header("Location:contact.php?signup=signup=success");
}
}
}
}
(html code will connect to contactform.php but not errorcheck.php)
contact.php form code is below
<form class="contact-form" action="contactform.php" method="post">
<label for="name">Name</label>
<input type="text" name="name" placeholder="Full name..">
<label for="mail">Email Address</label>
<input type="text" name="mail" placeholder="Your E-Mail..">
<label for="subject">Phone Number</label>
<input type="text" name="subject" placeholder="Phone Number..">
<label for="message">Message</label>
<textarea name="message" placeholder="Message.."></textarea>
<input type="submit" value="submit" name="submit"></input>
</form>
contactform.php code is below
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['mail'];
$message = $_POST['message'];
$mailTo = "sales#screenrunners.com";
$headers = "From: ".$mailFrom;
$txt = "You have received and e-mail from ".$name.".\n\n".$message;
mail($mailTo, $subject, $txt, $headers);
header("Location: contact.php?mailsend");
}
?>

PHP Mail Form not sending [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I know this is a popular question. However no other questions can give me the answer I'm looking for.
Have a contact form (mail() being using. And I can't get it to send. A coder that helped me create it somehow got his to send, because I received a few messages in my inbox the mail was coded to send to. I copy and pasted the code, and I'm testing it locally, but it's not sending the mail.
Is the problem it's not sending because I'm testing it locally and its not live and hosted yet? or does the problem rely in my code, and if so, where?
***Not including the validation code, but I have it there...
FORM:
<form method="post" action="">
<input type="text" name="name" placeholder="*Name" value="<?php echo $_POST['name']; ?>">
<input type="tel" name="phone" placeholder="*Phone Number" value="<?php echo $_POST['phone']; ?>">
<input type="email" name="email" placeholder="*Email" value="<?php echo $_POST['email']; ?>">
<input type="text" name="invoice" placeholder="Invoice Number (optional)" value="<?php echo $_POST['invoice']; ?>">
<textarea name="comments" maxlength="500" rows="10" cols="10" placeholder="*Please enter your comments here..."><?php echo htmlentities($_POST['comments'], ENT_COMPAT,'ISO-8859-1', true);?></textarea>
<button type="submit">Submit</button>
</form>
PHP:
if(!empty($_POST)){
$POST = filter_post($_POST);
$invoice = array_splice($POST,3,1);
$MSG = check_empty($POST);
$email = test_input($_POST["email"]);
if(!array_filter($MSG)){
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
$MSG[] = "Invalid Email Format (test#provider.com)";
}
else{
$POST['invoice'] = $invoice['invoice'];
if(send_mail($POST)){
header('Location: messageSent.php');
}
else{
$MSG[] = "Email Failed. Please Try Again.";
}
}
}
}
function send_mail($POST){
extract($POST);
$to = '7servicerepairs#gmail.com';
$sbj = 'New Question For Se7en Service!';
$msg = "Name: $name \n Phone: $phone \n Email: $email \n Invoice #: $invoice \n Comments: $comments";
$headers = "From: $email";
return(mail($to, $sbj, $msg, $headers));
}
You will need an smtp server in your localhost. Otherwise you won't be able to send it.

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

Php mail Contact Form Error

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.

Email Notification on new MySQL database entry

I've been creating a blog using tutorials around the web and I have a working comments system but what I would like is if when the user adds a comment that I get an email. I'd really love if you could explain how exactly I could go about implementing a notification. Any help is greatly appreciated.
Current Form:
<form id="commentform" method="post" action="process.php">
<p><input type="hidden" name="entry" id="entry" value="<?php echo $id; ?>" />
<input type="hidden" name="timestamp" id="timestamp" value="<?php echo $commenttimestamp; ?>">
<input type="text" name="name" id="name" title="Name (required)" /><br />
<input type="text" name="email" id="email" title="Mail (will not be published) (required)" /><br />
<input type="text" name="url" id="url" title="Website" value="http://" /><br />
<br />
<textarea title="Your Comment Goes Here" name="comment" id="comment"></textarea></p>
<p><input type="submit" name="submit_comment" id="submit_comment" value="Add Comment" /></p>
</form>
Process.php:
<?php
if (isset($_POST['submit_comment'])) {
if (empty($_POST['name']) || empty($_POST['email']) || empty($_POST['comment'])) {
die("You have forgotten to fill in one of the required fields! Please make sure you submit a name, e-mail address and comment.");
}
$entry = htmlspecialchars(strip_tags($_POST['entry']));
$timestamp = htmlspecialchars(strip_tags($_POST['timestamp']));
$name = htmlspecialchars(strip_tags($_POST['name']));
$email = htmlspecialchars(strip_tags($_POST['email']));
$url = htmlspecialchars(strip_tags($_POST['url']));
$comment = htmlspecialchars(strip_tags($_POST['comment']));
$comment = nl2br($comment);
if (!get_magic_quotes_gpc()) {
$name = addslashes($name);
$url = addslashes($url);
$comment = addslashes($comment);
}
if (!eregi("^([_a-z0-9-]+)(\.[_a-z0-9-]+)*#([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)) {
die("The e-mail address you submitted does not appear to be valid. Please go back and correct it.");
}
mysql_connect ('localhost', 'root', 'root') ;
mysql_select_db ('ultankc');
$result = mysql_query("INSERT INTO php_blog_comments (entry, timestamp, name, email, url, comment) VALUES ('$entry','$timestamp','$name','$email','$url','$comment')");
header("Location: post.php?id=" . $entry);
}
else {
die("Error: you cannot access this page directly.");
}
?>
If what you are looking for code in PHP that can send an email, below is one from here for you to look at. Insert the code to send email just before or after you INSERT the comment into your database.
<?php
//define the receiver of the email
$to = 'youraddress#example.com';
//define the subject of the email
$subject = 'Test email';
//define the message to be sent. Each line should be separated with \n
$message = "Hello World!\n\nThis is my first mail.";
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster#example.com\r\nReply-To: webmaster#example.com";
//send the email
$mail_sent = #mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
Requirements:
For the Mail functions to be
available, PHP must have access to the
sendmail binary on your system during
compile time. If you use another mail
program, such as qmail or postfix, be
sure to use the appropriate sendmail
wrappers that come with them. PHP will
first look for sendmail in your PATH,
and then in the following:
/usr/bin:/usr/sbin:/usr/etc:/etc:/usr/ucblib:/usr/lib.
It's highly recommended to have
sendmail available from your PATH.
Also, the user that compiled PHP must
have permission to access the sendmail
binary.

Categories