NYCPHP Meetup

NYPHP.org

[nycphp-talk] smtp socket error that i just don't get

Mark Armendariz nyphp at enobrev.com
Fri Nov 7 17:08:53 EST 2003


Hello all,
 
I'm hoping you guys can lend a hand... Below is some code I grabbed from the
manual (http://www.php.net/manual/en/ref.mail.php) for sending mail directly
through the socket rather than using the mail command.  Now it works well on
my server, but when I installed it on my client's server, I get an endless
loop of errors like so:
 
Notice: fputs(): send of 41 bytes failed with errno=32 Broken pipe in
/usr/local/etc/httpd/htdocs/bostonindoorgames/admin/mailer.php on line 136

Notice: fputs(): send of 42 bytes failed with errno=32 Broken pipe in
/usr/local/etc/httpd/htdocs/bostonindoorgames/admin/mailer.php on line 136
 
The code is below, and line 136 is right under the comment // --------
PROBLEM STARTS HERE!!! ----------------
 
Now, as I mentioned, this same code works on my server just fine.  There is
surrounding code, but it's all involved in setting the vars and such for the
email itself.  I assure you all vars are set properly, as I've checked them
at least 6 times.  I thought that there may possibly be some kind of socket
issue, but then wouldn't it have a problem connecting to the socket in the
first place? Or even sending the HELO command after the connect?
 
When searching for the error, I came up with this (which is a ll gibberish
to me):
 
Broken pipe
===========

This condition is often normal, and the message is merely
informational (as when piping many lines to the head program).
The condition occurs when a write on a pipe does not find a
reading process. This usually generates a signal to the executing
program, but this message displays when the program ignores the
signal.

Check the process at the end of the pipe to see why it exited.

The symbolic name forthis error is EPIPE, errno=32.

 
 
 
   // Open socket to SMTP server
   $connect = fsockopen (ini_get('SMTP'), ini_get('smtp_port'), $errno,
$errstr, 30) or die('Could not talk to the sendmail server!'); 
   $rcv = fgets($connect, 1024); 
  
   // Send greeting to SMTP server 
   fputs($connect, 'HELO {mail.' . $_SERVER['SERVER_NAME'] . '}' . $nl); 
   $rcv = fgets($connect, 1024); 
   
   // Loop through recipients and send mail
   foreach($recipients_array as $recipient) {
   
    if (strlen($recipient['recipient_name'])) {
     $to_name = $recipient['recipient_name'];
    } else {
     $to_name = '';
    }
    
    $to_address = $recipient['recipient_email'];
    
    if (strlen($queue_array['unsubscribe_text'])) {
     $html_unsubscribe_link  = '<a href="' . SITE_URL .
'unsubscribe.php?email=' . $to_address . '">' .
$queue_array['unsubscribe_text'] . '</a>';
     $ascii_unsubscribe_link = $queue_array['unsubscribe_text'] . ': ' .
SITE_URL . 'unsubscribe.php?email=' . $to_address;
    } else {
     $html_unsubscribe_link  = '<a href="' . SITE_URL .
'unsubscribe.php?email=' . $to_address . '">Unsubscribe</a>';
     $ascii_unsubscribe_link = 'Unsubscribe: ' . SITE_URL .
'unsubscribe.php?email=' . $to_address;
    }
   
    if (strlen(trim($to_name))) {
     $to_email = $to_name . ' <' . $to_address . '>';
    } else {
     $to_email = $to_address;
    }
    
    $outer_boudary  = "----=_EnoOuterBoundary_000";
    $inner_boudary  = "----=_EnoInnerBoundary_001";
    
    $message_header = 'From: ' . $from_name . ' <' . $from_email . '>' . $nl
        . 'To: ' . $to_email . $nl
        . 'Subject: ' . $subject . $nl
        . 'MIME-Version: 1.0' . $nl
        . 'X-Mailer: Enobrev Mailer Version 1.0' . $nl
        . 'Content-Type: multipart/mixed;'
        . "\t" . 'boundary="' . $outer_boudary . '"' . $nl;
    
    // Messages start with text/html alternatives
    $message  = 'This is a multi-part message in MIME format.' . $nl . $nl
        . '--' . $outer_boudary . $nl
        . 'Content-Type: multipart/alternative;' . $nl
        . "\t" . 'boundary="' . $inner_boudary . '"' . $nl . $nl
    
    // PLAIN SECTION
        . '--' . $inner_boudary . $nl
        . 'Content-Type: text/plain;' . $nl
        . "\t" . 'charset="iso-8859-1"' . $nl
        . 'Content-Transfer-Encoding: quoted-printable' . $nl . $nl
        
    // PLAIN TEXT
        . stripslashes($ascii_message . $nl . $nl . $ascii_unsubscribe_link)
. $nl . $nl
    
    // HTML SECTION
        . '--' . $inner_boudary . $nl
        . 'Content-Type: text/html;' . $nl
        . "\t" . 'charset="iso-8859-1"' . $nl
        . 'Content-Transfer-Encoding: base64' . $nl . $nl
        
    // HTML
        . chunk_split(base64_encode($html_message . '<br /><br />' .
$html_unsubscribe_link)) . $nl . $nl
    
    // END INNER BOUNDARY
        . '--' . $inner_boudary . '--' . $nl . $nl
    
    // END MESSAGE
        . '--' . $outer_boudary . '--' . $nl;
   
 
    // ------------------------------------------- PROBLEM STARTS HERE!!!
------------------------------------------------------
   
    fputs($connect, 'MAIL FROM:' . $from_email . $nl);  // <-- LINE 136
    $rcv = fgets($connect, 1024); 
    
    fputs($connect, 'RCPT TO:' . $to_address . $nl); 
    $rcv = fgets($connect, 1024); 
    
    fputs($connect, 'DATA' . $nl); 
    $rcv = fgets($connect, 1024); 
     
    fputs($connect, $message_header . $message . $nl); 
    $rcv = fgets($connect, 1024); 
    
    fputs($connect, '.' . $nl); 
    $rcv = fgets($connect, 1024);
    
    fputs($connect, 'RSET' . $nl); 
    $rcv = fgets($connect, 1024); 
    
    echo 'sent to ' . htmlspecialchars($to_email) . '<br />';
  
    $db->Execute('DELETE FROM ' . TABLE_QUEUE_RECIPIENTS 
        . ' WHERE queue_id = ' . $PassedVars['queue_id']
        . ' AND recipient_id = ' . $recipient['recipient_id']);
   }
   
   fputs ($connect, 'QUIT' . $nl); 
   $rcv = fgets ($connect, 1024); 
   echo $rcv;
   
   fclose($connect); 
   ini_restore('sendmail_from'); 
 
 
 
 
 
 
thank you for your help, and have a great weekend!!!
 
Mark
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20031107/2451e8ac/attachment.html>


More information about the talk mailing list