Solved

PHP Mailer

Posted on 2012-12-23
6
521 Views
Last Modified: 2012-12-24
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

0
Comment
Question by:jebbie635
  • 4
  • 2
6 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 38717691
Before starting the new loop (below the comment "// LOAD A LIST FOR EACH CONNECTION
"), reset the file counter:
$currentfile = 1;

Open in new window

0
 
LVL 35

Expert Comment

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

Author Comment

by:jebbie635
ID: 38717781
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 35

Assisted Solution

by:Robert Schutt
Robert Schutt earned 500 total points
ID: 38718015
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

0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 38718060
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.
0
 

Author Closing Comment

by:jebbie635
ID: 38718414
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now