Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 10th, 2003, 04:26 AM
Registered User
 
Join Date: Sep 2003
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??
 
Old September 10th, 2003, 04:36 AM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
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
:::::::::::::::::::::::::::::::::
 
Old September 10th, 2003, 09:10 AM
Registered User
 
Join Date: Sep 2003
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??
 
Old September 10th, 2003, 09:50 AM
Registered User
 
Join Date: Sep 2003
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 = "[email protected]";

$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??
 
Old September 10th, 2003, 01:05 PM
Friend of Wrox
 
Join Date: Jun 2003
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/
 
Old September 11th, 2003, 04:16 AM
Registered User
 
Join Date: Sep 2003
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??
 
Old September 11th, 2003, 12:39 PM
Friend of Wrox
 
Join Date: Jun 2003
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/
 
Old October 6th, 2003, 02:00 PM
Authorized User
 
Join Date: Jun 2003
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





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.