...

View Full Version : \r\n Replace not working while sending emails



nickburrett
11-11-2012, 10:34 PM
When an email is sent from my website it has \r\n where there should be a line break in the client_notes field. I have tried to replace with \n but it's not working?


if($rsUpdate) {

// Remove \r\n from client_notes field
$client_notes = str_replace("\r\n", "\n", $client_notes);

// The message
$message = "BOOKING UPDATED:\n\n";
$message .= date("l jS F Y", strtotime($start_time)) . "\n";
$message .= date("g:i a", strtotime($start_time)) . ' until ' . date("g:i a", strtotime($finish_time)) . "\n";
$message .= $client_first . " " . $client_last . "\n";
$message .= $event_occasion . "\n";
$message .= "\n";
$message .= $venue_name . "\n";
$message .= $venue_address_1 . "\n";
$message .= $venue_address_2 . "\n";
$message .= $venue_town . "\n";
$message .= $venue_county . "\n";
$message .= $venue_postcode . "\n";
$message .= "\n";
$message .= $client_notes . "\n";

// Additional headers
$headers = 'From: Booking System <noreply@discos.co.uk>' . "\r\n";

// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70);

// Send
mail('nick@djnickburrett.com', 'BOOKING UPDATED', $message, $headers);

} else { die('Invalid query: '.mysql_error()); }

djm0219
11-12-2012, 10:42 AM
You have the arguments to str_replace backwards. The value to be replaced is first followed by the replacement value.


$client_notes = str_replace("\n","\r\n", $client_notes);

nickburrett
11-12-2012, 11:36 AM
I have replaced the code with your suggestion, but I'm still experiencing the same problem?


<?php include 'header.php'; ?>

<?php include 'login_success.php'; ?>

<?php
/// In order to use this script freely
/// you must leave the following copyright
/// information in this file:
/// Copyright 2012 www.turningturnip.co.uk
/// All rights reserved.

include("connect.php");

$id = $_POST['id'];

$advert_source = trim(mysql_real_escape_string($_POST["advert_source"]));
$company = trim(mysql_real_escape_string($_POST["company"]));
$start_date = trim(mysql_real_escape_string($_POST["start_date"]));
$start_month = trim(mysql_real_escape_string($_POST["start_month"]));
$start_year = trim(mysql_real_escape_string($_POST["start_year"]));
$start_hour = trim(mysql_real_escape_string($_POST["start_hour"]));
$start_minute = trim(mysql_real_escape_string($_POST["start_minute"]));
$finish_date = trim(mysql_real_escape_string($_POST["finish_date"]));
$finish_month = trim(mysql_real_escape_string($_POST["finish_month"]));
$finish_year = trim(mysql_real_escape_string($_POST["finish_year"]));
$finish_hour = trim(mysql_real_escape_string($_POST["finish_hour"]));
$finish_minute = trim(mysql_real_escape_string($_POST["finish_minute"]));
$event_occasion = trim(mysql_real_escape_string($_POST["event_occasion"]));
$brides_name = trim(mysql_real_escape_string($_POST["brides_name"]));
$grooms_name = trim(mysql_real_escape_string($_POST["grooms_name"]));
$bg_surname = trim(mysql_real_escape_string($_POST["bg_surname"]));
$birthday_name = trim(mysql_real_escape_string($_POST["birthday_name"]));
$birthday_age = trim(mysql_real_escape_string($_POST["birthday_age"]));
$venue_name = trim(mysql_real_escape_string($_POST["venue_name"]));
$venue_address_1 = trim(mysql_real_escape_string($_POST["venue_address_1"]));
$venue_address_2 = trim(mysql_real_escape_string($_POST["venue_address_2"]));
$venue_town = trim(mysql_real_escape_string($_POST["venue_town"]));
$venue_county = trim(mysql_real_escape_string($_POST["venue_county"]));
$venue_postcode = trim(mysql_real_escape_string($_POST["venue_postcode"]));
$client_title = trim(mysql_real_escape_string($_POST["client_title"]));
$client_first = trim(mysql_real_escape_string($_POST["client_first"]));
$client_last = trim(mysql_real_escape_string($_POST["client_last"]));
$client_address_1 = trim(mysql_real_escape_string($_POST["client_address_1"]));
$client_address_2 = trim(mysql_real_escape_string($_POST["client_address_2"]));
$client_town = trim(mysql_real_escape_string($_POST["client_town"]));
$client_county = trim(mysql_real_escape_string($_POST["client_county"]));
$client_postcode = trim(mysql_real_escape_string($_POST["client_postcode"]));
$home_telephone = trim(mysql_real_escape_string($_POST["home_telephone"]));
$mobile_telephone = trim(mysql_real_escape_string($_POST["mobile_telephone"]));
$email_address = trim(mysql_real_escape_string($_POST["email_address"]));
$client_notes = trim(mysql_real_escape_string($_POST["client_notes"]));
$quotation_amount = trim(mysql_real_escape_string($_POST["quotation_amount"]));
$deposit_date = trim(mysql_real_escape_string($_POST["deposit_date"]));
$deposit_amount = trim(mysql_real_escape_string($_POST["deposit_amount"]));
$deposit_method = trim(mysql_real_escape_string($_POST["deposit_method"]));
$deposit_staff = trim(mysql_real_escape_string($_POST["deposit_staff"]));
$balance_date = trim(mysql_real_escape_string($_POST["balance_date"]));
$balance_amount = trim(mysql_real_escape_string($_POST["balance_amount"]));
$balance_method = trim(mysql_real_escape_string($_POST["balance_method"]));
$balance_staff = trim(mysql_real_escape_string($_POST["balance_staff"]));
$date_booking_taken = trim(mysql_real_escape_string($_POST["date_booking_taken"]));
$dj_requested = trim(mysql_real_escape_string($_POST["dj_requested"]));
$quotation_status = trim(mysql_real_escape_string($_POST["quotation_status"]));

