Jump to content

Problems with Email Code: Email is Being Sent 2 Times...


Recommended Posts

Hello Everyone,

 

I'm brand new to PHP, and have been doing a lot of searching, but just can't seem to figure out what I'm doing wrong.  I found the code below online and edited it to fit my needs, which is to send 2 emails after a new user has created an account on our site.

 

- One email goes to the new user and the 2nd one goes to the site admin. Now, the emails are successfully being sent, however the Admin receives 2 emails simultaneously.  The first is correct, while the 2nd one is missing any database driven information. 

 

Lastly, when the new user form is submitted, the following string shows along the bottom of the "Thank You" page (where the email code is stored): "user email couldn't be sentadmin email couldn't be sentuser email sentadmin email couldn't be sent"

 

If anyone can please tell me what I'm doing wrong, and how to eliminate the 2nd Admin Email from being sent, as well as that string of text, it would be appreciated!  Or if I'm going about this the complete wrong way, I'm open to any suggestions on how to clean up this code.

 

Thanks in advance!

dave.

- - - - - - - - - - - - - - - - - - - - - - - -

 

  <?php

//PULL DATABASE COMMANDS

  $f_name = $_REQUEST['f_name'];

  $l_name = $_REQUEST['l_name'];

  $email = $_REQUEST['email'] ;

 

  //HEADERS FOR BOTH EMAILS

function send_email($from, $to, $subject, $message){

$headers = "From: ".$from."\r\n";

$headers .= "Reply-To: ".$from."\r\n";

$headers .= "Return-Path: ".$from."\r\n";

$headers .= "Content-type: text/html\r\n";

 

  //SETUP USER EMAIL COMMANDS

if (mail($to,$subject,$message,$headers) ) {

  echo "user email sent";

} else {

  echo "user email couldn't be sent";

}

 

  //SETUP ADMIN EMAIL COMMANDS

if (mail($admin_to,$admin_subject,$admin_message,$headers) ) {

  echo "admin email sent";

} else {

  echo "admin email couldn't be sent";

}

}

  //USER EMAIL

$subject = "Your Account Has Been Created";

$message = "

<html>

<head>

<title> </title>

</head>

<body>

<p>Thank you $f_name, </br></br>

Body of email here.</p>

</body>

</html>

";

send_email("[email protected]", "$email",

$subject ,

$message);

 

 

  //ADMIN EMAIL

$admin_subject = "A New Account Has Been Created";

$admin_message = "

<html>

<head>

<title> </title>

</head>

<body>

<p>Body of email here.</p>

<table>

<tr>

<th>Name:</th>

<td>$f_name $l_name</td>

</tr>

</table>

</body>

</html>

";

send_email("[email protected]", "admin's email address",

$admin_subject ,

$admin_message);

?>

 

In your function:

function send_email($from, $to, $subject, $message){
   $headers = "From: ".$from."\r\n";
   $headers .= "Reply-To: ".$from."\r\n";
   $headers .= "Return-Path: ".$from."\r\n";
   $headers .= "Content-type: text/html\r\n";
   
  //SETUP USER EMAIL COMMANDS
   if (mail($to,$subject,$message,$headers) ) {
      echo "user email sent";
   } else {
      echo "user email couldn't be sent";
   }
   
  //SETUP ADMIN EMAIL COMMANDS
   if (mail($admin_to,$admin_subject,$admin_message,$headers) ) {
      echo "admin email sent";
   } else {
      echo "admin email couldn't be sent";
   }
}

you are sending two emails -- one to user and one to admin

 

Then in your code you are calling this function twice.  So you are sending, or trying to send, 4 emails.

 

The second send inside your function (the one for the admin) is referencing variables that are not defined within the scope of the function.  You should be getting an error message and the mail() function should fail.

 

I think if you take this second mail section out of the function (and continue to call the function twice as you do now), it should work.  The messages at the end of the 'thank you' page are coming from the echo() statements after the mail() function calls.  You should remove these if you don't want them or reword them appropriately.

 

You probably have syntax errors but you are not seeing them.  Add the following two lines of code to the top of every PHP script (especially during development) so you can see the errors:

error_reporting(E_ALL);
ini_set('display_errors', 1);

Thanks for getting back to me David. You were right, that snippet of code revealed a ton of errors that I was getting but unaware of. 

 

Now, I believe I followed your instructions correctly, and I tried a couple different variations of editing the code but I'm still receiving 2 emails at the admin address. Can you please take another look and see if I'm on the right track... 

 

 

<?php   
error_reporting(E_ALL);
ini_set('display_errors', 1);

//PULL DATABASE COMMANDS
  $f_name = $_REQUEST['f_name'];
  $l_name = $_REQUEST['l_name'];
  $email = $_REQUEST['email'] ;
  
  //HEADERS FOR BOTH EMAILS
function send_email($from, $to, $subject, $message){
$headers = "From: ".$from."\r\n";
$headers .= "Reply-To: ".$from."\r\n";
$headers .= "Return-Path: ".$from."\r\n";
$headers .= "Content-type: text/html\r\n"; 

  //SETUP USER EMAIL COMMANDS
if (mail($to,$subject,$message,$headers) ) {
   echo "user email sent";
} else {
   echo "user email couldn't be sent";
}
}

  //USER EMAIL
$subject = "Subject";
$message = "
<html>
<head>
<title> </title>
</head>
<body>
<p>Thank you $f_name, </br></br>
body</p>
</body>
</html>
";
send_email("[email protected]", "$email", 
$subject , 
$message);


  //ADMIN EMAIL	
$subject = "subject";
$message = "
<html>
<head>
<title> </title>
</head>
<body>
<p>body</p>
<table>
<tr>
<th>Name:</th>
<td>$f_name $l_name</td>
</tr>
</table>
</body>
</html>
";
send_email("[email protected]", "admin's address", 
$subject , 
$message);
?>

Hmm, I can't see any reason for that.  What is the second email?  Are you getting the admin email twice or one of each?  After each send, you are echoing "user email sent".  How many times do you see that on the screen?  Maybe you just flooded the mail server and it hasn't caught up?  Comment out one of the calls to send_email and see if you get the correct result, then comment out the other (and uncomment the first) and see what happens. 

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.