1

I'm using PHPMailer.

I record user e-mails at users table in email column. Also I have category column in my table. I want to send e-mail to these users. For example, I want to send e-mail to users who has tech value in category column. I can do this but when I send, PHPMailer send my e-mail like

first user -> first column email
second user -> first column email
               second column email
third user ->  first column email
               second column email
               third column email

I just want to send first user -> first column email, second user -> second column email.

I have these codes right now.

<?php
require_once("class.phpmailer.php");

if($_POST['message']){

    $mail = new PHPMailer(); 
    $mail->IsSMTP(); 
    $mail->IsHTML(true);
    $mail->Host = "host.name.net"; 
    $mail->Port = 587; 
    $mail->SMTPAuth = true; 
    $mail->CharSet  ="utf-8";
    $mail->Username = "mail@mymail.com"; 
    $mail->Password = "pass"; 
    $mail->SetFrom("hi@mymail.com", "my mail name");
    $categorychoose = $_POST['categorychoose'];    

    $query = (" SELECT email FROM users WHERE category LIKE '$categorychoose' ");

    $result = mysql_query($query);
    while( $data = mysql_fetch_assoc($result) )
    {
        $mail->AddAddress($data["email"]);
        $mail->Subject = $_POST['subject']; 
        $mail->Body = $_POST['message'];

        if(!$mail->Send())
        {
            echo "<h4>not send</h4>";
        } else {
            echo "<h4>send</h4>";
        }
    }
}

?>
ArK
  • 20,698
  • 67
  • 109
  • 136
  • That can't be all your code? – Naruto Oct 04 '16 at 09:25
  • Also I have form codes and some phpmailer setting codes, but I don't have problem with them. –  Oct 04 '16 at 09:28
  • 2
    Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Oct 04 '16 at 09:29
  • Your script is at risk of [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly Oct 04 '16 at 09:29
  • @RiggsFolly Little Bobby Tables is fine, AFAIK - it's just his school's database that has been compromised. – low_rents Oct 04 '16 at 09:32
  • I dont think any of us understand what you mean by `first user -> first column email, second user -> first column email, second column email` The code you show us wont do anything like that – RiggsFolly Oct 04 '16 at 09:33
  • Let me explain. PHPMailer send e-mail to first e-mail like -> example@mail.com. Then PHPMailer send e-mail to second e-mail like -> example@mail.com, example2@mail.com. –  Oct 04 '16 at 09:35
  • Then you have some other loop in your code. I see an unmatches `}` at the bottom of your code, what is that loop doing? – RiggsFolly Oct 04 '16 at 09:37
  • I added all of my code, you can see up there. @RiggsFolly –  Oct 04 '16 at 09:40
  • Are you sending something in `$_POST['categorychoose']` that will return more than one category? I would suggest using `WHERE category = '$categorychoose'` to see if that fixes the issue – RiggsFolly Oct 04 '16 at 09:42
  • Nope, I think It's not problem. My main problem is, When I clicked send button from my Form, PHPMailer send all of my users who has in " tech " category for example. But if there are 3 users in tech category, first user gets 3 e-mail. His first e-mail include just his e-mail at from adress. His second e-mail include his e-mail and second user's e-mail at from adress. And his third e-mail include his e-mail, second user's e-mail and third user's e-mail. –  Oct 04 '16 at 09:47

1 Answers1

0

I edited my previous code from your updated code.

require_once("class.phpmailer.php");

if($_POST['message']){
    $mail = new PHPMailer(); 
    $mail->IsSMTP(); 
    $mail->IsHTML(true);
    $mail->Host = "host.name.net"; 
    $mail->Port = 587; 
    $mail->SMTPAuth = true; 
    $mail->CharSet  ="utf-8";
    $mail->Username = "mail@mymail.com"; 
    $mail->Password = "pass"; 
    $mail->SetFrom("hi@mymail.com", "my mail name");
    $categorychoose = $_POST['categorychoose'];

    $query = (" SELECT email FROM users WHERE category LIKE '$categorychoose' ");

    $mail->Subject = $_POST['subject'];
    $mail->Body = $_POST['message'];

    $result = mysql_query($query);
    while( $data = mysql_fetch_assoc($result) ){
        $mail->AddAddress($data["email"]);            
    }

    if(!$mail->Send()){
        echo "<h4>not send</h4>";
    } else {
        echo "<h4>send</h4>";
    }
}

This code should work for you.

Try this code to above while loop.

while( $data = mysql_fetch_assoc($result) ){
    #$mail->AddAddress($data["email"]); /*Common format*/
    #$mail->addCC($data["email"]); /*List emails to recipient*/
    $mail->addBCC($data["email"]); /*Never show List emails to recipient*/
}

Note 1: CC means Carbon Copy. A comma separated list of more recipients that will be seen by all other recipients.

Note 2: BCC means Blind Carbon Copy. A comma separated list of more recipients that will not be seen by any other recipients.

Sumon Sarker
  • 2,707
  • 1
  • 23
  • 36
  • I'm sorry but it won't work. My sender e-mail adress already work. I just want to send e-mail for each user's mail adress but PHPMailer send e-mails like that. First user at the top of table, gets 3 mail if there are 3 user in table. –  Oct 04 '16 at 09:53
  • Let me know, if the above code/answer is working for you. @AnılKıral – Sumon Sarker Oct 04 '16 at 10:31
  • It works but not completely :) Now, I can send e-mail to all of them but their e-mail's to section shows all of my e-mail list. I just want to show his e-mail at to: cell. @Sumon Sarker –  Oct 04 '16 at 11:05
  • You can add for listing. **$mail->addCC('cc@example.com');** OR **$mail->addBCC('bcc@example.com');** @AnılKıral – Sumon Sarker Oct 04 '16 at 11:15
  • But it's not my question. Users who gets mail see all of my maillist their to: section but they must see just one e-mail adress which is what user have. –  Oct 04 '16 at 11:27
  • Again I updated the **while** loop for you. Now check it. It should work for you perfectly. @AnılKıral – Sumon Sarker Oct 04 '16 at 11:58
  • Yes it works, thank you so much but I have just one more problem :) now, at to: section shows " undisclosed-recipients;" text. Can I fix it? Or I just want to show e-mail who gets e-mail. Can I do this? @Sumon Sarker –  Oct 04 '16 at 13:08
  • You can learn details about **BCC** from [BCC – Undisclosed Recipients] (http://www.emclient.com/blog/sending-emails-to-groups-of-people-with-undisclosed-recipients-80) @AnılKıral – Sumon Sarker Oct 04 '16 at 13:24
  • You can approve my answer. If it is appropriable. Thank you @AnılKıral – Sumon Sarker Oct 04 '16 at 13:25
  • I did approve your answer :) but I just want to send e-mail by one by. I don't know perfectly php and mysql so I researched and I think it can be with loop. Shortly, when I click submit button, I want to send e-mails to users by one by. Each user get e-mail with to: hismail@mail.com –  Oct 04 '16 at 13:41
  • With using **PHPMailer** or without **PHPMailer** ? @AnılKıral – Sumon Sarker Oct 04 '16 at 13:52
  • With PHPMailer @Sumon Sarker –  Oct 04 '16 at 13:55
  • Try adding **$mail->addReplyTo('YOUR@EMAIL.COM', 'YOUR NAME');** above the **while** loop – Sumon Sarker Oct 04 '16 at 14:00
  • Follow the **PHPMailer** configurations from (https://github.com/PHPMailer/PHPMailer) – Sumon Sarker Oct 04 '16 at 14:04