Forum Moderators: coopster

Message Too Old, No Replies

Text email format troubles

         

Sylver

12:56 pm on Apr 8, 2006 (gmt 0)

10+ Year Member



I am using this bit of code to send email alerts:

$emaildata = 'This email is automatically generated to inform you'. "\r\n"
. 'that a potential customer requested a quote on your website.' . "\r\n\r\n";
$emaildata = $emaildata . 'Date: ' .$date . "\r\n" . 'Client name: ' . $clientName . "\r\n";
$emaildata = $emaildata . 'Client email: ' . $clientEmail . "\r\n" . 'Text submitted:' . "\r\n";
$emaildata = $emaildata . $textArea . "\r\n";
$emaildata = $emaildata . 'URL submitted: ' . $url . "\r\n";

However, for some reason, when I receive the email, 2 carriage returns are missing:

...inform you'. "\r\n" . 'that a potential...
and
$clientEmail . "\r\n" . 'Text submitted:'

I tried to echo $emaildata and it shows just fine, but my email client persists on ignoring those 2 carriage returns. The others work just fine.

Advice anyone?

coopster

1:55 pm on Apr 8, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, Sylver.

It's just another one of those beautiful MicroSoft features we have all come to love.

How do you hack mail() to get around Outlook's removal of line breaks [webmasterworld.com]?

fixed link

[edited by: coopster at 4:57 pm (utc) on April 8, 2006]

Birdman

4:19 pm on Apr 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The syntax looks bad to me. I see no reason for the double quotes and periods. Also, there is an easier way to "append" to a variable.

Try this instead:

$emaildata = 'This email is automatically generated to inform you \r\nthat a potential customer requested a quote on your website.\r\n\r\n';
$emaildata .= 'Date: ' .$date . '\r\nClient name: ' . $clientName . '\r\n';
$emaildata .= 'Client email: ' . $clientEmail . '\r\n'Text submitted: ' . time() . '\r\n';
$emaildata .= $textArea . '\r\n';
$emaildata .= 'URL submitted: ' . $url . '\r\n';

Sylver

6:28 pm on Apr 8, 2006 (gmt 0)

10+ Year Member



Thanks to both of you.

Birdman, when you say the syntax looks bad to you, do you mean it's actually wrong or simply ugly?

Either case ".=" is a lot cleaner.

Birdman

7:47 pm on Apr 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, I must appologize. I did some reading and testing and found that your syntax is valid. It just seemed odd that you mixed single and double quotes.

I did, however, read that multiple concenations(using ' . $var . ') is slow. Apparently, using double quotes is faster because vars are1 expanded and no concanations are needed.

Another thing I learned is that line breaks and such are preserved inside double quotes, which means that you wouldn't need to keep appending strings to a variable. You could do this:

$emaildata = "This email is automatically generated to inform you
that a potential customer requested a quote on your website

Date: $date
Client name: $clientName
Client email: $clientEmail
Text submitted: $textArea
URL submitted: $url
";

Thanks for making me learn something today :)

Sylver

12:56 am on Apr 9, 2006 (gmt 0)

10+ Year Member



Simple is beautiful. :-)

Thanks a lot! Really appreciate you taking the trouble of checking it out.

Birdman

1:26 pm on Apr 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Check out this good thread I just stumbled onto:

R. v. Concatenation of Strings, ex p. Pushing/Joining of Arrays [webmasterworld.com]

Sylver

3:55 pm on Apr 9, 2006 (gmt 0)

10+ Year Member



That's interesting experiment, but it looks like the results are wrong:

1. I ran his test on my machine several times and got these results:

Taylor, Zac, Isaac, Leo, Aaron, Nick, Scott, Dave, Bob, Clint, Chris, Thomas, Ryan, Dennis, Thimo, Steve,
Taylor, Zac, Isaac, Leo, Aaron, Nick, Scott, Dave, Bob, Clint, Chris, Thomas, Ryan, Dennis, Thimo, Steve
using concat: 0.0124199390411 seconds
using array: 0.0235970020294 seconds
difference: -0.0111770629883 seconds

Taylor, Zac, Isaac, Leo, Aaron, Nick, Scott, Dave, Bob, Clint, Chris, Thomas, Ryan, Dennis, Thimo, Steve,
Taylor, Zac, Isaac, Leo, Aaron, Nick, Scott, Dave, Bob, Clint, Chris, Thomas, Ryan, Dennis, Thimo, Steve
using concat: 0.000205039978027 seconds
using array: 0.000146865844727 seconds
difference: 5.81741333008E-005 seconds

Taylor, Zac, Isaac, Leo, Aaron, Nick, Scott, Dave, Bob, Clint, Chris, Thomas, Ryan, Dennis, Thimo, Steve,
Taylor, Zac, Isaac, Leo, Aaron, Nick, Scott, Dave, Bob, Clint, Chris, Thomas, Ryan, Dennis, Thimo, Steve
using concat: 0.000194072723389 seconds
using array: 0.000144958496094 seconds
difference: 4.91142272949E-005 seconds

As you can see, the results changed on each run, and most markedly between the first and the second run. All runs after the first returned a shorter time for the array, so it would seem he was right.

*BUT*

2. The code starts both processes from an array.

I tried concatening straight ($s= 'Taylor, '.'Zac, '. etc.) and this time the results are in favor of concatenation with a consistant E-005 times.

Conclusion from these experiments:

1. Results change quite a bit so the fastest method may change based on set-up and possibly system cache. (tried with Firefox, Mozilla and IE). I suspect PHP version, server, and a bunch of other variables may interfere with the test results. (In other words, this experiment has too many variables to allow a finite conclusion.

2. If the data are already in an array, pushing the array tends to be faster than extracting the array and concatenating.

3. If the data is not in an array, it's appears to be consistently faster, on my machine, to concat directly.

Here is the code revisited:

<?php
/*
Comparing speed of concatenating strings
and pushing and joining arrays
Original Author: andreasfriedrich

*/
#
function getmicrotime($t) {
list($usec, $sec) = explode(" ",$t);
return ((float)$usec + (float)$sec);
}
#
$x = array('Taylor','Zac','Isaac','Leo','Aaron','Nick','Scott',
'Dave','Bob','Clint','Chris','Thomas','Ryan','Dennis',
'Thimo','Steve');
# ##### section added - sylver #####
$s0 = microtime();
$s = 'Taylor, '.'Zac, '.'Isaac, '.'Leo, '.'Aaron, '.'Nick, '.'Scott, '.
'Dave, '.'Bob, '.'Clint, '.'Chris, '.'Thomas, '.'Ryan, '.'Dennis ,'.
'Thimo, '.'Steve';
echo $s, '<br>';
$e0 = microtime();
#
unset($s);
$s = array();
#
$s1 = microtime();
foreach($x as $val) {
$s .= $val . ', ';
}
echo $s, '<br>';
$e1 = microtime();
#
unset($s);
$s = array();
#
$s2 = microtime();
foreach($x as $val) {
array_push($s, $val);
}
$y = implode(', ', $s);
echo $y, '<br>';
$e2 = microtime();
#
$t0 = (getmicrotime($e0) - getmicrotime($s0));
$t1 = (getmicrotime($e1) - getmicrotime($s1));
$t2 = (getmicrotime($e2) - getmicrotime($s2));
$td = $t1 - $t2;
#
echo "using concat: $t0 seconds<br />
using concat from array: $t1 seconds<br>
using array: $t2 seconds<br><br>";
?>