Page 1 of 1

mail() function woes

Posted: Wed Aug 31, 2005 3:18 am
by influx
Hi all,

I am writing a script to email all fifty of my contacts from my address book from a CSV file. Everything is fine with the CSV file, i fgetcsv() without problems, but when I am sending my message, it is a good ten lines long with three paragraphs.

When I input my \n into the e-mail, it doesn't show a line break it actually displays "\n" in the e-mail without any line breaks in it. Just free flow text.

Here is my code:

Code: Select all

//csv column 1=names, column 2=emails
$handle = fopen ("test.csv","r"); 
while ($data = fgetcsv ($handle, 1000, ","))
{ 
	print $data[0] . "<br>\n"; 
	print $data[1] . "<br><br>\n";
	$message = 'Dear '.$data[0].',\n\nI hope you are well! It\'s been a while and I wanted to check in with you, see how you\'ve been, and make sure you have my long term contact info for your address book.\n\nI\'m still coordinating several special projects ahsdflkjhalksdjhf lkjahsdflkj hlaksjdhflkjahsdflkj halksdjhf laksjdhflkjahs lkjahsldk. That\'s all on my side - look forward to hearing how you are doing.\n\nTake care,\nball';
	$message = wordwrap($message, 70);
	mail($data[1], 'Greetings from Ball', $message, 'From: ball@gmail.com');
} 
fclose ($handle);

Thanks a ton![/b]

Posted: Wed Aug 31, 2005 7:05 am
by raghavan20
you should use double quotes for echo/string assignment instead of single quotes.
single quotes is used for considering the input as mere literals rather understanding its innate meaning.
refer usage of single and double quotes in echo statements.

Posted: Wed Aug 31, 2005 3:58 pm
by jayshields
He is exactly right, things included in double quotes will be used 'properly', whereas things included in single quotes will just be printed. So...

Code: Select all

$name = "John";

echo "$name"; //Would output John
echo '$name'; //Would output $name

//So...

echo "\n"; //Would send a newline to the browser
echo '\n'; //Would send the characters \ and n to the brower

It's hard to explain so I hoped my examples helped.

Posted: Wed Aug 31, 2005 4:56 pm
by josh
jayshields wrote:things included in double quotes will be used 'properly'
(expanded)
jayshields wrote:whereas things included in single quotes will just be printed
(printed as a literal)


Just thought I would put the 'techno mumbo jumbo names' on here


As an alternative you could send an html email and use <br /> instead of newline characters, you could use simple formatting as well.
Note: it is important to note you need to use \r\n when specifying a line between header data, just FYI.