Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/240624-contact-form-duplicated-emails/
Share on other sites

@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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.