1

I am trying to send multiple emails using php. But everytime I try to send the email I'm getting “errorerrorerror”—one “error” for each email—that's in the table. Here's the code

$emailsql = "SELECT Username FROM Companyuserinfo WHERE Company_ID = '$cid'";
$emailquery = mysqli_query($connection, $emailsql);


while($emailrow = mysqli_fetch_array ($emailquery)){  
 $Usernamesend = $emailrow['Username'];

$sendsql = "SELECT * FROM users WHERE username = '$Usernamesend'";
$sendquery = mysqli_query($connection, $sendsql);

$sendrow = mysqli_fetch_array ($sendquery);
    $emailtosend = $sendrow['email'];

    $to="$emailtosend";
    $from = "[email protected]";
    $subject="TEST!";
    $message="HEY MY BROTHER!! I AM TESTING TdfdHIS BABY! WOOHOO!";
    $headers = "From: $from\n";
        $headers .= "MIME-Version: 1.0\n";
        $headers .= "Content-type: text/html; charset=iso-8859-1\n";
     mail($to, $subject, $message, $headers);

     if (!mail($to, $subject, $message, $headers)){
     echo "error";
     }
     else{
         echo "Form submitted successfully! Press back $emailtosend";
}
}
10
  • You called mail() twice in 1 while loop cycle. Commented Jan 2, 2014 at 3:21
  • 1
    mail() should be telling you what's wrong. Make sure error reporting is on. Also hesnet.org needs to be running on the server you're sending this from. Commented Jan 2, 2014 at 3:22
  • You can also save the mail() result into a tmp var, like $result = mail(), then check this instead of calling mail() twice, as Shivan pointed out. Also, error_get_last() may help you on finding the error (as Mario answered at stackoverflow.com/questions/4913817/…) Commented Jan 2, 2014 at 3:31
  • thanks for pointing that out @ShivanRaptor, but there's still the same error :/ Commented Jan 2, 2014 at 3:39
  • 1
    @user3063919 try $error = error_get_last(); print_r($error); Commented Jan 2, 2014 at 3:44

3 Answers 3

6

I got it to work....replaced my previous script with this;

$emailsql = "SELECT * FROM Companyuserinfo WHERE Company_ID = '$cid'";
$email_query = mysqli_query($connection, $emailsql);
while($emailrow = mysqli_fetch_array ($email_query)){
 $Usernamesend = $emailrow['Username'];
$company_name = $emailrow['Company_Name'];
$sendsql = "SELECT * FROM users WHERE username = '$Usernamesend'";
$send_query = mysqli_query($connection, $sendsql);
$mail_body = '';
$sendrow = '';
$sendrow = mysqli_fetch_array($send_query);
    $email = $sendrow["email"];
    $name = $sendrow["first_name"];

    $to = "$email";                          
        $from = "[email protected]";
        $subject = "TESTINGG";
        $message = 'TEST!!';
        $headers = "From: $from\n";
        $headers .= "MIME-Version: 1.0\n";
        $headers .= "Content-type: text/html; charset=iso-8859-1\n";
        $headers .= "X-Priority: 1 (Highest)\n";
        $headers .= "X-MSMail-Priority: High\n";
        $headers .= "Importance: High\n";
        $mail_result = mail($to, $subject, $message, $headers);
}       
        if ($mail_result) {
        echo "Submitted Successfully! Press close";
    } else {
       echo "There was an error submitting... Press close";
    }
Sign up to request clarification or add additional context in comments.

Comments

0
mail($to, $subject, $message, $headers);
if (!mail($to, $subject, $message, $headers)){
   echo "error";
}

Inside the if statement, it is running mail() function. Remove the former function and JUST keep it inside of the if statement.

Try:

//mail($to, $subject, $message, $headers);
if (!mail($to, $subject, $message, $headers)){
   echo "error";
}

4 Comments

I'm not sure what you mean by that.
The problem is deeper than the duplicated mail() call, as those don't seem to be working at all
OH! Wow! That's embarrassing. The logical error seems to be fixed, but the mail function itself is not. I remember fiddling with the Apache folder contents before I got the mail() to work properly.
yes i agree with @Pekka웃 very strange.. prob its just lying right in front of me :/ what could it be
0

Your code has logical errors, and here is a suggested fix.

$error_cnt = 0;
$emailsql = "SELECT Username FROM Companyuserinfo WHERE Company_ID = '$cid'";
$emailquery = mysqli_query($connection, $emailsql);
$from = "[email protected]";
$subject="TEST!";
$message="HEY MY BROTHER!! I AM TESTING TdfdHIS BABY! WOOHOO!";
$headers = "From: $from\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
while($emailrow = mysqli_fetch_array ($emailquery)){  
  $Usernamesend = $emailrow['Username'];
  $sendsql = "SELECT * FROM users WHERE username = '$Usernamesend'";
  $sendquery = mysqli_query($connection, $sendsql);
  $sendrow = mysqli_fetch_array ($sendquery);
  if (!mail($sendrow['email'], $subject, $message, $headers)){
    $error_cnt++;
    break; // (optional) stop the loop when error is encountered.
  }
}
if($error_cnt === 0) {
  echo "Form submitted successfully! Press back $emailtosend";
} else {
  echo $error_cnt . ' error(s) occurred.';
}

Side Note: If your SMTP server needs authentication, mail() does not support it. Try to use PHPMailer or SwiftMailer for authentication and / or attachment support.

Also, noticed that $emailsql uses single quote for the Company_ID field. If it is an integer, single quotes are not required.

Next, consider to combine 2 SQL statements into one by joining the table ( if possible ).

Last, slightly increase the performance by having variable stated outside while loop to avoid re-creation of variable of same content.

3 Comments

thanks for the response, i tried this though, but to no avail :/
okay... did a little digging up, and then tried your method again... now im getting (1) error occured @ShivanRaptor
Then 1 email cannot be sent. Others are sent out successfully. Maybe email address is incorrect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.