1

As part of an online subscription page, I have a PHP mailer that sends the details the user just entered, to 2 specified email addresses. The function has been working fine as far as I can remember but since a week it somehow doesn’t.

Users get the following error message in the browser:

Failed to add recipient: yyy@yyy.nl [SMTP: Invalid response code received from server (code: 550, response: R1: HELO should be a FQDN or address literal (See RFC 2821 4.1.1.1))]_________________________________________MIME-Version: 1.0 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable From: xxx@xxx.nl To: yyy@yyy.nl, zzz@zzz.com Date: Tue, 03 Jun 2014 18:18:41 +0200 Subject: Contact formulier Onderstaand bericht is op Tuesday, June 3rd, 2014 om 06:18 PM verstuurd via= het lidworden-contactformulier van de ledensite. Graag actie ondernemen! Subscribtion : Belangstellend lidmaa= tschap Surname :

Name :

Dateofbirth :

Placeofbirth :

Email :

Adres :

Postalcode :

City :

Telephone :

Mobiletelephone :

Warning: Cannot modify header information - headers already sent by (output started at /xxxxxxxxxxxxx/sendmail.php:39) in/xxxxxxxxxx.public_html/mailer.php on line 50

This is the code from the mailer.php:

<?PHP
include("sendmail.php");
    $from = “xxx@xxx.nl";
    $to = “yyy@yyy.nl, zzz@zzz.com";
    $subject = "Contact formulier"; 
    $reply_to = "";

    $date = date ("l, F jS, Y"); 
    $time = date ("h:i A"); 

if ($_SERVER['REQUEST_METHOD'] == "POST") {
    $data = $_POST;
} else {
    $data = $_GET;
}

$msg .= "<html>
    <body>
        Message<p>
    <table>\r\n";

foreach ($data as $key => $value) {
    if( strtolower($key) != "submit" ) {    // Ignore submit button
        $msg .= "<tr><td valign='top'>" . ucfirst ($key) . "</td><td>:</td><td>". nl2br($value) . "</td></tr>\n";
        if( $key == "Email" ) {
        $reply_to = $value;
            }
        }
    }

$msg .= "</table>
    </body>
    </html>";

$result = sendmail($from, $to, $reply_to, $subject, $msg, "");

if( $result ) {
    $location="/?page=response_success&menu=lerenvliegen";
} else {
    $location="/?page=response_failure&menu=lerenvliegen";
}
header ("Location:$location"); 
?>

Code from sendmail.php:

<?php
function sendmail($from, $to, $reply_to, $subject, $mailhtml) {
set_include_path("/usr/local/lib/php" . PATH_SEPARATOR . ini_get(”include_path”));

require_once 'Mail.php';
require_once 'Mail/mime.php';
$host = "localhost";  
$username = "";     
$password = ""; 
$mime = new Mail_mime();
$mime->setHTMLBody($mailhtml);  

if( $reply_to != "") {
    $extraheaders = array('From' => $from, 'To' => $to, 'Date' => date( 'r' ), 'Subject' => $subject, 'Reply-To' => $reply_to, 'Return-Path' => $reply_to);
} else {
    $extraheaders = array('From' => $from, 'To' => $to, 'Date' => date( 'r' ), 'Subject' => $subject);
}
$recipients['To']  = $to;

$smtp = Mail::factory('smtp',
    array ('host' => $host,
    'auth' => false,
    'username' => $username,
    'password' => $password));

$body = $mime->get();  
$headers = $mime->headers($extraheaders);
$result = $smtp->send($recipients, $headers, $body);

IF (PEAR::isError($result)):
    echo $result->getMessage() . "_________________________________________" . $mime->getMessage();
    return false;
ENDIF;

return true;
}

Am I missing some mistake in the code?

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
  • It's most likely the smart/curly quotes `“`. Beautiful, yet *deadly* `“`. – Funk Forty Niner Jun 03 '14 at 16:40
  • What's with the quotes in your code? Ex: `$from = “xxx@xxx.nl";` – j08691 Jun 03 '14 at 16:40
  • @ Fred, j08691, they just changed when I copied the code in to Textedit to make some privacy alterations to the code. – user3692149 Jun 03 '14 at 16:46
  • it's a SMTP server problem. It warns about an invalid `HELO` format, ie it must be like `HELO smtp.domain.com`. Check the script options, or php smtp settings. – Deadooshka Jun 03 '14 at 17:24
  • 1
    It appears to me that problem is that your server doesn't have a valid "fully qualified domain name" and the MTA is now rejecting emails the script is trying to send. Something about your MTA was either changed or upgraded in all probability. The "Headers already sent" is just a latent bug you always had, in the way the code is written, but it didn't happen because errors weren't occurring. – gview Jun 03 '14 at 17:24
  • @gview Look at my answer. The script was sending to `localhost` via `smtp` without any credentials for a while. Then suddenly stopped. Very simple issue. Either use `sendmail` if possible or get new a new `smtp` setup. – Giacomo1968 Jun 03 '14 at 17:46
  • @JakeGould That would default to using the defined MTA for the server, whatever that was. This doesn't look like a coding problem, and you're of course welcome to create a gigantic domain specific answer that is essentially an exercise in teaching debugging and system administration, but your assumption that the system already can't find an MTA is clearly not correct, because the existing MTA is returning the FQDN error. – gview Jun 03 '14 at 18:32
  • @gview “…and you're of course welcome to create a gigantic domain specific answer that is essentially an exercise in teaching debugging and system administration…” Quite patronizing for no reason. Take care! – Giacomo1968 Jun 03 '14 at 18:44
  • @JakeGould You're reading something into my reply that was not intended. With that said, my point is that this is not a problem with programming, other than that the code was inherently buggy when an SMTP error occurred. If this is a linux server there are things one would do to debug the problem from a bash shell, but that's system administration related. – gview Jun 03 '14 at 19:41
  • @gview Okay, no harm no foul. Appreciate the insight. – Giacomo1968 Jun 03 '14 at 20:40