$start_time = sprintf('%d-%d-%d %d:%d', $start_year, $start_month, $start_date, $start_hour, $start_minute);
$finish_time = sprintf('%d-%d-%d %d:%d', $finish_year, $finish_month, $finish_date, $finish_hour, $finish_minute);

$deposit_explode = explode("/", $deposit_date);
$deposit_implode = sprintf('%d-%d-%d', $deposit_explode[2], $deposit_explode[1], $deposit_explode[0]);

$balance_explode = explode("/", $balance_date);
$balance_implode = sprintf('%d-%d-%d', $balance_explode[2], $balance_explode[1], $balance_explode[0]);

$booking_taken = explode("/", $date_booking_taken);
$booking_taken_implode = sprintf('%d-%d-%d', $booking_taken[2], $booking_taken[1], $booking_taken[0]);

$rsUpdate = mysql_query("UPDATE bookings
SET advert_source = '$advert_source', company = '$company', start_time = '$start_time', finish_time = '$finish_time', event_occasion = '$event_occasion', brides_name = '$brides_name', grooms_name = '$grooms_name', bg_surname = '$bg_surname', birthday_name = '$birthday_name', birthday_age = '$birthday_age', venue_name = '$venue_name', venue_address_1 = '$venue_address_1', venue_address_2 = '$venue_address_2', venue_town = '$venue_town', venue_county = '$venue_county', venue_postcode = '$venue_postcode', client_title = '$client_title', client_first = '$client_first', client_last = '$client_last', client_address_1 = '$client_address_1', client_address_2 = '$client_address_2', client_town = '$client_town', client_county = '$client_county', client_postcode = '$client_postcode', home_telephone = '$home_telephone', mobile_telephone = '$mobile_telephone', email_address = '$email_address', client_notes = '$client_notes', quotation_amount = '$quotation_amount', deposit_date = '$deposit_implode', deposit_amount = '$deposit_amount', deposit_method = '$deposit_method', deposit_staff = '$deposit_staff', balance_date = '$balance_implode', balance_amount = '$balance_amount', balance_method = '$balance_method', balance_staff = '$balance_staff', date_booking_taken = '$booking_taken_implode', dj_requested = '$dj_requested', quotation_status = '$quotation_status'
WHERE id = '$id' ");

if($rsUpdate) {

// Remove \r\n from client_notes field
$client_notes = str_replace("\n","\r\n", $client_notes);

// The message
$message = "BOOKING UPDATED:\n\n";
$message .= date("l jS F Y", strtotime($start_time)) . "\n";
$message .= date("g:i a", strtotime($start_time)) . ' until ' . date("g:i a", strtotime($finish_time)) . "\n";
$message .= $client_first . " " . $client_last . "\n";
$message .= $event_occasion . "\n";
$message .= "\n";
$message .= $venue_name . "\n";
$message .= $venue_address_1 . "\n";
$message .= $venue_address_2 . "\n";
$message .= $venue_town . "\n";
$message .= $venue_county . "\n";
$message .= $venue_postcode . "\n";
$message .= "\n";
$message .= $client_notes . "\n";

// Additional headers
$headers = 'From: Booking System <noreply@discos.co.uk>' . "\r\n";

// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70);

// Send
mail('nick@djnickburrett.com', 'BOOKING UPDATED', $message, $headers);

} else { die('Invalid query: '.mysql_error()); }

if($rsUpdate) { echo "Successfully updated"; } else { die('Invalid query: '.mysql_error()); }
?>

<?php include 'footer.php'; ?>

djm0219
11-12-2012, 11:50 AM
I actually misread what you were trying to do and you had it correct in your original post. What type of field is client_notes on your form?

nickburrett
11-12-2012, 11:52 AM
Hi

It's a textarea.

Cheers
Nick

Fou-Lu
11-12-2012, 07:32 PM
Ummm, you can't use \r\n within $client_notes if you intend to use it in your email. Body of email is separated by \n; headers are separated by \r\n. If it comes from a textarea, then its just \n that's within it. You shouldn't need to do a thing with it at all.
Are you seeing the literal chars \r\n? If so, that's because you have chosen to use SQL escaped data in your string. Don't do this; SQL escapes are designed for the SQL engine in question, not for string data you plan to use elsewhere. Effectively the SQL escape method converts \r\n to \\r\\n so when you issue it in a string you see the literal chars \r\n since it interprets the \\ as a \ and the r or n as separate chars.

You can clean up the escapes as well by using a loop or an array walk or map.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum