mail() function woes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
influx
Forum Commoner
Posts: 31
Joined: Fri Aug 05, 2005 9:28 am

mail() function woes

Post 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]
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post 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.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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.
Post Reply