Solved

PHP Mailer

Posted on 2012-12-23
6
527 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

630 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