Solved

php mailer

Posted on 2012-12-23
11
445 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
[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
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 110

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 110

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 …

738 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