PHP mail() question

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
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

PHP mail() question

Post by akimm »

I have this tiny program made to help my friend send poems from a form with 4 variables,
the name of the sender, the senders email, the senders title of their poem, and finally their poem.

My problem is, I want the poem to have line breaks where the poet wants them. But instead when the program reads the mail input it send it all in one line. I was thinking maybe just adding ASCII space "/n" isthis what I want? Or what? Here is my program thus far, form and all.

simple_form.htm

Code: Select all

<form action="send_simpleform.php" method="POST">
<p><b>your name</b>
<input type="text" name="name" size="30"></p>

<p><b>your email</b>
<input type="text" name="sender_email" size="30"></p>

<p><b>your title</b>
<input type="text" name="title" size="30"></p>

<p><b>your poem</b>
<TEXTAREA NAME="poem" COLS="30" ROWS="5" WRAP="virtual"></TEXTAREA></P>

<p><input type="submit" name="submit" value="send your poem!"></p>
Here is the PHP to go with the form.

Code: Select all

<?php 
if ($_POST['name'] = "" && 
    $_POST['sender_email'] = "" && 
    $_POST['title'] = "" && 
    $_POST['poem'] ="") 
{ 
echo "Please fill out all boxes so I can see your great work";
} 
else {
$msg = "E-MAIL SENT FROM THE CHATAHOLIC POETRY PAGE\n"; 
$msg .= "Sender's NAME: \t{$_POST['name']}\n"; 
$msg .= "Sender's EMAIL:\t{$_POST['sender_email']}\n"; 
$msg .= "Sender's TITLE:\t{$_POST['title']}\n"; 
$msg .= "Sender's POEM:\t{$_POST['poem']}\n"; 
$to = "kiddo@chatsaholic.com\n"; 
$subject = "Poetry Submission\n"; 
$mailtoheaders = "From: Malina <kiddo@chatsaholic.com>\n"; 
$mailheaders .= "Reply-To: {$_POST['sender_email']}\n"; 

mail($to, $subject, $msg, $mailheaders); 
}
?> 



<html> 
<head> 
</head> 
<body> 
<h1> the following email has been sent!</h1> 

<P><b>Your Name:</b><br> 
<?php echo $_POST['name']; ?> 
<P><b>Your E-Mail Address:</b><br> 
<?php echo $_POST['sender_email']; ?> 
<P><b>title:</b><br> 
<?php echo $_POST['title']; ?> 
<P><b>poem:</b><br> 
<?php echo $_POST['poem']; ?> 
<FORM> 
<INPUT type="button" value="Click here to go back" onClick="history.back()"> 
</FORM> 
</body> 
</html>
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Hmm... maybe \r\n as the EOL. Sounds more like the situation where the mail is being parsed as HTML though :?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

d11wtq wrote:Hmm... maybe \r\n as the EOL. Sounds more like the situation where the mail is being parsed as HTML though :?
..or the email client removes the line breaks?
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

So...

Post by akimm »

Are you guys suggesting that it is what it is? Perhaps inform the user that to implement line breaks by something like a <br> or perhaps, a pound sign, so the recipient of the mail knows the sender wants a space there..


Or is there a way i'm not aware of to assist me here.

Thank you very much either way.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: So...

Post by Chris Corbyn »

akimm wrote:Are you guys suggesting that it is what it is? Perhaps inform the user that to implement line breaks by something like a <br> or perhaps, a pound sign, so the recipient of the mail knows the sender wants a space there..


Or is there a way i'm not aware of to assist me here.

Thank you very much either way.
What happened when you tried using \r\n in the mail. I know SMTP servers see \r\n as a line ending rather than \n.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Something I noticed earlier: the following code will always set $_POST['name'] to an empty string.

Code: Select all

if ($_POST['name'] = "" &&
    $_POST['sender_email'] = "" &&
    $_POST['title'] = "" &&
    $_POST['poem'] ="")
Add another "=" to each "=" or better yet empty($_POST['name'])
There's also a logic error with the logical operators. The current logic requires that all fields be blank for the error to show. The error you have appears to wish to be shown if any of the fields are blank. IF so, swap "&&" or "||"
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

I do like your new avatar ~feyd... I wish I could do cool Graphics like that :cry:
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Thanks for the help on my code feyd

Post by akimm »

I'll make the suggested fixes.


Now d11 with the /r /n I don't know exactly how i should apply thoses.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Thanks for the help on my code feyd

Post by Chris Corbyn »

akimm wrote:Now d11 with the /r /n I don't know exactly how i should apply thoses.
Note that it's backslash, not forward slash \ not /

Code: Select all

$msg = "E-MAIL SENT FROM THE CHATAHOLIC POETRY PAGE\r\n";
$msg .= "Sender's NAME: \t{$_POST['name']}\r\n";
$msg .= "Sender's EMAIL:\t{$_POST['sender_email']}\r\n";
$msg .= "Sender's TITLE:\t{$_POST['title']}\r\n";
$msg .= "Sender's POEM:\t{$_POST['poem']}\r\n";
$to = "kiddo@chatsaholic.com\r\n";
$subject = "Poetry Submission\r\n";
$mailtoheaders = "From: Malina <kiddo@chatsaholic.com>\r\n";
$mailheaders .= "Reply-To: {$_POST['sender_email']}\r\n";
You should techincally, as a matter of compliancy always use \r\n in headers (i.e. the subject line etc) anyway, although some servers very badly try to fix lone LF to make CRLF.... sadly, their logic is alwfully poor at doing this and you often end up with double line spacing.

If all else fails, open the email with mail2web.com (if you don't know how to view the email source from your mail clinet), then click "View Source" and post us what you get.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Thanks

Post by akimm »

It works perfectly.

With all the suggestions made, except feyd, if you see this I have a quick question..

empty($_POST['name'] = "");

would that check to make sure name is empty?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Thanks

Post by RobertGonzalez »

akimm wrote:It works perfectly.

With all the suggestions made, except feyd, if you see this I have a quick question..

empty($_POST['name'] = "");

would that check to make sure name is empty?
No it won't. It is improper use of empty(). Do something like this...

Code: Select all

<?php
if (empty($_POST['name'])) {
...
}
?>
PS And why are you excluding Feyd?
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

well

Post by akimm »

I mean to say,

Thank you to all,

except, feyd I still have a question. I see how it can be miconstrued, I should probably change it before i offend someone.

And by the way, thank you for this clarifying my mistake.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You know, if you ever want information about a PHP function, if you visit the PHP manual website at http://www.php.net/<FUNCTION_NAME> you should get the manual page for your function.
Post Reply