Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 461
  • Last Modified:

php mailer

Hi experts,

I need to add smtp connections to this php mailer.

Please help.


<?php
$list = file('list.txt');

foreach ($list as $line) {

$info = explode(",", $line);
$email = $info[0];
$firstname = $info[1];
$lastnameraw = $info[2];
$lastname = str_replace("\r\n","",$lastnameraw);
$fullname = "$firstname $lastname";

$fromemail = "";
$fromname = "";
$replyto = "";

$header = "From: $fromname <$fromemail>\r\nReply-To: $replyto\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: text/html\r\n";
$header .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
$header .= "$message\r\n";

$subject = "";
$message = "";

mail($email, $subject, "", $header);
}
?>

Open in new window

0
jebbie635
Asked:
jebbie635
  • 5
  • 3
  • 2
  • +1
2 Solutions
 
jebbie635Author Commented:
I don't understand it.

I need to load the smtps from a file like this

$hosts = file('hosts.txt');
$info = explode(",", $line);
$host = $info[0];
$user = $info[1];
$password = $info[2];

and use this for every connection simultaneously

$smtp_port = 25;
$authtype = 'LOGIN';
$bccsize = '50'; // disconnect from the host after sending to 50 recipients
$smtp_server = '$host':
$user = '';
$password = '';

How can i make it remember what emails were sent to not resend them and then reconnect and continue?

Thanks.
0
 
jebbie635Author Commented:
I was thinking to split the recipients list to the number of hosts. If each host gets a file will it remember what emails were sent? Seems complicated.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Ray PaseurCommented:
How can i make it remember what emails were sent to not resend them and then reconnect and continue?
You can use a data base.  Make it a "log" table.  As each email is sent, add it to the log.  Whenever the script has to be restarted, SELECT the already sent emails from the log, and skip sending the email if the log contains an indicator that the message has already been sent.
0
 
Dave BaldwinFixer of ProblemsCommented:
The PHP mail() function in your original post will not do username and password authentication.  You would need something more complex like PHPMailer.  Here's a link to the current version: http://code.google.com/a/apache-extras.org/p/phpmailer/wiki/PHPMailer
0
 
jebbie635Author Commented:
That's a good solution. I have one more thing to do before doing that. Here's what i have, all i have to do is make 'foreach' function work. I get this error: Warning: Invalid argument supplied for foreach().

<?php
$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;

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);
}
}

echo "$listcount / $hostscount = $splitnum";

$port = 25;
$bccsize = 50;
$mydomain = "mydomain.com"; 

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

foreach ($hosts as $line) {

$info = explode(",", $line);
$hostname = $info[0];
$username = $info[1];
$password = $info[2];
$lists = fopen("list.txt" . ($currentfile++), "r");

foreach ($lists as $line) {

$info = explode(",", $line);
$email = $info[0];
$firstname = $info[1];
$lastnameraw = $info[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
 
Dave BaldwinFixer of ProblemsCommented:
0
 
jebbie635Author Commented:
what do i have to do?
0
 
Dave BaldwinFixer of ProblemsCommented:
Your first 'foreach' is:

foreach ($hosts as $line)

Trouble is,  $host is nothing more than the filename.  Nowhere do you read that file into a variable that I can see.  While you supposedly read it on line 5, all you actually store is the count, not the file contents.

http://us3.php.net/manual/en/function.file.php
0
 
jebbie635Author Commented:
Would you please write it?
0
 
Ray PaseurCommented:
I don't think we can write your code for you, at least not for free but we can offer some suggestions.  

First and foremost, learn what the PHP functions return.  PHP file() reads a file.  You do not have to open it and read it again!  If you use atomic statements instead of compound statements like lines 4 and 5 in the snippet above, you will be able to access the content of the output from file() and you can store this content in a variable.  Then you can apply the count() function to that variable.  And then you will have the file contents in an array and a count of the array.  This means half the load on the server.

Second, adopt the habit of raising error_reporting(E_ALL); because you're in a coding and debugging process and you really want to see all the notices, warnings and errors.

Third, learn about coding standards, especially for indentation of control structures.  A partial example is in this code snippet.  When the code is lined up this way, the logical intent is much clearer.  When there is no indenting or incorrect indenting it makes the programming hard to read and obscures the programmers intent.

Last, learn about how to add comments to your code.  It will help anyone in the future to understand what you were thinking.  This includes you, day after tomorrow, when you come back from a long weekend and take up a complex task all over again.

Best regards and good luck with your project, ~Ray
<?php
error_reporting(E_ALL);

$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;

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);
    }
}

Open in new window

0

Featured Post

Industry Leaders: 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!

  • 5
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now