PHP Mail stops working [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP Mail stops working without me editing the code
I've had a problem with a clients mail function for a while. The thing is that the mail I use to receive the mails stops receiving mails from time to time. I've noticed this because when I change from my clients mail (info#allflytt.com) to my own (daniel#codia.se) it starts working properly. Also the "success function" gives me the message "The mail was not sent." when I use my clients mail (info#allflytt.com). So the problem seems to be located in the mailadress wich somehow, sometimes, doesn't accept the mails. This happens a couple of times every day and all I can do is wait for it to work again. I've posted the code below.
<?php
if(isset($_POST['submit'])){
$mottagare = 'info#allflytt.com';
$titel = 'Meddelande';
$namn = strip_tags($_POST['namn']);
$foretag = strip_tags($_POST['foretag']);
$adress = strip_tags($_POST['adress']);
$postnr = strip_tags($_POST['postnr']);
$ort = strip_tags($_POST['ort']);
$telefon = strip_tags($_POST['telefon']);
$epost = strip_tags($_POST['epost']);
$meddelande = strip_tags($_POST['meddelande']);
$meddelande=nl2br($meddelande);
$body =
"
Namn: <b>". $namn ."</b><br />
Företag: <b>". $foretag ."</b><br />
Adress: <b>". $adress ."</b><br />
Postnr: <b>". $postnr ."</b><br />
Ort: <b>". $ort ."</b><br />
Telefon: <b>". $telefon ."</b><br />
E-post: <b>". $epost ."</b><br /><br />
Meddelande: <b><br />". $meddelande
;
$headers = 'From: '. $namn .' <webmaster#allflytt.com>' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
$success = mail($mottagare, $titel, $body, $headers);
}
?>
<?php
if(!($success)){
echo "<p class=\"field_error\">The mail was not sent.</p>";
}
?>
<?php
if($success){
echo "<p class=\"p_success\">Great! The mail was sent.</p>";
}
?>
<form method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
<p>
<label for="namn">Namn: *</label><br />
<input type="text" name="namn" id="namn" class="text" tabindex="15" />
<br />
<label for="foretag">Företag:</label><br />
<input type="text" name="foretag" id="foretag" class="text" tabindex="20" />
<br />
<label for="adress">Adress:</label><br />
<input type="text" name="adress" id="adress" class="text" tabindex="30" />
<br />
<label for="postnr">Postnummer:</label><br />
<input type="text" name="postnr" id="postnr" class="text_medium" tabindex="40" />
<br />
<label for="ort">Ort:</label><br />
<input type="text" name="ort" id="ort" class="text" tabindex="50" />
<br />
<label for="telefon">Telefon: *</label><br />
<input type="text" name="telefon" id="telefon" class="text" tabindex="60" />
<br />
<label for="epost">E-post:</label><br />
<input type="text" name="epost" id="epost" class="text" tabindex="70" />
<br />
<label for="meddelande">Meddelande: *</label><br />
<textarea name="meddelande" id="meddelande" class="textarea" tabindex="80"></textarea>
<br />
<input type="submit" name="submit" value="Skicka" class="submit" />
</p>
</form>

The problem is likely with the $namn variable, which can get practically any value, and therefore could either not be a valid e-mail at all, or could be an e-mail that's being blocked by the recipient.
Firstly you should make sure that $namn is a valid e-mail, but personally I would recommend that you use a single constant address for your From, preferably one the domain of which resolves to your mail server's IP.

Problem solved. Changed to a new server host. No issues any more!

Related

mail.php can't get it to email the correct data

I've no experience with php, due to an old formtomail cgi script no longer working on a host server I decided to switch to a simple html to php mail for a contact page on a website. I took a template online and set up the html page but having difficulty editing the mail.php file so that all the data requests on my html form get emailed over to me. The template I use just had email and message. My html contact page has different requests i.e contact name not name, and asks for more information. so I need the php form to email all this information and need help on how to edit the php file to include whats requested on the html contact page.
I have a contact form HTML page with the following:
<form action="mail.php" method="POST">
<p class="bodytextgrey">
Contact Name:<br /><input type="text" name="contact name" size="50" /><br />
Firm Name:<br /><input type="test" name="firm name" /><br /><br />
E-mail: (Please enter the email address you would like the document is to be sent to)<br />
<input type="text" name="email" size="50" /><br />
Job Number:<br /><input type="test" name="job number" /><br /><br />
Document you require:<br /><form action=""><select name="Document you require">
<option value="Data Sheet">Data Sheet</option>
</select><br/ ><br />
Discount code:<br /><input type="text" name="Discount code" size="20" /><br /><br />
<input type="submit" value="Send"><input type="reset" value="Clear">
</form>
I then have a mail.php with this (MY EMAIL is my actual email):
<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "MY EMAIL";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>
Would also like it to bring up an existing thank you.html page rather than just a thankyou word on a white page so also need help adding in how I link this.
Any help would be appreciated
Formatting your code will help immensely to start debugging this. I've reformatted your current HTML with some indenting and line breaks to help make it clear:
<form action="mail.php" method="POST"> <!-- missing closing tag -->
<p class="bodytextgrey"> <!-- missing closing tag -->
Contact Name:<br />
<input type="text" name="contact name" size="50" /><br />
Firm Name:<br />
<input type="test" name="firm name" /><br /><br />
E-mail: (Please enter the email address you would like the document to be sent to)<br />
<input type="text" name="email" size="50" /><br />
Job Number:<br />
<input type="test" name="job number" /><br /><br />
Document you require:<br />
<form action=""> <!-- nested form in a form will break things -->
<select name="Document you require">
<option value="Data Sheet">Data Sheet</option>
</select><br/ ><br />
Discount code:<br />
<input type="text" name="Discount code" size="20" /><br /><br />
<input type="submit" value="Send">
<input type="reset" value="Clear">
</form>
There are a few things wrong with this:
Opening <p> with no </p> (closing tag) anywhere
<form action=""> nested inside of an existing form isn't allowed and will cause submission issues (see "Permitted content" at https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form)
The closing </form> that exists matches the <form action="">, but you're missing a </form>
You don't have a message field anywhere
Here's the updated HTML with my suggested fixes:
<form action="mail.php" method="POST">
<p class="bodytextgrey">
Contact Name:<br />
<input type="text" name="contact_name" size="50" /><br />
Firm Name:<br />
<input type="test" name="firm_name" /><br /><br />
E-mail: (Please enter the email address you would like the document to be sent to)<br />
<input type="text" name="email" size="50" /><br />
Job Number:<br />
<input type="test" name="job_number" /><br /><br />
Document you require:<br />
<select name="document">
<option value="Data Sheet">Data Sheet</option>
</select><br/ ><br />
Discount code:<br />
<input type="text" name="discount" size="20" /><br /><br />
Message:<br />
<textarea name="message"></textarea><br /><br />
<input type="submit" value="Send">
<input type="reset" value="Clear">
</p>
</form>
As others have mentioned, it's not clear what the 500 error is, so that will need to be resolved before this will work. But here's a cleaned up version of the PHP. This has been simply tested at https://3v4l.org/WePRh, though it won't show the form or any input values, but it does prove that the script runs. This is formatted better, includes all form inputs, and should make things easier to work with:
<?php
// get all form values
$input['Contact name'] = $_POST['contact_name'] ?? '';
$input['Firm name'] = $_POST['firm_name'] ?? '';
$input['Email'] = $_POST['email'] ?? '';
$input['Job number'] = $_POST['job_number'] ?? '';
$input['Document'] = $_POST['document'] ?? '';
$input['Discount'] = $_POST['discount'] ?? '';
$input['Message'] = $_POST['message'] ?? '';
// generate the email content (i.e. each line is like "Contact name: Mark")
$formContent = '';
foreach($input as $key => $value) {
$formContent .= $key . ": " . $value . "\n";
}
$to = "my#email.com"; // TODO: replace this with your email address
$subject = "Contact Form";
/*
Note that this was likely causing downstream issues. Most email clients (gmail, outlook) will mark emails as spam
if you try to send an email from an email address that your server isn't configured to send from.
See https://stackoverflow.com/questions/17533863/how-to-configure-php-to-send-e-mail for more info.
It's also safer to define an email address like "no-reply#example.com" to send your emails from.
*/
$mailheader = "From: no-reply#example.com";
mail($to, $subject, $formContent, $mailheader) or die('Error sending email');
// redirect to the thank you page
// TODO: update this URL to be yours
header('Location: http://www.example.com/thankyou.html');
exit;
?>
You can use the header() function:
Example:
header('location: thankyou.html');

PHP Email Function sending a blank body message with subject & sender email

I am trying to make a quote forum that sends the input to an email. I have PHP sending the email, displaying the subject and the senders email, but it doesn't display the main body of content in the email that is sent.. here is my code:
<?php
$action=$_REQUEST['action'];
if ($action=="") /* display the contact form */
{
?>
<div class="quote_text">Send us some of your information and we will get back to you as soon as possible. Let's get this ball rolling.</div>
<br /><br />
<span class="important">* = required fields</span><br />
<form action="" method="POST" enctype="multipart/form-data">
<table width="814" height="310" border="0">
<tr>
<td width="419">
<input type="hidden" name="action" value="submit">
Your Name:<span class="important">*</span><input name="name" type="text" value="" size="30"/><br /><br />
Your Email:<span class="important">*</span><input name="email" type="text" value="" size="30" placeholder="example#example.com"/><br /><br />
Phone Number:<input name="phone" type="text" value="" size="10" maxlength="12" placeholder="(xxx)-xxx-xxxx" /><br /><br />
Company Name:<input name="company_name" type="text" value="" size="30"/><br /> <br />
</td>
<td width="385">
Address of Installation:<span class="important">*</span>
<input name="install_address" type="text" value="" size="30"/><br /><br />
City:<span class="important">*</span><input name="city" type="text" value="" size="30"/><br /><br />
State:<span class="important">*</span><input name="state" type="text" value="" size="30"/><br /><br />
Zip Code:<span class="important">*</span><input name="zip" type="text" value="" size="30"/><br /><br /><br /><br />
</td>
</tr>
<tr>
<td height="102">
Fence Type Description:<br /><textarea name="description" rows="6" cols="45"></textarea>
Number of Corners:<input name="corners" type="text" value="" size="30"/>
</td>
<td><br />
Linear Feet:<input name="linear" type="text" value="" size="30"/><br />
OR<br />
Acres:<input name="acres" type="text" value="" size="30"/><br /><br />
Number of Gate Openings:<input name="gate_opening" type="text" value="" size="30"/>
</td>
</tr>
</table><br><br>
<input type="submit" value="Send email"/>
</form>
<?php
} else {
// Grab forum elements and push into variables for the email
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$phone = $_REQUEST['phone'];
$company_name = $_REQUEST['company_name'];
$install_address = $_REQUEST['install_address'];
$city = $_REQUEST['city'];
$state = $_REQUEST['state'];
$zip = $_REQUEST['zip'];
$description = $_REQUEST['description'];
$corners = $_REQUEST['corners'];
$linear = $_REQUEST['linear'];
$acres = $_REQUEST['acres'];
$gate_opening = $_REQUEST['gate_opening'];
//Build Email
$message="$name<br />
$email<br />
$phone<br />
$company_name<br />
$install_address<br />
$city<br />
$state<br />
$zip<br />
$description<br />
$corners<br />
$linear<br />
$acres<br />
$gate_opening<br />";
$message=$_REQUEST['message'];
if (($name=="")||($email=="")||($install_address="")||($city="")||($state="")|| ($zip=""))
{
echo "Please fill the required fields. Click here to try again.";
}
else{
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="Blue Ridge Fencing - Quote Forum";
mail("info#blueridgefenceco.com", $subject, $message, $from);
echo "Email sent! We will get back to you as soon as possible.";
}
}
?>
Thank you so much for you help!

Can someone find where's wrong in this code PHP email

I'm not receiving mails on the email mail#example.com. Below is my form code and my send-mail.php code. Can anyone help me with this cause everything seems working great bu i'm not receiving any emails. I'm using localhost as the server.
Contact form:
<form id="contactForm" action="#" method="post">
<p>Email us by filling in the form below. Make sure you fill in the message and all fields.</p>
<fieldset>
<div>
<input name="name" id="name" type="text" class="form-poshytip" title="Enter your name" />
<label>Name</label>
</div>
<div>
<input name="web" id="web" type="text" class="form-poshytip" title="Enter your surname" />
<label>Surname</label>
</div>
<div>
<input name="email" id="email" type="text" class="form-poshytip" title="Enter your email address" />
<label>Email</label>
</div>
<div>
<textarea name="comments" id="comments" rows="5" cols="20" class="form-poshytip" title="Enter your comments"></textarea>
</div>
<!-- send mail configuration -->
<input type="hidden" value="mail#example.com" name="to" id="to" />
<input type="hidden" value="Enter the subject here" name="subject" id="subject" />
<input type="hidden" value="send-mail.php" name="sendMailUrl" id="sendMailUrl" />
<!-- ENDS send mail configuration -->
<p><input type="button" value="Send" name="submit" id="submit" /> <span id="error" class="warning">Message</span></p>
</fieldset>
</form>
<p id="sent-form-msg" class="success">Form data sent. Thanks for your feedback.</p>
<!-- ENDS form -->
and here is the send-mail.php
<?php
//vars
$subject = $_POST['subject'];
$to = explode(',', $_POST['to'] );
$from = $_POST['mail#example.com'];
//data
$msg = "NAME: " .$_POST['name'] ."<br>\n";
$msg .= "EMAIL: " .$_POST['email'] ."<br>\n";
$msg .= "WEBSITE: " .$_POST['web'] ."<br>\n";
$msg .= "COMMENTS: " .$_POST['comments'] ."<br>\n";
//Headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers .= "From: <".$from. ">" ;
//send for each mail
foreach($to as $mail){
mail($mail, $subject, $msg, $headers);
}
?>
$_POST['subject'];
$_POST['to'];
$_POST['myemail#gmail.com'];
$_POST['name'];
$_POST['email'];
$_POST['web'];
$_POST['comments'];
I didn’t find any of these elements in your form. That's the reason why nothing is happening.Try
echo '<pre>';
print_r($_POST);
This will give you the posted array when the form is submitted.
i have some suggestion.if u have kept the 'to' address as hidden in the form then why cant u try keeping it directly in sendmail function and in $from you try to keep
<?php
$to="kurtfarrugia92#gmail.com";
$from =$_POST['field_name'];
// not the mail id because i didn't see any field with name as "kurtfarrugia92#gmail.com"
?>
You cannot use this function to send mail from localhost. I am not sure but you should try PHP mailer for this task.

PHP Mail stops working without me editing the code

I have a problem with my PHP Mail. It stops working from time to time without me even touching the code. I have a script which checks if required forms are empty. This script works. But if I fill in all the required fields I should get a text which says "Your mail was successfully sent" but I don't. But if I edit my code by just moving a bit of text some lines down and back to the original position again it works, for a while. It's often under the night it stops working. Can it be the server that is causing trouble or is it my code that I posted below?
<?php
if(isset($_POST['submit'])){
$namn = strip_tags($_POST['namn']);
$foretag = strip_tags($_POST['foretag']);
$adress = strip_tags($_POST['adress']);
$postnr = strip_tags($_POST['postnr']);
$ort = strip_tags($_POST['ort']);
$telefon = strip_tags($_POST['telefon']);
$epost = strip_tags($_POST['epost']);
$meddelande = strip_tags($_POST['meddelande']);
function check_required_fields($required_array){
$field_errors = array();
foreach($required_array as $fieldname){
if ((!isset($_POST[$fieldname])) || (empty($_POST[$fieldname]))){
if($_POST[$fieldname] != '0'){
$field_errors[] = $fieldname;
}
}
}
return $field_errors;
}
$errors = array();
$required_fields = array('namn', 'telefon', 'meddelande');
$errors = array_merge($errors, check_required_fields($required_fields));
if(empty($errors)){
$meddelande=nl2br($meddelande);
if(empty($foretag)){ $foretag='-'; }
if(empty($adress)){ $adress='-'; }
if(empty($postnr)){ $postnr='-'; }
if(empty($ort)){ $ort='-'; }
if(empty($epost)){ $epost='-'; }
$body =
"
Namn: <b>". $namn ."</b><br />
Företag: <b>". $foretag ."</b><br />
Adress: <b>". $adress ."</b><br />
Postnr: <b>". $postnr ."</b><br />
Ort: <b>". $ort ."</b><br />
Telefon: <b>". $telefon ."</b><br />
E-post: <b>". $epost ."</b><br /><br />
Meddelande: <b><br />". $meddelande
;
$headers = "From: $namn <webmaster#allflytt.com>\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";
$headers .= "X-Mailer: PHP v".phpversion();
$success = mail('info#allflytt.com', 'Meddelande', $body, $headers);
}
}
?>
<form method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
<?php
if(!empty($errors)){
echo "<p class=\"field_error\">De rödmarkerade fälten måste fyllas i.</p>";
}
?>
<?php
if(empty($errors) && $success){
echo "<p class=\"p_success\">Tack för ditt meddelande! Vi kommer att besvara det inom kort.</p>";
}
?>
<p>
<label for="namn">
<?php if(!empty($errors)){if(in_array("namn", $errors)){echo "<span class=\"field_error\">";}}?>Namn: *<?php if(!empty($errors)){if(in_array("namn", $errors)){echo "</span>";}} ?>
</label><br />
<input type="text" name="namn" id="namn" class="text" tabindex="15" value="<?php if(!empty($errors)){ echo $namn; } ?>" />
<br />
<label for="foretag">Företag:</label><br />
<input type="text" name="foretag" id="foretag" class="text" tabindex="20" value="<?php if(!empty($errors)){ echo $foretag; }?>" />
<br />
<label for="adress">Adress:</label><br />
<input type="text" name="adress" id="adress" class="text" tabindex="30" value="<?php if(!empty($errors)){ echo $adress; } ?>" />
<br />
<label for="postnr">Postnummer:</label><br />
<input type="text" name="postnr" id="postnr" class="text_medium" tabindex="40" value="<?php if(!empty($errors)){ echo $postnr; } ?>" />
<br />
<label for="ort">Ort:</label><br />
<input type="text" name="ort" id="ort" class="text" tabindex="50" value="<?php if(!empty($errors)){ echo $ort; } ?>" />
<br />
<label for="telefon">
<?php if(!empty($errors)){if(in_array("telefon", $errors)){echo "<span class=\"field_error\">";}}?>Telefon: *<?php if(!empty($errors)){if(in_array("telefon", $errors)){echo "</span>";}} ?>
</label><br />
<input type="text" name="telefon" id="telefon" class="text" tabindex="60" value="<?php if(!empty($errors)){ echo $telefon; } ?>" />
<br />
<label for="epost">E-post:</label><br />
<input type="text" name="epost" id="epost" class="text" tabindex="70" value="<?php if(!empty($errors)){ echo $epost; } ?>" />
<br />
<label for="meddelande">
<?php if(!empty($errors)){if(in_array("meddelande", $errors)){echo "<span class=\"field_error\">";}}?>Meddelande: *<?php if(!empty($errors)){if(in_array("meddelande", $errors)){echo "</span>";}} ?>
</label><br />
<textarea name="meddelande" id="meddelande" class="textarea" tabindex="80"><?php if(!empty($errors)){ echo $meddelande; } ?></textarea>
<br />
<input type="submit" name="submit" value="Skicka" class="submit" />
</p>
</form>
could not find if anything wrong in your code.
check if APC cache is enabled in your server.
sometimes it creates problem in taking updated code.
if you are using SMTP server then mail() function may not work.
you can use PEARS for this.
I have experienced a similar problem. Turned out the browser, by error, sometimes double-posts your form; One time with content and another time without content.
If no content is submitted to your script, obviously there's nothing to act upon. There are a number of ways to check on this, but they way I discovered it was a desperate last resort measure where I sent emails to myself with each run of the script. And I showed that I often got two mails where the script had run only once.
It's worht trying :)
Problem solved. Changed to a new server host. No issues any more!

PHP email form is not sending all fields

here is my html form
<form action="formmail1.php" method="post" enctype="multipart/form-data">
<li>
Name<br /><input name="name" type="text" id="name" size="50" maxlength="50" /> <br />
First and Last Name<br /><br />
</li>
<li>
Phone<br /><input name="phone" Type="text" id="email" size="12" maxlength="12"/><br />
___-___-____<br /><br />
</li>
<li>
Email<br />
<input name="email" Type="text" id="email" size="50"/><br />
Valid email address<br /><br />
</li>
<li>
How do you want to be contacted?<br />
<input Type="radio" name="how_to_be_contacted" id="r1" class="radio" value="1" /><label for="r1">Email</label><br />
<input Type="radio" name="how_to_be_contacted" id="r2" class="radio" value="2" /><label for="r1">Phone</label><br /><br />
</li>
<li>
I would like information about...<br />
<input name="aquatic_therapy" type="checkbox" id="aquatic_therapy" />Aquatic Therapy <input name="occupational_therapy" type="checkbox" id="occupational_therapy" />Occupational Therapy<br />
<input name="speech_therapy" type="checkbox" id="speech_therapy" />Speech Therapy <input name="reflex_integration" type="checkbox" id="reflex_integration" />Reflex Integration
<br />
Select all that apply<br />
</li>
<br />
<li>
Message<br />
<textarea name="message" cols="50" rows="10" id="message"></textarea><br />
</li>
</ol>
<input id="submit" type="submit">
Here is my PHP
<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
//send email
$name = $_REQUEST['name'] ;
$phone = $_REQUEST['phone'] ;
$email = $_REQUEST['email'] ;
$how_do_you_want_to_be_contcted = $_REQUEST['how_do_you_want_to_be_contcted'] ;
$information = $_REQUEST['information'] ;
$message = $_REQUEST['message'] ;
mail("email#nowhere.com", "Subject: Contact Us Form",
$message "From: $email" );
echo "Thank you for using our mail form";
else
//if "email" is not filled out, display the form
echo "<form method='post' action='mailform.php'>
Email: <input name='email' type='text' /><br />
Subject: <input name='subject' type='text' /><br />
Message:<br />
<textarea name='message' rows='15' cols='40'>
</textarea><br />
<input type='submit' />
</form>";
All that is sending is message text field but none of the other fields....
$_REQUEST['how_do_you_want_to_be_contcted'] won't work because the field name in the HTML code is how_to_be_contacted.
You're defining the phone field as $phone and then referring to $emailphone. That clearly won't work either.
mail("eweb#gmail.com", "Subject: Contact Us Form", $emailphone
$message "From: $email" );
This has syntax errors, so can't work at all as shown.
You haven't escaped any of the input values, so if someone enters something with invalid data it could break the program and/or result in the site getting hacked.
Checkboxes and Radio buttons aren't sent unless checked.
Here is another possibility:
the request name you are asking for is: how_do_you_want_to_be_contcted
in your form you call it: how_to_be_contacted

Categories