[Chugalug] Using g-mail for mass mailings

Mike Harrison cluon at geeklabs.com
Tue Oct 16 13:26:33 UTC 2012


On Tue, 16 Oct 2012, Ed King wrote:

> A high-tech company I know sends out 600+ emails a day via gmail with a
> <sarcasm> copyrighted </sarcasm> LGPL php class.   The top of the script is
> shown below...

Sarcasm noted. :)

You could just use Mail and Net_SMTP.

This is a quickly hacked together example that sends both plain text and 
HTML/Mime encoded.

This is egoware, if you like any part of this, sing praise. If you don't like it. Stay quite. 
=============================================================
<?php
#A partial smtp auth emailer example script.
#Kinda cut and pasted... not complete or real
#Requires:
#  pear install Mail
#  pair install Net_SMTP
#installed to operate properly.
     $message = "This is a test email, please ignore. " ;
     $email = "president at whitehouse.gov" ; #or other email address
     $host = "mail.mailserver.com.local";
     $username = "spamaroone";
     $password = "notapasswrd";
     $mailfrom = "foo at zoo.org.local" ;


#-----------------------------------------------------------------------------
     #Make a text and html version. - This is for a very specific case but might be useful as an example. r
     if (preg_match("/\<table/", $message, $m)) { #  consider original message is HTML if it has a table in it.
         $message = str_replace("tr>\n", "tr>", $message);
         $message = str_replace("/tr>", "/tr>\n", $message);
         $message = str_replace(" ", "", $message);
         $messagetext = strip_tags($message);
         $lines = split("[\n]", $messagetext); $messagetext = '' ;
         foreach($lines as $line) {  #right-aligns text to 60 chars
         while(strlen($line) < 60) { $line = ' ' . $line ; } ;
             $messagetext .= $line . "\n"  ;
         } ;
         $messagehtml = $message;
     } else { # message is probably plain text - wrap one with pre for html
         $messagetext = $message;
         $messagehtml = "<pre>\n$message\n</pre>";
     };
     $message = '';

#-----------------------------------------------------------------------------
     $boundary = '=' . md5(uniqid("boundary")); # should create a unique boundary for these messages
     $message.= "--$boundary\nContent-Type: text/plain; charset=iso-8859-1\nContent-Transfer-Encoding: 7bit\n\n";
     $message.= "$messagetext\n\n";
     $message.= "--$boundary\nContent-Type: text/html; name=\"juicemessage.html\" ; charset=iso-8859-1\nContent-Transfer-Encoding: 7bit\nContent-Disposition: attachment; filename=\"juicemessage.html\"\n\n";
     $message.= "$messagehtml\n\n";
     $message.= "--$boundary\n";
     $headers.= "From: $mailfrom<$mailfrom>\r\n";
     $headers.= "MIME-Version: 1.0\n";
     $headers.= "Content-type: multipart/mixed; boundary=\"$boundary\"\n";
     $headers.= "X-Mailer: Foo\nUser-Agent: Foo Email\n";
     $headers.= "X-Priority: 3\n";
     $headers.= "Importance: 3 (normal)\n";
     $headers.= "X-MSMail-Priority: Normal\n";
     $headers.= "\r\n\r\n"; #break between headers and non-mime body
     $headers.= "This is a MIME-formatted message.  If you see this text it 
means that your\nE-mail software does not support MIME-formatted 
messages.\n\n$messagetext\n\n";
     require_once "Mail.php";
        $aheaders = array ('From' => $mailfrom,'To' => "$email",'Subject' 
=> "$subject",
        'MIME-version' => '1.0',
        'Content-type' => "multipart/mixed; boundary=\"$boundary\"",
        'X-Mailer' => "Juice",
        'X-Priority' => "3",
        'Importance' => "3 (normal)",
        'X-MSMail-Priority' => "Normal"
        );
        $smtp = Mail::factory('smtp',array ('host' => $host,'auth' => 
true,'username' => $username,'password' => $password));

        $mail = $smtp->send("$email", $aheaders, $message);
        if (PEAR::isError($mail)) {
#            echo("<p>" . $mail->getMessage() . "</p>");
         } else {
#            echo("<p>Message successfully sent!</p>");
         }

?>




More information about the Chugalug mailing list