Hello PHP People
Topic: I have trouble sending emails from a php script. The line feed is strange...
On my community website www.deafzone.ch, I use a PHP script to write newsletter to all registred visitors. It's a form: <textarea name="body" cols="60" rows="20" wrap="VIRTUAL"></textarea>.
Here I write down my newsletter, then click on send. Then following code is executed to send the newsletter:
if ($submitted == 1) {
// Collecting subject, eMail etc.
...
// Then send...
$report = sendRoundmail($subject, $body, $eMail, $eMail);
}
// function from mail.inc
function sendRoundmail($subject, $body, $from, $replyTo) {
$query = ("SELECT DISTINCT Email FROM ADRESS ORDER BY ID DESC");
$result = mysql_query($query);
while($row_array = mysql_fetch_array($result)) {
$mailTo = $row_array[0];
$success = sendmail($mailTo, $subject, $body, $from, $replyTo);
if (strlen($report) > 0) {
$report = $report . "<BR>";
}
if ($success == true) {
$report = $report . "+ eMail for $mailTo sent!";
} else {
$report = $report . "- eMail for $mailTo not sent!";
}
}
return $report;
}
// function from mail.inc
function sendmail($address, $subject, $body, $from, $replyTo) {
$subject = stripslashes($subject);
$body = stripslashes($body);
$body = ereg_replace("\r\n", "\r", $body);
$success = false;
$header = "From: " . $from . "\r\n";
$header .= "Reply-To: ". $replyTo . "\r\n";
$header .= "X-Mailer: PHP/" . phpversion() . "\r\n";
$success = mail("$address", "$subject", "$body", "$header");
return $success;
}
So, my problem is the line feed! On some mail clients, the newsletter appears correctly. But other mail clients, most of all Outlook Express 6.0, show all phrases on one line! There the line feed doesn't work!
I assume, the problem is on that line:
-> $body = ereg_replace("\r\n", "\r", $body);
I now, Unix and Windows use different signs for line feed. But how must I handle this?
Regards
Michael Heuberger
Software Engineer from Switzerland