PHP Mailer

jebbie635
jebbie635 used Ask the Experts™
on
Hi experts,

I have a list of smtp servers and one with recipients which i am splitting to the number of servers for each connection to load a different list. I got stuck on the loading part, please take a look at the code. I also need to send cc/bcc size to the server and dont know how.

Thanks.

<?php
error_reporting(E_ALL); 

$port        = 25;
$bccsize     = 50;
$mydomain    = "";
$fromemail   = "";
$fromname    = "";
$replyto     = "";
$subject     = "";
$message     = "";
$list        = "list.txt";
$hosts       = "hosts.txt";
$listcount   = count(file($list));
$hostscount  = count(file($hosts));
$splitnum    = (int)($listcount / $hostscount);
$handle      = fopen("list.txt", "r");
$currentfile = 1;
$output      = FALSE;

echo "Recipients: $listcount<br>";
echo "Servers: $hostscount<br>";
echo "Files: $splitnum<br>";

// SPLIT RECIPIENTS LIST FOR EACH CONNECTION

if ($list) 
{
    while (!feof($handle)) 
    {
        $buffer = fgets($handle, 4096);
        if (!$output) 
        { 
            $output = fopen("list.txt" . ($currentfile++), "w");
            $lines = 0;
        }
        fwrite($output, $buffer);
        if (strrchr($buffer,"\n")) 
        {
            $complete = TRUE;
            ++$lines;
            if ($lines == $splitnum) 
            {
                fclose($output);
                $output = FALSE;
            }
        } 
        else 
        {
            $complete = FALSE;
        }
    }
    fclose($handle);
    if ($output) 
    {
        if (!$complete) 
        {
            fwrite($output,"\n");
        }
        fclose($output);
    }
}

// LOAD A LIST FOR EACH CONNECTION

foreach (file($hosts) as $line) {

$info = explode(",", $line);
$hostname = $info[0];
$username = $info[1];
$password = $info[2];
$lists = fopen("list.txt" . ($currentfile++), "r");  // HERE IS THE PROBLEM

foreach (file($lists) as $line2) {

$info2 = explode(",", $line2);
$email = $info2[0];
$firstname = $info2[1];
$lastnameraw = $info2[2];
$lastname = str_replace("\r\n","",$lastnameraw);
$fullname = "$firstname $lastname";

}

$connect = fsockopen($hostname,$port); 
fputs($connect, "EHLO $mydomain\r\n"); 
fputs($connect, "AUTH LOGIN\r\n"); 
fputs($connect, base64_encode($username)."\r\n"); 
fputs($connect, base64_encode($password)."\r\n"); 

fputs($connect, "FROM:$fromname <$fromemail>\r\nReply-To: $replyto\r\n");
fputs($connect, "RCPT TO:$fullname <$email>\r\n"); 
fputs($connect, "DATA\r\n"); 
fputs($connect, "MIME-Version: 1.0\r\n");
fputs($connect, "Content-Type: text/html\r\n");
fputs($connect, "Content-Transfer-Encoding: base64\r\n\r\n");
fputs($connect, "To: $recipient\n");
fputs($connect, "Subject: $subject\n\n"); 
fputs($connect, "$message\r\n"); 
fputs($connect, ".\r\n"); 
fputs($connect, "QUIT\r\n");

}
?>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Software Engineer
Commented:
Before starting the new loop (below the comment "// LOAD A LIST FOR EACH CONNECTION
"), reset the file counter:
$currentfile = 1;

Open in new window

Robert SchuttSoftware Engineer

Commented:
Note, I have not tested the code as a whole. One more thing I noticed is that you use an undeclared variable ($recipient).

Author

Commented:
That works i tested it by editing the files created but i need to change this
$lists = fopen("list.txt" . ($currentfile++), "r"); 

Open in new window

into something like this to work with second 'foreach'
$lists = file("list.txt" . ($currentfile++)); 

Open in new window

or like this
$lists = "'list.txt' . ($currentfile++)"; 

Open in new window

Tried several times i cant get it work.

As for that variable i can say i have no idea why it is there, i pasted from o forum the headers and added some more from another php mailer, but RCPT-TO & TO seem the same to me. I will post another question about this.
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Robert SchuttSoftware Engineer
Commented:
I think this part
$lists = fopen("list.txt" . ($currentfile++), "r");  // HERE IS THE PROBLEM

foreach (file($lists) as $line2) {

Open in new window

should be taken together and replaced by
foreach (file("list.txt" . ($currentfile++)) as $line2) {

Open in new window

Robert SchuttSoftware Engineer

Commented:
There seem to be some other problems in the logic of the script which I suggest you test with echo first, commenting out the smtp socket code for the moment...

Some things I noticed while testing:
- in the splitting loop it didn't generate the files I would have expected, eg 7 addresses split over 2 hosts gave 3 lines in file1, 3 lines in file2 and 1 in file3. That last file would not be processed by the second loop because that runs only twice, once for each host.
- the last bit of code is run once per host, it could be that it was originally designed to send the mail to all of the recipients at once, but if you want to personalize the mail and/or avoid being classified as spam you would need to put this inside the inner loop. It might be possible (and if so indeed a good idea) to do the connect before the loop. By the way, depending on your server setup, I think there are better ways of sending mail. I saw a post by the awesome Ray_Paseur about this recently.

Author

Commented:
The split is ok for now. I just want to make one host send to more than one recipients for each connection. That should be easy after i get the smtp headers right. Im posting another question about this.

Thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial