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?
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:
sendmail.php
is designed to output this error directly to the screen. Which sends headers the first time around.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.