Page 1 of 1

PHP mail() question

Posted: Tue Aug 22, 2006 5:43 pm
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>

Posted: Tue Aug 22, 2006 5:52 pm
by Chris Corbyn
Hmm... maybe \r\n as the EOL. Sounds more like the situation where the mail is being parsed as HTML though :?

Posted: Tue Aug 22, 2006 5:54 pm
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?

So...

Posted: Tue Aug 22, 2006 5:56 pm
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.

Re: So...

Posted: Tue Aug 22, 2006 6:13 pm
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.

Posted: Tue Aug 22, 2006 6:17 pm
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 "||"

Posted: Tue Aug 22, 2006 6:19 pm
by Chris Corbyn
I do like your new avatar ~feyd... I wish I could do cool Graphics like that :cry:

Thanks for the help on my code feyd

Posted: Tue Aug 22, 2006 6:29 pm
by akimm
I'll make the suggested fixes.


Now d11 with the /r /n I don't know exactly how i should apply thoses.

Re: Thanks for the help on my code feyd

Posted: Tue Aug 22, 2006 6:35 pm
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.

Thanks

Posted: Wed Aug 23, 2006 12:08 am
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?

Re: Thanks

Posted: Wed Aug 23, 2006 12:19 am
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?

well

Posted: Wed Aug 23, 2006 12:26 am
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.

Posted: Wed Aug 23, 2006 12:33 am
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.