Tom1791 Posted June 18, 2010 Share Posted June 18, 2010 Hello, I've been using the code at the bottom of this post on my contact form to validate and then send me an email with name, email address and the message. However my ISP has changed their policy and as a result the script no longer works. They say: "The From address on mail sent from the server must be set to a valid address under the domain the form is hosted on. We had to do this to stop people spamming through PHP forms under Windows unfortunately. In your script, you need to change the "From" section in the additional headers on your mail() command to be spmething @mydomain.com, and add the $values["name"] and $values["email"] into the message body" I have limited PHP knowledge (it took me about 3 days to work out the script below!!!) and have tried to do what has been requested but with no luck. If anyone can help before I end up pulling all of my hair out that would be much appreciated. <?php function VerifyForm(&$values, &$errors) { // Do all necessary form verification if (strlen($values['name']) == 0) $errors['name'] = 'Error'; if (!ereg('.*@.*\..{2,4}', $values['email'])) $errors['email'] = 'Error'; if (strlen($values['text']) == 0) $errors['text'] = 'Error'; return (count($errors) == 0); } function DisplayForm($values, $errors) { ?> <?php if (count($errors) > 0) echo "<div id='contactRight'><h2><span class='error'>Please enter your details in the orange fields.</span></h2> <p class='sml'>*all fields are required</p>"; else echo "<div id='contactRight'><h2>Please fill in the form.</h2><p class='sml'>*all fields are required</p>"; ?> <form action="<?= $_SERVER['PHP_SELF'] ?>" method="post" class="clearfix"> <fieldset> <input type="hidden" name="recipient" value="[email protected]" /> <input type="hidden" name="subject" value="Message from mydomain" /> <legend>Your details</legend> <label class="contact<?= $errors['name'] ?>" for="name">Your name</label><br /> <input type="text" name="name" id="name" size="30" value="<?= htmlentities($values['name']) ?>" class="formText contact<?= $errors['name'] ?>" tabindex="1" /><br /> <label class="contact<?= $errors['email'] ?>" for="email">Your email</label><br /> <input type="text" name="email" id="email" size="30" value="<?= htmlentities($values['email']) ?>" class="formText contact<?= $errors['email'] ?>" tabindex="2" /><br /> <label class="contact<?= $errors['text'] ?>" for="message">Your message</label><br /> <textarea cols="24" rows="8" name="text" id="message" class="formArea contact<?= $errors['text'] ?>" tabindex="3"><?= htmlentities($values['text']) ?></textarea> <input type="image" src="images/send.gif" alt="send" class="formButton" tabindex="4" /> </fieldset> </form> </div><!-- end contactRight --> <?php } function ProcessForm($values) { mail('[email protected]', 'Message from mydomain', $values['text'], "From: \"{$values['name']}\" <{$values['email']}>"); //mail('[email protected]', 'Message from mydomain', $values['name','text','email'], "From: \"[email protected]"); echo "<div id='contactRightDone'> <h2>Thanks for your message</h2> <p>I'll respond to your enquiry as soon as possible.</p> </div>"; } if ($_SERVER['REQUEST_METHOD'] == 'POST') { $formValues = $_POST; $formErrors = array(); if (!VerifyForm($formValues, $formErrors)) DisplayForm($formValues, $formErrors); else ProcessForm($formValues); } else DisplayForm(null, null); ?> Thanks Tom Quote Link to comment https://forums.phpfreaks.com/topic/205153-help-with-mail-script/ Share on other sites More sharing options...
mentalist Posted June 18, 2010 Share Posted June 18, 2010 You should be putting their info in the message body and your (the sites) return info in the headers... function ProcessForm($values) { mail('[email protected]', "My Subject", "Message from mydomain\n".$values['name']."\n".$values['email']."\n", "From: [email protected]\r\n"."Reply-To: [email protected]\r\n" ); ... Quote Link to comment https://forums.phpfreaks.com/topic/205153-help-with-mail-script/#findComment-1073842 Share on other sites More sharing options...
Tom1791 Posted June 18, 2010 Author Share Posted June 18, 2010 Thanks for that - it works! However, when I add the "text" value so that the message is included in the email I don't get the email coming through. Is this how it should be written? function ProcessForm($values){ mail('[email protected]', "My Subject", "Message from mydomain\n".$values['name']."\n".$values['email']."\n".$values['text']."\n", "From: [email protected]\r\n"."Reply-To: [email protected]\r\n" ); ... I've added in: $values['text']."\n" Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/205153-help-with-mail-script/#findComment-1073852 Share on other sites More sharing options...
mentalist Posted June 18, 2010 Share Posted June 18, 2010 The problem there will depend on what your sending through... Referring to: mail Each line should be separated with a LF (\n). Lines should not be larger than 70 characters. Caution (Windows only) When PHP is talking to a SMTP server directly, if a full stop is found on the start of a line, it is removed. To counter-act this, replace these occurrences with a double dot. <?php $text = str_replace("\n.", "\n..", $text); ?> Quote Link to comment https://forums.phpfreaks.com/topic/205153-help-with-mail-script/#findComment-1073857 Share on other sites More sharing options...
PFMaBiSmAd Posted June 18, 2010 Share Posted June 18, 2010 Actually, the Reply-To: email address CAN be the address that the visitor supplied. You should form the message body in a variable (i.e. $message) so that you can echo it to see what exactly it is. You then simply put the $message variable into the mail() function call as the 3rd parameter. I also recommend testing the value that mail() returns so that you can see if your mail server accepted the email or returned an error - if(mail(your parameters here...)){ // the mail was accepted by the server for sending echo "<div id='contactRightDone'> <h2>Thanks for your message</h2> <p>I'll respond to your inquiry as soon as possible.</p> </div>"; } else { // the mail was not accepted by the server for sending echo "Sorry, a server error occurred and your form submission could not be processed... "; } If you get the Sorry... message, you can troubleshoot further (by turning on full php error_reporting/display_errors) why the mail server is not accepting the email. Quote Link to comment https://forums.phpfreaks.com/topic/205153-help-with-mail-script/#findComment-1073859 Share on other sites More sharing options...
Tom1791 Posted June 18, 2010 Author Share Posted June 18, 2010 It's all working now, many thanks Mentalist. Quote Link to comment https://forums.phpfreaks.com/topic/205153-help-with-mail-script/#findComment-1073875 Share on other sites More sharing options...
Tom1791 Posted June 18, 2010 Author Share Posted June 18, 2010 And thank you too PFMaBiSmAd! Quote Link to comment https://forums.phpfreaks.com/topic/205153-help-with-mail-script/#findComment-1073876 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.