Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP Mailer

Posted on 2012-12-30
7
Medium Priority
?
438 Views
Last Modified: 2012-12-31
Hi experts,

I almost finished this php mailer. It creates a file for every smtp server then it splits the recipients file to the number of smtp servers, and at last it should open every smtp file, connect to the smtp in it and assign a recipients file. The code as it is now only connects to the first smtp. Please help.

<?php
error_reporting(E_ALL); 

$hosts        = "smtp.txt";
$emails       = "list.txt";
$hostscount   = count(file($hosts));
$emailscount  = count(file($emails));
$splitnum     = (int)($emailscount / $hostscount);
$handle       = fopen("smtp.txt", "r");
$handle2      = fopen("list.txt", "r");
$currentfile  = 1;
$output       = FALSE;
$output2      = FALSE;

// MESSAGE DETAILS

$fromemail    = "";
$fromname     = "";
$replyto      = "";
$mydomain     = "";
$subject      = "";

// PUT EVERY SMTP IN A DIFFERENT FILE

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

// SPLIT RECIPIENTS LIST TO THE NUMBER OF SMTP SERVERS

$currentfile2 = 1;

if ($emails) 
{
    while (!feof($handle2)) 
    {
        $buffer2 = fgets($handle2, 4096);
        if (!$output2) 
        { 
            $output2 = fopen("m-" . ($currentfile2++), "w");
            $lines2 = 0;
        }
        fwrite($output2, $buffer2);
        if (strrchr($buffer2,"\n")) 
        {
            $complete2 = TRUE;
            ++$lines2;
            if ($lines2 == $splitnum) 
            {
                fclose($output2);
                $output2 = FALSE;
            }
        } 
        else 
        {
            $complete2 = FALSE;
        }
    }
    fclose($handle2);
    if ($output2) 
    {
        if (!$complete2) 
        {
            fwrite($output2,"\n");
        }
        fclose($output2);
    }
}

// ASSIGN A RECIPIENTS FILE TO EVERY SMTP SERVER (FILE)

$currentfile3 = 1;
$currentfile4 = 1;

foreach (file("s-" . ($currentfile3++)) as $line) {

foreach (file("m-" . ($currentfile4++)) as $line2) {

$info2        = explode(",", $line2);
$email        = $info2[0];
$firstname    = $info2[1];
$lastname     = $info2[2];
$message      = "";

$info         = explode(",", $line);
$hostname     = $info[0];
$username     = $info[1];
$password     = $info[2];

$connect      = fsockopen($hostname, 25); 
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, "MAIL FROM: $fromemail\r\n");
fputs($connect, "RCPT TO: $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: 8bit\r\n");
fputs($connect, "To: $email\r\n");
fputs($connect, "From: \"$fromname\" <$fromemail>\r\n");
fputs($connect, "Subject: $subject\r\n\r\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
7 Comments
 
LVL 39

Expert Comment

by:Aaron Tomosky
ID: 38732942
Kinda hard to read with all that long fopen while not eof stuff.
With file_get_contents and explode you can make an array from a \n file in two lines.

What exactly are you having problems with?
0
 
LVL 17

Expert Comment

by:nanharbison
ID: 38732953
Have you tried echoing out your results to see if everything is working?
0
 

Author Comment

by:jebbie635
ID: 38733346
Don't need to echo, the results are the created files and test emails received. It connects to only one server.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Assisted Solution

by:Robert Schutt
Robert Schutt earned 2000 total points
ID: 38733398
The two foreach loops on lines 108 and 110 process all lines in files s-1 and m-1 respectively. They do not loop over existing files s-* and m-*, the ++ only increments the variables but you have no looping defined for them. You probably meant to loop those variables from one up to $currentfile1 and 2? (as those hold the number of files created)
0
 

Author Comment

by:jebbie635
ID: 38733537
How do i do that?
0
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 2000 total points
ID: 38733567
Well around the outer foreach you could place a loop:
while ($currentfile3 < $currentfile) {
// your entire foreach code ending near the end of the file...
}

Open in new window

I'm not sure if you need another loop, I checked (like the previous remark suggested) with some echo statements and it seems this would do it as each host has 1 associated file with emails.
0
 

Author Closing Comment

by:jebbie635
ID: 38733582
Works great. Thank you.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …
Suggested Courses

609 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