martingreenwood Posted November 6, 2009 Share Posted November 6, 2009 hey I need help with my mail script when the form on http://www.mcgdesignstudio.com/contact.html is filled out, it send the from with no errors.. however i never receive it.. my hosting company have told me as long as i have the fourth element in if -f or something it will work, dunno where to put that though.. this is my script: <?php if(!$_POST) exit; $email = $_POST['email']; //$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS'; if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){ $error.="Invalid email address entered"; $errors=1; } if($errors==1) echo $error; else{ $values = array ('name','email','message'); $required = array('name','email','message'); $your_email = "martin@mcgdesignstudio.com"; $email_subject = "New Message: ".$_POST['subject']; $email_content = "new message:\n"; foreach($values as $key => $value){ if(in_array($value,$required)){ if ($key != 'subject' && $key != 'company') { if( empty($_POST[$value]) ) { echo 'You need to fill in all the required fields...'; exit; } } $email_content .= $value.': '.$_POST[$value]."\n"; } } if(mail("$your_email", "$email_subject", "$email_content", "To: Martin Greenwood <$your_email >\n" . "From: mcgdesignstudio.com <martin@mcgdesignstudio.com>\n" . "MIME-Version: 1.0\n" . "Content-type: text/html; charset=iso-8859-1" )){ echo 'Your message has been sent successfully..'; } else { echo 'Oops, something went wrong...try again later.'; } } ?> Quote Link to comment Share on other sites More sharing options...
Bricktop Posted November 6, 2009 Share Posted November 6, 2009 Hi martingreenwood, Chnage your script to the below: <?php if(!$_POST) exit; $email = $_POST['email']; //$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS'; if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){ $error.="Invalid email address entered"; $errors=1; } if($errors==1) echo $error; else{ $values = array ('name','email','message'); $required = array('name','email','message'); $to_email = "martin@mcgdesignstudio.com"; $from_email = "From: mcgdesignstudio.com <martin@mcgdesignstudio.com>"; $email_subject = "New Message: ".$_POST['subject']; $email_content = "new message:\n"; foreach($values as $key => $value){ if(in_array($value,$required)){ if ($key != 'subject' && $key != 'company') { if( empty($_POST[$value]) ) { echo 'You need to fill in all the required fields...'; exit; } } $email_content .= $value.': '.$_POST[$value]."\n"; } } if(mail($to_email, $email_subject, $email_content, $from_email)) { echo 'Your message has been sent successfully..'; } else { echo 'Oops, something went wrong...try again later.'; } } ?> Changes made: 1. Changed the variable name of the "To" address to $to_email. 2. Added a new $from_email variable and added the relevant "From" information 3. Removed the extra header information 4. Rewritten the mail() command Give the above a try and report back with the result. Hope this helps. Quote Link to comment Share on other sites More sharing options...
martingreenwood Posted November 6, 2009 Author Share Posted November 6, 2009 hey, cheers for that, it sent the mail... but again I didn't receive it... I just ran this script <?php $from = 'martin@mcgdesignstudio.com'; $to = 'martin@mcgdesignstudio.com'; if ( mail("$to", "Test mail from phpmail","This is a test PHP Email", "From: $from", "-f$from" )) { print "Mail sent"; } else { print "failed"; } ?> And I got an email instantly... Quote Link to comment Share on other sites More sharing options...
Bricktop Posted November 6, 2009 Share Posted November 6, 2009 OK, add the -f to your script as below: <?php if(!$_POST) exit; $email = $_POST['email']; //$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS'; if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){ $error.="Invalid email address entered"; $errors=1; } if($errors==1) echo $error; else{ $values = array ('name','email','message'); $required = array('name','email','message'); $to_email = "martin@mcgdesignstudio.com"; $from_email = "From: mcgdesignstudio.com <martin@mcgdesignstudio.com>"; $email_subject = "New Message: ".$_POST['subject']; $email_content = "new message:\n"; foreach($values as $key => $value){ if(in_array($value,$required)){ if ($key != 'subject' && $key != 'company') { if( empty($_POST[$value]) ) { echo 'You need to fill in all the required fields...'; exit; } } $email_content .= $value.': '.$_POST[$value]."\n"; } } if(mail($to_email, $email_subject, $email_content, "-f$from_email")) { echo 'Your message has been sent successfully..'; } else { echo 'Oops, something went wrong...try again later.'; } } ?> Quote Link to comment Share on other sites More sharing options...
martingreenwood Posted November 6, 2009 Author Share Posted November 6, 2009 no luck.. still not receiving.. Quote Link to comment Share on other sites More sharing options...
huszi001 Posted November 6, 2009 Share Posted November 6, 2009 Hi. Try this. $to_email = "martin@mcgdesignstudio.com"; $from_email = 'MIME-Version: 1.0' . "\r\n"; $from_email .= 'Content-type: text/plain; charset=UTF-8' . "\r\n"; $from_email .= 'From: mcgdesignstudio.com <martin@mcgdesignstudio.com>' . "\r\n"; $email_subject = "New Message: ".$_POST['subject']; $email_content = "new message:\n"; foreach($values as $key => $value){ if(in_array($value,$required)){ if ($key != 'subject' && $key != 'company') { if( empty($_POST[$value]) ) { echo 'You need to fill in all the required fields...'; exit; } } $email_content .= $value.': '.$_POST[$value]."\n"; } } if(mail($to_email, $email_subject, $email_content, $from_email)) { echo 'Your message has been sent successfully..'; } else { echo 'Oops, something went wrong...try again later.'; } } or this if you don't want to send headers $to_email = "martin@mcgdesignstudio.com"; $from_email = "martin@mcgdesignstudio.com"; $email_subject = "New Message: ".$_POST['subject']; $email_content = "new message:\n"; foreach($values as $key => $value){ if(in_array($value,$required)){ if ($key != 'subject' && $key != 'company') { if( empty($_POST[$value]) ) { echo 'You need to fill in all the required fields...'; exit; } } $email_content .= $value.': '.$_POST[$value]."\n"; } } if(mail($to_email, $email_subject, $email_content, null, $from_email)) { echo 'Your message has been sent successfully..'; } else { echo 'Oops, something went wrong...try again later.'; } } Quote Link to comment Share on other sites More sharing options...
martingreenwood Posted November 6, 2009 Author Share Posted November 6, 2009 nope. didn't work... I think i might just have to find another script.. Quote Link to comment Share on other sites More sharing options...
dgoosens Posted November 6, 2009 Share Posted November 6, 2009 hi, I might be wrong, but it seems to me that you are missing an argument here if(mail($to_email, $email_subject, $email_content, "-f$from_email")) Could you try like this: if(mail($to_email, $email_subject, $email_content, $from_email, "-fmartin@mcgdesignstudio.com")) Quote Link to comment Share on other sites More sharing options...
martingreenwood Posted November 6, 2009 Author Share Posted November 6, 2009 nope that didn't work either.... Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted November 6, 2009 Share Posted November 6, 2009 mail() <?php #boundary 1; $boundary1 = rand(0,9)."-" .rand(10000000000,9999999999)."-" .rand(10000000000,9999999999)."=:" .rand(10000,99999); #to; $user = 'martin@mcgdesignstudio.com'; #from email; $from_email = 'martin@mcgdesignstudio.com'; #from; $from = 'Wayne Gretzky <'.$from_email.'>'; #headers; $headers = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/html; boundary='.$boundary1. "\r\n" . 'Content-Transfer-Encoding: 8bit' . "\r\n" . 'From: ' . $from . "\r\n" . 'Reply-To: ' . $from_email . "\r\n"; #subject; $subject = 'New Message: ' . $_POST['subject']; #message; $message = 'Thank you for contacting us.<br /><br />' . "\r\n"; $message .= 'Please allow 24 hours for us to get back to you.' . "\r\n"; $mail = (mail ($user, $subject, $message, $headers, "-f {$from_email}") ? true : false); if ($mail) { echo 'Sent!'; } else { echo 'Failed!'; } ?> that code will email somebody. so, if it doesn't work, there's something wrong with the mail server, or there is something you are leaving out. Quote Link to comment Share on other sites More sharing options...
martingreenwood Posted November 6, 2009 Author Share Posted November 6, 2009 Right, that worked.. I got an email... now how do i get the content of the form.... Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted November 6, 2009 Share Posted November 6, 2009 try replacing the existing: $message = ...; $message .= ...; with: $message = wordwrap (nl2br (strip_tags ($_POST['message'])), 70, '<br />'); just a start for you. Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted November 6, 2009 Share Posted November 6, 2009 another note dealing with email is security. a lot of malicious activity can take place via a mail script, so beefing up security is near as important as when doing security for SQL injections, XSS attacks, etc. an example of mail() code manipulation would be when an attacker adds aditional To:, Bcc:, Cc: addresses, and ultimately solicits thousands of emails using your form(s), server(s), routing, etc. this can get you blackballed. as well, tracking darts can also be placed with mail scripts to gather information from users, as well as malicious iframes can also be embedded within email body's set to execute code upon opening of email. take all the necessary precautions when using email as you would with any other security. Quote Link to comment Share on other sites More sharing options...
martingreenwood Posted November 6, 2009 Author Share Posted November 6, 2009 thanks for you help.. ill try and edit it now and get it to send hat i need Quote Link to comment 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.