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-23
11
Medium Priority
?
448 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
 

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
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.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

610 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