My basic contact form is giving me the error "error during sending mail". Of course, the code below is very simple and has no protection against spammers or SQL injection, but my main concern at the moment is getting the message to send.
Thanks in advance for any help given, despite the simplicity of the question.
HTML:
<form action="sendmail.php" method="post">
<table>
<tr>
<td>Name:</td>
<td><input type="text" name="Name" size="20" maxlength="40" /></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="Email" size="20" maxlength="60" /></td>
</tr>
<tr>
<td>Message:</td>
<td><textarea name="Message" maxlength="1000" cols="25" rows="6"></textarea></td>
</tr>
</table>
<div align="left">
<input name="submit" type="submit" />
</div>
</form>
sendmail.php PHP:
<?php
if(isset($_POST['submit']))
{
$name = $_POST['Name'];
$email = $_POST['Email'];
$message = $_POST['Message'];
$from = 'From: CWON Australia';
$to = "......#hotmail.com";
$subject = "CWON Message";
$content = "From: $name\n E-Mail: $email\n Message:\n $message";
if(mail ($to, $subject, $content, $from)) {
echo "mail has been sent";
}
else
{
echo "error during sending mail";
}
}
?>
Your code is functional, and I assume you're getting the "mail has been sent" response.
If you aren't, then you'll first want to check your php.ini for the correct sendmail path (this I believe is the default):
sendmail_path = /usr/sbin/sendmail -t -i
If sendmail path is good, do you see your email in mail logs? I'd also test by mailing your user account on the server itself.
It's more likely that you're getting owned by a spam filter, especially if you're emailing to an "#hotmail.com" address.
See this SO question for some guidance
The mail() function is not very reliable in php and it is hard to configure... So I recommend you use the PHPMailer to send your mails...
You can read all about how to send mail using PHPMailer in here : PHPMailer GitHUB
Related
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I am creating a contact us form, In that i am using HTML and PHP .Now my doubt is after click the submit button the details are not sending to mail,I want to know where i am mistaken on my code.
Here my HTML code for create the form
<form action="sendmail.php" method="post" >
<table width="100%" border="0"align="center"cellpadding="3"cellspacing="1">
<tr>
<td width="10%">Subject</td>
<td width="2%">: </td>
<td width="82%"><input name="name" type="text" id="name" size="50"></td>
</tr>
<tr>
<td>Detail</td>
<td width="2%">: </td>
<td width="82%"><textarea name="detail" cols="50" rows="4"id="detail"></textarea></td>
</tr>
<tr>
<td>Email</td>
<td>: </td>
<td><input name="customer_mail" type="text" id="customer_mail" size="50"></td>
</tr>
<tr>
<td>Name</td>
<td>: </td>
<td><input name="name" type="text" id="name" size="50"></td>
</tr>
<tr>
<td><input type="submit" name="send" value="Submit">
<input type="reset" name="submit" value="Reset">
</td>
</tr>
</table>
</form>
PHP code is below:
<?php
$subject="$subject";
$message="$detail";
$mail_from="customer_mail";
//From
$header="from: $name <$mail_from>";
//Enter your email address
$to="simon#abcinfomedia.in";
$sendmail=mail($to,$subject,$message,$header);
//check mail send to ur mail
if($sendmail){
echo"success";
}
else{
echo"Error";
}
?>
please try this:
<?php
if(isset($_POST['send']) && !empty($_POST['send'])) {
$subject = $_POST['subject'];//if you sent by post method else put manually
$message = $_POST['detail'];
$mail_from = $_POST['customer_mail'];
$header = "from: $name <$mail_from>";
//Enter your email address
$to = "simon#abcinfomedia.in";
$sendmail = mail($to, $subject, $message, $header);
//check mail send to ur mail
if ($sendmail) {
echo "success";
} else {
echo "Error";
}
}
?>
This is not working because you're not filling the variables with the data from the form by using $_POST[''].
The HTML doesn't change, but try this PHP:
<?php
$subject = $_POST['subject'];
$message = $_POST['detail'];
$mail_from = $_POST['customer_mail'];
$header = "from: $name <$mail_from>";
$to = "simon#abcinfomedia.in";
//check mail send to ur mail
if(mail($to,$subject,$message,$header)){
echo"success";
}
else{
echo"Error";
}
?>
I have defined subject, message and mail_from using the $_POST data from the form. For the if statement, I have dropped the part where you store the mail() function as a variable and just put it straight into the if.
This will simultaneously send the email and check it at the same time.
In my test, the email worked but there were flaws because some of your fields are missing and not defined, but I believe the above solves your initial problem. :)
I have a rather basic php form that submits on the same page it is called "registration.php". I have it set to collect the form data, store it in the $to, $subject and $body variables then I call the mail() method to send off an email to myself.
please see code below ( Don't worry! there is a question comin' right up! ):
<?php
ob_start(); //output buffering because I like it.
if (isset($_POST['submit'])) {
// Process the form
$message = "Thank you for registering! We will respond to your request shortly";
$name = $_POST['name'];
$email = $_POST['email'];
$address = $_POST['address'];
$comments = $_POST['userComment'];
$date = gmdate("M d Y");
$to = "myemail#outlook.com";
$subject = "Registration Submission";
$body = " Date: $date \n Registrant Name/Name's: $name \n Registrant E-mail: $email \n \n User Comments: \n $comments \n \n";
mail($to,$subject,$body);
}
?>
<form id="contactForm" name="contactForm" action="registration.php" method="post">
<table>
<tr>
<td><label for="name"><strong>Registrant Name / Names</strong></label></td>
<td><input required='required' type="text" id="name" name="name" /></td>
</tr>
<tr>
<td><label for="email"><strong>Registrant E-mail</strong></label></td>
<td><input required='required' type="email" maxlength="40" id="email" name="email" /></td>
</tr>
<tr>
<td><label for="subject"><strong>Registrant Address</strong></label></td>
<td><input required='required' type="text" maxlength="100" id="address" name="address" /></td>
</tr>
<tr>
<td>Message:</td>
<td> </td>
</tr>
<tr>
<td id="textAreaInput" colspan="2"><textarea rows="3" id="userComment" name="userComment" placeholder="Let us know your thoughts!"></textarea></td>
</tr>
</table>
<br />
<button type="submit" name="submit" id="submit">Submit</button>
</form>
What I want to know is Can I send a custom response email to the users collected $email with a custom message using just php? can I send other variables through the mail() method? like $to2, $subject2, $body2 or must they be named $to, $subject & $body when passed?
I have a working solution using PHPMailer found here:
https://github.com/PHPMailer/PHPMailer
I am just curious as to weither or not there is an easy process using PHP that wouldn't rely on a required library.
I think all you have to do is
if(mail($to,$subject,$body)){
mail($email,"Thanks!!","Thank you very much for registering!!!")
}
I am currently creating a contact form and with the help of thw world wide web I made a php background, which I also understand.
Unfortunately it looks al great to me, but I do get aan error, saying:
Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\wamp\www\jquery\mail.php on line 16 [line 16 is the place where the if statement with the mail and the attributes appaeat, see below]
I have honestly no clue what the error means, and my code is below, does anybody now what it means and how I could fix it? Thanks in advance!
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$city = $_POST['city'];
$message = $_POST['message'];
$from = 'From: timohoogie';
$to = 'ownemail#gmail.com';
$subject = 'Contact zoeken met whoduniit';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\n City: $city\n Message:\n $message";
if ($_POST['submit'] && $human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>
<form method="post" action="mail.php">
<label for="Name">Naam:</label>
<input type="text" name="name" id="name" />
<label for="City">Stad:</label>
<input type="text" name="city" id="city" />
<label for="email">Email:</label>
<input type="text" name="email" id="email" />
<label for="gevonden"> Hoe heeft u ons gevonden</label>
<input type="text" name="gevonden" id="gevonden" />
<br>
<br>
<label for="Message">Bericht:</label><br />
<textarea name="message" rows="20" cols="20" id="message"></textarea>
<label>*What is 2+2? (Anti-spam)</label>
<input name="human" placeholder="Type Here">
<input type="submit" name="submit" value="Submit" class="submit-button" />
</form>
You need to have a SMTP server running on the machine that hosts this code with your current settings. Or else you need to change the SMTP settings in your php.ini file.
Read more about how to set this up here: http://www.quackit.com/php/tutorial/php_mail_configuration.cfm
If you want to set up your own SMTP server, I dont have any good tips on top of my head, but there are several free server softwares out there. Although, you would probably be better off using a public one, or even you ISP SMTP server.
mail is a PHP function, yes. But it must be installed on the server that is doing the actual sending. SMTP is a slightly different protocol that seems to be getting triggered because the simple mail function is failing (for it not being installed.)
You need to check with your service provider to see what mail function you should be using.
I am beginner in php and I tried to programe an easy contact form. My question is: whats wrong with my code? I cant seem to find any mistake with formating or anything... This is my code:
contact.php:
...page content...
<div class="contact">
<form action="send_mail.php" method="post">
<fieldset>
<label for="name">Name:</label>
<input type="text" name="name" placeholder="Enter your name" />
<label for="email">Email:</label>
<input type="email" name="email" placeholder="Enter your email address" />
<label for="message">Message:</label>
<textarea name="message" placeholder="What's on your mind?"></textarea>
<br>
<input type="submit" name="submit" value="Send message" />
</fieldset>
</form>
</div>
...page content...
send_mail.php:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: My website';
$to = 'someone#email.cz';
$subject = 'My contact form message';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
}
?>
I am getting the "Something went wrong" message, which means taht mail() returned false but I dont know why. Thanks for any help!
This is a common problem with running scripts on your local computer. Most computers will not have an email server and be configured to send email from that server.
For the PHP mail function to work, your computer must have an email server set up.
You can either look at online hosting with email support or for local testing, install a test mail server.
There is a bit about non mac OS's at the end. I've had some luck with this in the past.
If your on windows, this is probably better: http://www.toolheap.com/test-mail-server-tool/
Your headers need to contain proper headers.
Using only From: My website stands at going to Spam.
Using an Email => From: $email\r\n will work better.
Give this a try, tested.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $email\r\n";
$to = 'your_email#somewhere.com';
$subject = 'My contact form message';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if(empty($email)) {
die("Something went wrong, go back and try again!");
}
elseif (isset($_POST['email'])) {
mail($to,$email,$body,$headers);
echo "<p>Your message has been sent!</p>";
}
?>
1 . To made work this code.you should send your mail from any domain.
for example: you should have a live website thru that domain email id only send mail to
others
2 . use PHP-MAILER coding for sending mail.just download from net and make changes and use
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.";
?>