1 Answers1

1

Warning: Cannot modify header information - headers already sent by (output started at /xxxxxxxxxxxxx/sendmail.php:39) in/xxxxxxxxxx.public_html/mailer.php on line 50

It seems that somewhere in sendmail.php around line 39 headers are already being output ahead of the headers being sent from mailer.php around line 50.

Or it could be that something changed on your server to cause an error in sendmail.php that would make it output an error around line 39. Which would mean header output was technically already started. So the additional headers being sent out in mailer.php around line 50 would choke.

EDIT: With the added code from sendmail.php in place, I noticed this error check:

IF (PEAR::isError($result)):
    echo $result->getMessage() . "_________________________________________" . $mime->getMessage();
    return false;
ENDIF;

But looking at your error, this seems to be the product of that:

Failed to add recipient: yyy@yyy.nl [SMTP: Invalid response code received from server (code: 550, response: R1: HELO should be a FQDN or address literal (See RFC 2821 4.1.1.1))]_____________________________________

Which to me reads like you are having an SMTP connection issue. The logic being:

  1. The mailer fails with a SMTP error.
  2. The code in sendmail.php is designed to output this error directly to the screen. Which sends headers the first time around.
  3. Then the code in mailer.php fails because the headers were already sent by sendmail.php.

Which all means the SMTP connection might be broken for your config. Looking at this code in mailer.php:

$host = "localhost";  
$username = "";     
$password = ""; 

And this below it:

$smtp = Mail::factory('smtp',
    array ('host' => $host,
    'auth' => false,
    'username' => $username,
    'password' => $password));

It seems like you were once able to connect via SMTP to localhost without a username or password at some point. And now it suddenly stopped working that way. Since Mail::factory seems to be a part of the PEAR Mail package, I would recommend trying to change that to sendmail and see if that works on your localhost:

$smtp = Mail::factory('sendmail',
    array ('sendmail_path' => '/usr/bin/sendmail',
    'sendmail_args' => '-i'));

The config is adjusted to match the PEAR Mail package manual sendmail specifications:

$params["sendmail_path"] - The location of the sendmail program on the filesystem. Default is /usr/bin/sendmail.

$params["sendmail_args"] - Additional parameters to pass to the sendmail. Default is -i.

You might need to change /usr/bin/sendmail to another path if that is not where sendmail is located on your server. Perhaps it would be in /usr/local/bin/sendmail? But try with /usr/bin/sendmail for now.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
  • This is line 39: 'echo $result->getMessage() . "_________________________________________" . $mime->getMessage(); '. I don't see how that would be already sending the header... – user3692149 Jun 03 '14 at 17:07
  • Can you add the code from `sendmail.php` to your question, please? – Giacomo1968 Jun 03 '14 at 17:07
  • I have just added the code to the question. Thanks! – user3692149 Jun 03 '14 at 17:16
  • @user3692149 That was a big help. Makes a big difference. It seems like you were able to send mails with `smtp` without a password via `localhost` in the past. That seems to have stopped working. The best solution I know now is to use `sendmail`. Please check my edits. Your SMTP connection is just not working. – Giacomo1968 Jun 03 '14 at 17:25
  • Jake, thanks a lot for your extensive comment, I tried it with /usr/bin/sendmail and that unfortunately didn't fix the issue. I then tried with /usr/local/bin/sendmail, this added the following lines to the error the user receives in the browser: Warning: escapeshellarg() has been disabled for security reasons in /usr/local/lib/php/Mail/sendmail.php on line 120 Warning: escapeshellarg() has been disabled for security reasons in /usr/local/lib/php/Mail/sendmail.php on line 120 Warning: escapeshellarg() has been disabled for security reasons in /usr/local/lib/php/Mail/sendmail.php on line 144 – user3692149 Jun 03 '14 at 17:40
  • @user3692149 Are you on a shared host? Or do you have your own server. It boils down to the `smtp` not working locally anymore. So the next best solution is to ask your ISP about `sendmail` or for you to change to `smtp` settings to another host/mailer. But this is the best I can do. Very clear issue. If you find this answer was useful, please be sure to upvote it. – Giacomo1968 Jun 03 '14 at 17:43
  • I'm not on a shared host, the company is renting its own server as far as I know. I will contact the original author of the code with your solution as I'm not yet sure how to effectively fix it. I do, however, now clearly understand what the issue is, so thank you for that. – user3692149 Jun 03 '14 at 19:22
  • @user3692149 “I'm not on a shared host, the company is renting its own server as far as I know.” Then whoever is the systems administrator for that server needs to be involved. No other option. – Giacomo1968 Jun 03 '14 at 19:53
  • thanks for clarifying that, I will make sure to contact them first thing in the morning and refer them back to this page. I'd up vote you if I had the reputation for it... ;-) – user3692149 Jun 03 '14 at 20:09
  • @user3692149 No problem. Another suggestion? Maybe you should see if a reboot of the server itself will clear this up. Can’t hurt. – Giacomo1968 Jun 03 '14 at 20:46
  • I unfortunately don't have such direct access to the server. – user3692149 Jun 03 '14 at 22:36