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 = "info@site.org";
    $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";
}
}
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
user3063919
  • 91
  • 3
  • 11
  • You called `mail()` twice in 1 `while` loop cycle. – Raptor Jan 02 '14 at 03: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. – Pekka Jan 02 '14 at 03: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 http://stackoverflow.com/questions/4913817/catching-php-mail-errors-and-showing-reasonable-user-error-message) – thicolares Jan 02 '14 at 03:31
  • thanks for pointing that out @ShivanRaptor, but there's still the same error :/ – user3063919 Jan 02 '14 at 03:39
  • @colares i tried what you suggested. but instead of error, i get ArrayArrayArray – user3063919 Jan 02 '14 at 03:42
  • 1
    @user3063919 try `$error = error_get_last(); print_r($error);` – thicolares Jan 02 '14 at 03:44
  • @user3063919 read my answer below. – Raptor Jan 02 '14 at 03:47
  • interesting... I'm getting Array([type] =>8 Unidentified index: Company_ID[file] => /././.check_login_status.php [line]=> 26) – user3063919 Jan 02 '14 at 03:50
  • check_login_status.php is a session checking function. – user3063919 Jan 02 '14 at 03:50
  • i had set a session in my file, that i did not need.... the table doesnt even have that column.... but now, i get a blank screen... no errors – user3063919 Jan 02 '14 at 04:05

3 Answers3

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 = "info@mysite.com";
        $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";
    }
user3063919
  • 91
  • 3
  • 11
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";
}
Goodies
  • 4,439
  • 3
  • 31
  • 57
  • I'm not sure what you mean by that. – Goodies Jan 02 '14 at 03:26
  • 1
    The problem is deeper than the duplicated `mail()` call, as those don't seem to be working at all – Pekka Jan 02 '14 at 03:27
  • 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. – Goodies Jan 02 '14 at 03:29
  • yes i agree with @Pekka웃 very strange.. prob its just lying right in front of me :/ what could it be – user3063919 Jan 02 '14 at 03:36
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 = "info@hesnet.org";
$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.

Raptor
  • 53,206
  • 45
  • 230
  • 366