Improve company productivity with a Business Account.Sign Up

x
?
Solved

php mailer

Posted on 2012-12-23
11
Medium Priority
?
473 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
 

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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 
LVL 111

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 84

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 84

Assisted Solution

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

Author Comment

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

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 111

Accepted Solution

by:
Ray Paseur earned 1000 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

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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…
I recently worked on a Wordpress site that utilized the popular ContactForm7 (https://contactform7.com/) plug-in that only sends an email and does not save data. The client wanted the data saved to a custom CRM database. This is my solution.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

607 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