Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 10th, 2003, 04:26 AM
Registered User
 
Join Date: Sep 2003
Location: St. Albans, Herts., United Kingdom.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Form -> PHP -> email - how hard can it be??

I really hope someone can help me!

I'm "evolving" an existing PHP script which turns form content into an email. The problem I'm having is that when it works, all data is on one, long, line when it arrives in an email. If I try to use "/n" and "/r" to format the email from the PHP, the email never arrives (in one circumstance, the browser doesn't get past the PHP to display the "your email has been sent" page).

I'm still trying to get to grips with PHP and work seems to have thrown me in at the deep end with this one.

Can anyone help? Please, anyone??

------------------------------------------------

But where's the RUM??
Reply With Quote
  #2 (permalink)  
Old September 10th, 2003, 04:36 AM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

You need to format the newline character as '\n' and not '/n' and the same for carriage returns '\r' not '/r'.. but to break up a body you don't really need to use carriage returns. I'm not entirely mail savvy, but I do know that when used in some contexts carriage returns have special meaning to an SMTP server. And using them in a mail body may or may not cause an error. *not sure*

The body should be structured like this (for plain text)

$mail_body = "Some content";
$mail_body .= "Some more content";

If you attempt to do this:

$mail_body = "Some content
              Some more content";

All of the space characters will remain and the formatting will likely not be what you intended.

So yes... use \n instead and you should get the desired result.

: )
Rich


:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
  #3 (permalink)  
Old September 10th, 2003, 09:10 AM
Registered User
 
Join Date: Sep 2003
Location: St. Albans, Herts., United Kingdom.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your help Rich. The slash was my mistake, I have actually been using backslashes and not forward. Guess I'm reverting to my DOS days! :)

I'll try your suggestion out and post back with a result!

------------------------
But where's the RUM??
Reply With Quote
  #4 (permalink)  
Old September 10th, 2003, 09:50 AM
Registered User
 
Join Date: Sep 2003
Location: St. Albans, Herts., United Kingdom.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I've checked the script out Rich, but can't see a problem. I've pasted below and would welcome any comments you could offer.

This has been modified quite a bit from original.

The variables that are called in the "$mess#" lines are the names of the form inputs.

Thanks!

<?php
//peon.incrimina.com
//you only need to edit the $to variable for this to work, but feel free to edit the rest if you feel the need.

$to = "nospam@thankyou.com";

$extra = "From: $email\r\nReply-To: $email\r\n";
$subject = "Request for Guide";

$mess = "This request for a copy of the Guide is from ".$contact." (Membership No: ".$memnum.")";
$mess2 = "Name of Business: ".$busname.;
$mess3 = "Address: ".$address." ".$postcode.;
$mess4 = "Telephone No: ".$telephone.;


mail ($to, $subject, $mess, $mess2, $mess3, $mess4, $extra, $contact, $memnum, $address, $busname, $telephone, $postcode);

echo("Your details have been sent. Thank you for contacting us.<br><br><a href=\"".$HTTP_REFERER."\">Home</a>\n");

?>

------------------------
But where's the RUM??
Reply With Quote
  #5 (permalink)  
Old September 10th, 2003, 01:05 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Wait a minute... look at the parameters you're sending to mail()! What is all that stuff?

Take a look at the manual: http://www.php.net/function.mail

This is the function declaration/signature for mail()

bool mail ( string to, string subject, string message [, string additional_headers [, string additional_parameters]])


That means that mail() returns a boolean TRUE or FALSE to signify success in sending the message.

You're passing a lot of extra parameters to the function. Let's compare each parameter you pass to what the function actually expects.

Code:
  You                PHP
 $to                to        // ok!
 $subject          subject     // ok!
 $mess             message     // ok!
 $mess2          addl headers  // NOT OK!
 $mess3          addl params   // NOT OK!
 $mess4                        // NOT OK!
 $extra                        // NOT OK!
 $contact                      // NOT OK!
 $memnum                       // NOT OK!
 $address                      // NOT OK!
 $busname                      // NOT OK!
 $telephone                    // NOT OK!
 $postcode                     // NOT OK!
You're passing 8 more parameters to the function than it expects. Only ONE parameter is supposed to contain the entire message body, so you need to format your entire message as a single string before you send it to mail.


$msg = "this request for a copy of the Guid is from {$contact} (Membership No: {$memnum})\n";
$msg .= "Name of Business: {$busname}\n"; // see, i'm appending to $msg
$msg .= "Address: {$address} {$postcode}.\n"; // |
$msg .= "Telephone No: {$telephone}.\n"; // V

$ret = mail($to, $subject, $msg); // no extra headers or params necessary

if ($ret)
{
echo "Mail successfully sent.\n";
}
else
{
echo "Error sending message!\n";
}



Take care,

Nik
http://www.bigaction.org/
Reply With Quote
  #6 (permalink)  
Old September 11th, 2003, 04:16 AM
Registered User
 
Join Date: Sep 2003
Location: St. Albans, Herts., United Kingdom.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks ever so much for your help Nik, it makes a little bit more sense to me now. I've implemented your amended script and the email now arrives! The only problem is, it hasn't passed any of the form data to the email! I've double checked the form input names and the parameter names that PHP is calling and they match, but for some reason the email displays all the text that it should do, except the contents of the parameters!

I'm checking out the site you mentioned in search of answers, but I'd definitely be grateful for any thoughts you might have.

Very appreciative,
John

------------------------
But where's the RUM??
Reply With Quote
  #7 (permalink)  
Old September 11th, 2003, 12:39 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Check your register_globals setting. If it's off, you'll need to access your form data through either the $_GET or $_POST superglobal arrays. For example, $busname should actually be $_GET['busname'].


For more info on register_globals, read my old FAQ thread here:
  http://p2p.wrox.com/archive/beginnin...2002-11/17.asp

There are several links to relevant pages in the PHP manual at the bottom of that post.

For more information on types of PHP strings and how variable substitution works, read:
  http://www.php.net/types.string


Take care,

Nik
http://www.bigaction.org/
Reply With Quote
  #8 (permalink)  
Old October 6th, 2003, 02:00 PM
Authorized User
 
Join Date: Jun 2003
Location: San Jose, CA, USA.
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

FYI, you may also have the mail being sent as HTML in which case, /n's and /r's won't do a damn thing. Try appending a <P> or <BR> tag to the end of those lines and see if it helps.

Also, if your stuck, I found a GREAT mail class that works very well. Here's the website. Look for SZMail class.

http://www.seduction747.com


----------
~cmiller
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Chpt 7 >> Pg 245 >>Try It Out #4-5 harrison4411 BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 0 March 2nd, 2006 06:26 PM
Achitecture ? SQL -> XML -> ASP -> PDF or HTML Frm jstrong Classic ASP XML 0 July 9th, 2005 01:18 PM
Chapter 4> ERROR using "=>>>" guiro BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 5 January 13th, 2005 06:38 PM
VB.Net -> Filename -> DTS Package -> tempdB daniel Pro VB.NET 2002/2003 1 October 7th, 2004 01:46 PM
php.ini -> regisiter globals -> On = BAD? troinfo Beginning PHP 7 July 21st, 2003 02:48 PM



All times are GMT -4. The time now is 01:16 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.