Solved

php mailer

Posted on 2012-12-23
11
443 Views
Last Modified: 2012-12-23
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
Comment
Question by:jebbie635
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 30

Expert Comment

by:IanTh
ID: 38716918
0
 

Author Comment

by:jebbie635
ID: 38716931
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
 

Author Comment

by:jebbie635
ID: 38716941
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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 109

Expert Comment

by:Ray Paseur
ID: 38716999
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
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 38717308
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
 

Author Comment

by:jebbie635
ID: 38717435
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
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 250 total points
ID: 38717452
0
 

Author Comment

by:jebbie635
ID: 38717454
what do i have to do?
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 38717483
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
 

Author Comment

by:jebbie635
ID: 38717497
Would you please write it?
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 38717534
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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.  …
The viewer will learn how to dynamically set the form action using jQuery.
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…

828 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