soltek Posted June 28, 2011 Share Posted June 28, 2011 Hey guys, here I am looking for your awesome advices. I have this code to send me emails from my website into my email account, but for some reason it send two emails instead of one. php <?php //If the form is submitted if(isset($_POST['submitted'])) { //Check to make sure that the name field is not empty if(trim($_POST['contactName']) === '') { $nameError = 'You forgot to enter your name.'; $hasError = true; } else { $name = trim($_POST['contactName']); } //Check to make sure sure that a valid email address is submitted if(trim($_POST['email']) === '') { $emailError = 'You forgot to enter your email address.'; $hasError = true; } else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) { $emailError = 'You entered an invalid email address.'; $hasError = true; } else { $email = trim($_POST['email']); } //Check to make sure comments were entered if(trim($_POST['comments']) === '') { $commentError = 'You forgot to enter your comments.'; $hasError = true; } else { if(function_exists('stripslashes')) { $comments = stripslashes(trim($_POST['comments'])); } else { $comments = trim($_POST['comments']); } } //If there is no error, send the email if(!isset($hasError)) { $emailTo = myemail@gmail.com'; $subject = 'Contact Form Submission from '.$name; $sendCopy = trim($_POST['sendCopy']); $body = "Name: $name \n\nEmail: $email \n\nComments: $comments"; $headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email; mail($emailTo, $subject, $body, $headers); if($sendCopy == true) { $subject = 'You emailed Your Name'; $headers = 'From: Your Name <noreply@somedomain.com>'; mail($email, $subject, $body, $headers); } $emailSent = true; } } ?> HTML: <form action="<?php the_permalink(); ?>" id="contactForm" method="post" style="width:300px;float:left;"> <ol class="forms"> <li><label for="contactName">Name</label> <input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" class="requiredField" /> <?php if($nameError != '') { ?> <span class="error"><?=$nameError;?></span> <?php } ?> </li> <li><label for="email">Email</label> <input type="text" name="email" id="email" value="<?php if(isset($_POST['email'])) echo $_POST['email'];?>" class="requiredField email" /> <?php if($emailError != '') { ?> <span class="error"><?=$emailError;?></span> <?php } ?> </li> <li class="textarea"><label for="commentsText">Comments</label> <textarea name="comments" id="commentsText" rows="20" cols="30" class="requiredField"><?php if(isset($_POST['comments'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['comments']); } else { echo $_POST['comments']; } } ?></textarea> <?php if($commentError != '') { ?> <span class="error"><?=$commentError;?></span> <?php } ?> </li> <li class="inline"><input type="checkbox" name="sendCopy" id="sendCopy" value="true"<?php if(isset($_POST['sendCopy']) && $_POST['sendCopy'] == true) echo ' checked="checked"'; ?> /><label for="sendCopy">Send a copy of this email to yourself</label></li> <li class="buttons"><input type="hidden" name="submitted" id="submitted" value="true" /><button type="submit">Email me »</button></li> </ol> </form> I'd be really thankful if you could give me a hand, Thanks Quote Link to comment https://forums.phpfreaks.com/topic/240624-contact-form-duplicated-emails/ Share on other sites More sharing options...
TeNDoLLA Posted June 28, 2011 Share Posted June 28, 2011 Forget what I said.. didn't read it well enough. Quote Link to comment https://forums.phpfreaks.com/topic/240624-contact-form-duplicated-emails/#findComment-1235933 Share on other sites More sharing options...
Maq Posted June 28, 2011 Share Posted June 28, 2011 If you're filling out the email input field with the same email from this line it will send twice: $emailTo = myemail@gmail.com'; Quote Link to comment https://forums.phpfreaks.com/topic/240624-contact-form-duplicated-emails/#findComment-1235935 Share on other sites More sharing options...
WebStyles Posted June 28, 2011 Share Posted June 28, 2011 @TeNDoLLA: No, that's not it, they're sent to different email addresses. I still haven't figured it out though... I know this sounds really stupid, but just to be sure: Maybe you're using your email to test, so you get the copy too??? EDIT: Yep, Basically the same as Maq said. Quote Link to comment https://forums.phpfreaks.com/topic/240624-contact-form-duplicated-emails/#findComment-1235936 Share on other sites More sharing options...
TeNDoLLA Posted June 28, 2011 Share Posted June 28, 2011 My best bet is to just make a plain EMPTY php file and try the most simple send mail() in there. See if that comes also twice or not. Also it could be mail server settings that is causing it. Quote Link to comment https://forums.phpfreaks.com/topic/240624-contact-form-duplicated-emails/#findComment-1235943 Share on other sites More sharing options...
EdwinPaul Posted June 28, 2011 Share Posted June 28, 2011 If you look at the color, you can see there is a single quote too many: <?php . . $emailTo = myemail@gmail.com'; $subject = 'Contact Form Submission from '.$name; . . ?> Quote Link to comment https://forums.phpfreaks.com/topic/240624-contact-form-duplicated-emails/#findComment-1235958 Share on other sites More sharing options...
TeNDoLLA Posted June 28, 2011 Share Posted June 28, 2011 Actually it is missing one before the email address. But I think that must be a typo, or OP doesn't have error reporting on. Because it should give a parse error. Quote Link to comment https://forums.phpfreaks.com/topic/240624-contact-form-duplicated-emails/#findComment-1235961 Share on other sites More sharing options...
Maq Posted June 28, 2011 Share Posted June 28, 2011 It's probably because he doesn't want to give his personal email out so he manually replaced it and didn't know he removed the single quote. Quote Link to comment https://forums.phpfreaks.com/topic/240624-contact-form-duplicated-emails/#findComment-1235962 Share on other sites More sharing options...
EdwinPaul Posted June 28, 2011 Share Posted June 28, 2011 Personally, I think this is funny: <?php . . $sendCopy = trim($_POST['sendCopy']); . . ?> What is there to trim? Quote Link to comment https://forums.phpfreaks.com/topic/240624-contact-form-duplicated-emails/#findComment-1235972 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.