Jump to content

Emails are being combined


SuiGenerisMN

Recommended Posts

Hey again, hopefully this is a bit less beginner than my previous issue but I will likely find out not.

 

I am writing a php page to send an email reminder to a list in a MySQL database. I want two separate email messages based on whether two of the columns in the database are yes/yes or the second email message if yes/no and no email message if yes/(yes or no). This is not the issue as I have the following in two separate parts of php.

 

This is appropriately selecting which 'users' to send emails to and which message.

 

Right now I am only testing the DB and don't have any real data loaded yet.

 

My issue is when I have 2+ users with the same set up combination (Yes/Yes) or (Yes/No) that the email message is 1) same message is being sent to both emails 2) the messages are being combined

 

Example

User 1 - email

Hi User 1,

Welcome to the web

From Me

Hi User 2,

Welcome to the web

From Me

 

User 2 - email

Hi User 1,

Welcome to the web

From Me

Hi User 2,

Welcome to the web

From Me

 

 

I can't figure out how to separate the messages to the different users and ensure the right information is sent to the right user.

<?php

$servername = "localhost";
$username = "";
$password = "";
$dbname = "";
$tblname = "";

        $conn = mysqli_connect($servername, $username, $password, $dbname) or die("Unable to Connect to '$dbhost' - 
Please email support@domain.com");

    $sql = "SELECT * FROM $tblname WHERE item1='No' AND item2='Yes'";
    $result = mysqli_query ($conn, $sql)
    or die ('Error querying database. 1');
    
   while($row = mysqli_fetch_array($result)){
        $first_name = $row['First'];
        $last_name = $row['Last'];
        $stuff1 = $row['Stuff1'];
        $stuff2 = $row['Stuff2'];
        $stuff3 = $row['Stuff3'];
        $email_to = $row['Email'];
   
    $email_from = "support@domain.com"; 
    $email_subject = "Subject 1";
    $email_message .= "Hello $first_name $last_name,\n";
    $email_message .= "\n";
    $email_message .= "Message\n";
    $email_message .= "\n";
    $email_message .= "Message\n";
    $email_message .= "\n";
    $email_message .= "Message\n";
    $email_message .= "\n";
    $email_message .= "Message\n";
    $email_message .= "Message\n";
    
// create email headers
mail($email_to, $email_subject, $email_message, 'From:' . $email_from);
echo 'Email sent to: ' . $email_to. '<br>';

 
}
mysqli_close($conn);

?>

<?php
$servername = "localhost";
$username = "";
$password = "";
$dbname = "";
$tblname = "";

        $conn = mysqli_connect($servername, $username, $password, $dbname) or die("Unable to Connect to '$dbhost' - Please email support@domain.com");

    $sql = "SELECT * FROM $tblname WHERE item1='Yes' AND item2='Yes'";
    $result = mysqli_query ($conn, $sql)
    or die ('Error querying database. 2');
   
    while($row = mysqli_fetch_array($result)){
        $first_name2 = $row['First'];
        $last_name2 = $row['Last'];
        $stuff12 = $row['Stuff1'];
        $stuff22 = $row['Stuff2'];
        $stuff32 = $row['Stuff3'];
        $email_to2 = $row['Email'];
     
    $email_from = "support@domain.com"; 
    $email_subject2 = "Subject 2";
    $email_message2 .= "Hello $first_name2 $last_name2,\n";
    $email_message2 .= "\n";
    $email_message2 .= "Message2\n";
    $email_message2 .= "\n";
    $email_message2 .= "Message2\n";
    $email_message2 .= "\n";
    $email_message2 .= "Message2\n";
    $email_message2 .= "\n";
    $email_message2 .= "Message2\n";
    $email_message2 .= "Message2\n";
    
// create email headers
mail($email_to2, $email_subject2, $email_message2, 'From:' . $email_from);
echo 'Email sent to: ' . $email_to2. '<br>';       
            
}
mysqli_close($conn);
?>
Link to comment
Share on other sites

Since you are doing two completely separate processes I fail to see how you can have any situation where 'the messages are combined'.  

 

Personally I think you took the wrong approach.  I would have built two separate messages to begin with.  Then I would do a complete query getting all the desired records that need an email of any kind.  Then in the loop I would then check the values that determine which email and send that email to that person.  

 

There is no reason to do a re-connect nor a close of your connection.  Plus you should avoid setting up the same info repeatedly in a loop.  Do it beforehand.

Link to comment
Share on other sites

ginerjm,

 

The two separate messages are not being combined, but rather the same message to multiple recipients. And the message is being placed in all emails of that set parameter.

 

Right now I have 4 records in the DB.

User 1 - Yes/Yes

User 2 - Yes/No

User 3 - No/Yes

User 4 - Yes/Yes

 

With the script, User 3 should not get a message; User 2 should get a different message than User 1 and User 4.

 

The issue is User 1 and User 4 should get the same message, but in their own email; what is happening is their individual messages are being combined into a single email and sent to both email addresses.

 

Both emails look the same such as:

Hello User 1,

Thanks for checking us out on X day; glad you chose product ABC to review.

Have a great day,

This company

Hello User 4,

Thanks for checking us out on Y day; glad you chose product TUV to review.

Have a great day,

This company

 

What I want is these emails to be separated; and sent only to the user's email that is in the greeting.

 

Hope that provides more clarification.

Link to comment
Share on other sites

you are getting messages combined because you are not initializing the message variable, which would be producing a php error the first time you reference it.

 

one of the biggest things you can do to get php to help you, is to set php's error_reporting to E_ALL and display_errors to ON (preferably in the php,ini on your development system), so that php will report and display all the errors it detects.

 

next, don't design database tables with columns like itemx, stuffx, nor write php code with numerical variable endings, or with code that isn't being used. use meaningful names for database columns and don't repeat code that's performing the same processing.

 

you should run one query that gets the data you want in the order that you want it. the only apparent difference between the two sets of code that you have now is the subject line. you should only write program logic for things that are different, not duplicate all the logic for each possible value. as you loop over the result set from the single query, just use conditional logic to determine which subject to use (and any other actual differences between the two messages.)

Link to comment
Share on other sites

mac_gyver,

 

Agree with the DB labeling, I modified the code for example but the actual labels are meaningful.

 

Duplicating the code is perhaps redundant, but it is separating the messages as desired. The issue, even if I remove the redundant coding, is that both messages for two recipients are being placed on the same email and being sent to both emails. The email would be even more ugly if I had 50 users with the same Yes/Yes or Yes/No combination.

 

But again, the Yes/Yes message and Yes/No message are not being combined, it is only the users with the same combination.

 

I just can't figure out, or find, how to say:

"Send email with only user 1 message to user 1"

stop

"Send email with only user 4 message  to user 4"

stop

Link to comment
Share on other sites

the first paragraph in my reply told you what's causing the problem. the second paragraph told you how to get php to help you find the cause of the problem and to find all kinds of other simple mistakes you are likely to make while learning, developing, and debugging code.

 

the reason no one told you what keys to press to make the problem go-a-way is that it is much better if you set up your development system to report and display all the errors php detects, so that you can find and fix all these simple problems in your code rather than expect others to do it for you.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.