Solved

PHP Mailer

Posted on 2012-12-23
6
520 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

746 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

9 Experts available now in Live!

Get 1:1 Help Now