Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
PHP/SMTP Mail Problem

Posted by Garrie (Garrie), 18 November 2004
Hello,
I am have problems with sending out emails. I am getting the following message:

Code:
PHP Warning:  mail() [<a href='function.mail'>function.mail</a>]: SMTP server re
sponse: 554 SMTP synchronization error in C:\Documents and Settings\Garrie Power
s\Desktop\PHP Mail\phpmail.php on line 73


Can anyone help me in identifying where and what the problem is?

The PHP code below is being executed from the command line via a BAT file.

The code is
Code:
<?php

$subject="National Student Survey";           // The email subject
$from   ="From:ns-survey@kcl.ac.uk\r\n";     // Who the email is from
$ReplyTo="Reply-To:ns-survey@kcl.ac.uk\r\n"; // Who replies should be sent to

// Extra header information
$extraHeader= $from . $ReplyTo . "X-Mailer: PHP/" . phpversion();

ini_set('sendmail_from', $from);

$lines = file('Test data.csv');  // Read the whol file into an array

//Go through each line of the array
foreach ($lines as $line_num => $line) {

  $details = explode(",",trim($line)); // Split the line up
  $sendTo  = $details[0]; // Student's email address
  $SCJ_CODE= $details[1]; // Student Number
  $SCE_SRTN= $details[2]; // Name

// Message text
$message="

Reasons for non-participation (reply to ns-survey@kcl.ac.uk).

***$SCJ_CODE      $SCE_SRTN
I confirm that I do not wish to participate in the National Student Survey   [   ]


My reason for not participating is (please mark one of the following boxes and delete the other options):

a)[   ]  I do not wish my personal details to be used for the purposes of this survey

b)[   ]  I believe that participation will exacerbate my health difficulties

c)[   ]  I believe I have been wrongly selected for inclusion in the survey.


Please note that instead of passing your personal contact details through to HEFCE/Ipsos, the above reason, in coded form, will be passed through instead.  

";                  

  mail($sendTo, $subject, $message, $extraHeader);

  sleep(1);

}
  ini_restore('sendmail_from');
?>


Posted by admin (Graham Ellis), 18 November 2004
You're running on a Windows box - slightly more complex as the sendmail is probably on another box and more configurable.  Some thoughts / ideas:

a) I would tend to cut the \r out of the extra headers as a test (they may or may not be needed)

b) Try a simple test (or have you already) with just a mail command to a fixed address, with a simple subject and one or two lines of text and see if that works

c) have a look at the php.ini; the SMTP configuration information in there applies to Windows. Make sure that you are talking to the right server

d) Make sure that your sendmail server can accept connections from the machine your PHP is running on and all the necessary authorisations are there.



Posted by Garrie (Garrie), 23 November 2004
Hello Graham,
Thanks for the reply. In the end I found a different solution using PHP. I found a PHP mailing list function which, after modification, did what I wanted. The difference is that this code connects directly to the SMTP server and works fine on Windows. I have included my modified version below. The original function can be found at http://www.greywyvern.com/php.php

Garrie
Code:
<?php
/* ***************************************************************
* Socket_mail Function v1.4
*  A simple and efficient mailing list function
* Copyright (C) 2004 GreyWyvern
*
* This program may be distributed under the terms of the GPL
*   - http://www.gnu.org/licenses/gpl.txt
*
* Tested using a Linux SMTP server.  I am unsure how a Windows
* SMTP server will react to this function; it may need some
* tweaking.
*
* This function accepts an array of email addresses in the form:
*
* array("Recipient's Name 1 <email@address1.com>",
*       "Recipient's Name 2 <email@address2.com>",
*       "Recipient's Name 3 <email@address3.com>",
*       ...);
*
* See the inline comments and http://www.greywyvern.com/php.php
* for more info
*************************************************************** */


 // Setup
 $fromName = "Brian Salter";
 $fromEmail = "ns-survey@kcl.ac.uk";
 $fromMailer = "Socketmail v2.0";
 $smtp = "smtp.kcl.ac.uk";
 $smtp_port = 25;
 $charset = "ISO-8859-1";

 $toArray = file('Test data.csv');  // Read the whole file into an array
  // The file is in the format EMAIL, SCJ_CODE, SCE_SRTN
 $subject = "National Student Survey";

 $baseMessage="
Reasons for non-participation (reply to ns-survey@kcl.ac.uk).

***SCJ_CODE      SCE_SRTN
I confirm that I do not wish to participate in the National Student Survey   [   ]


My reason for not participating is (please mark one of the following boxes and delete the other options):

a)[   ]  I do not wish my personal details to be used for the purposes of this survey

b)[   ]  I believe that participation will exacerbate my health difficulties

c)[   ]  I believe I have been wrongly selected for inclusion in the survey.


Please note that instead of passing your personal contact details through to HEFCE/Ipsos, the above reason, in coded form, will be passed

through instead.
";

 // Strip "\r" from the message (if it came from a form input)
 $baseMessage = str_replace(chr(13), "", $baseMessage);

 $baseMessage = str_replace("\r\n.", "\r\n..", str_replace("\n", "\r\n", stripslashes($baseMessage))." \r\n");

 ini_set('sendmail_from', $fromEmail);

 $connect = @fsockopen ($smtp, $smtp_port, $errno, $errstr, 5);
   if (!$connect) return false;
   $rcv = fgets($connect, 1024);

//  fputs($connect, "HELO {$_SERVER['SERVER_NAME']}\r\n");
 fputs($connect, "HELO 127.0.0.1\r\n");
   $rcv = fgets($connect, 1024);

 foreach ($toArray as $to) {

   $toBits   = explode(",", $to);
   $toRcpt   = trim($toBits[0]);
   $SCJ_CODE = $toBits[1];
   $SCE_SRTN = $toBits[2];

   $searchFor   = array("SCJ_CODE", "SCE_SRTN");
   $replaceWith = array($SCJ_CODE , $SCE_SRTN );
   $message     = str_replace($searchFor, $replaceWith, $baseMessage);

   fputs($connect, "RSET\r\n");
     $rcv = fgets($connect, 1024);

   fputs($connect, "MAIL FROM:$fromEmail\r\n");
     $rcv = fgets($connect, 1024);
   fputs($connect, "RCPT TO:$toRcpt\r\n");
     $rcv = fgets($connect, 1024);
   fputs($connect, "DATA\r\n");
     $rcv = fgets($connect, 1024);

   fputs($connect, "Subject: $subject\r\n");
   fputs($connect, "From: $fromName <$fromEmail>\r\n");
   fputs($connect, "To: $toRcpt\r\n");
   fputs($connect, "X-Sender: <$fromEmail>\r\n");
   fputs($connect, "Return-Path: <$fromEmail>\r\n");
   fputs($connect, "Errors-To: <$fromEmail>\r\n");
   fputs($connect, "Message-Id: <".md5(uniqid(rand())).".".preg_replace("/[^a-z0-9]/i", "", $fromName)."@$smtp>\r\n");
   fputs($connect, "X-Mailer: PHP - $fromMailer\r\n");
   fputs($connect, "X-Priority: 3\r\n");
   fputs($connect, "Date: ".date("r")."\r\n");
   fputs($connect, "Content-Type: text/plain; charset=$charset\r\n");
   fputs($connect, "\r\n");
   fputs($connect, $message);

   fputs($connect, "\r\n.\r\n");
     $rcv = fgets($connect, 1024);
 }

 fputs ($connect, "QUIT\r\n");
   $rcv = fgets ($connect, 1024);
 fclose($connect);
 ini_restore('sendmail_from');
?>




This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho