Solved

PHP Mailer

Posted on 2012-12-23
6
524 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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 …
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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 dynamically set the form action using jQuery.

861 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