Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
| Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP 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 December 13th, 2003, 01:17 AM
pi pi is offline
Registered User
 
Join Date: Dec 2003
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default mail() help needed

Hi,
I am new to PHP. I am trying to get a simple script to send me an email notification from my web site hosted by mydomain. From the books and on-line tutorials it seems all I need is:

mail("name@domain", "subject", "message text");

Is there more to it? mail() returns FALSE and I get no email. If it is relevant, from the phpinfo(): I have php 4.3.0 running on Linux and Apache 1.3.27.
Thanks,
pi
 
Old December 13th, 2003, 09:26 AM
Authorized User
 
Join Date: Jun 2003
Location: , , .
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

[quote]mail("name@domain", "subject", "message text");[quote]
I am not sure but shouldn't you have a from: at the end to let the recepient know where you have sent the mail from, like this

mail("name@domain.com","subject","message text","From: you@yoursite.com");

try that as that is how mine works and it is fine.

Sami
 
Old December 13th, 2003, 12:53 PM
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

Quote:
quote:
mail("name@domain.com","subject","message text","From: you@yoursite.com");
The 4th parameter is optional, when the From field is unspecified most generally this is filled in with a default value by SendMail.

What errors are you seeing? Do you have error_reporting set to E_ALL?

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old December 14th, 2003, 03:30 AM
pi pi is offline
Registered User
 
Join Date: Dec 2003
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks guys!
I have tried it with and without "from: " string. This is the entire damn script:
<?php

     error_reporting (E_ALL);

     $ret = mail("name@domain","test","123","From : webmaster@domain\r\n");

    if ($ret)
       echo "sent";

    else
       echo "not sent";

?>

All that I see when I run it is "not sent" in my browser. Should I expect errors to be reported there, or is there a better mechanism to debug? My hunch is that something is not set up on the web hosting side of things, but since I am totaly new to php I could not be sure that I am not making some elementary mistake. Thanks so much, any help is greatly appreciated.
pi

 
Old December 14th, 2003, 12:20 PM
Authorized User
 
Join Date: Jun 2003
Location: , , .
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
Yes quesadilla5 is completely correct, the fourth parameter is optional, sorry about that. But I think you are missing some curly braces and I think your script should read like this:

<?php

     error_reporting (E_ALL);

     $ret = mail("mail@domain.com","test","123","From : webmaster@domain\r\n");

    if ($ret)
      {
       echo "sent";
      }

    else
      {
       echo "not sent";
      }

?>

It is like this simple example:

if(this is true)
   {
    do this
   }
else
   {
    do this instead
   }


Sami
 
Old December 14th, 2003, 02:46 PM
pi pi is offline
Registered User
 
Join Date: Dec 2003
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks, Sami.
Tried it with the braces, same result: "not sent" in the browser, no error messages, no email. I come to php from C, where the braces would be optional for just one istruction after if and else, so I just assumed it would be the same... At any rate, my real problem is not getting the email sent and not having eny errors reported to give me any clue as to what is going on. Perhaps, I'll try to bang my head against a wall... ok, helped a little, I think....
pi

 
Old December 14th, 2003, 06:56 PM
Authorized User
 
Join Date: Nov 2003
Location: , , Australia.
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello,

I set the mail parameters into variables that come from a form (in my page not this example) this allows me to build up the body of the email from multiple text boxes.
<?php
$mail_to = "me@somedomain.com";
$mail_subject = "Emails subject";
$mail_body = "blah blah blah";
if (mail($mail_to, $mail_subject, $mailbody))
   echo "Email Sent";
else
   echo "An error has occored while trying to send this email";
?>

i dont have any braces and the script works so maybe PHP is like C when theres only one instruction in the script?

The one problem i did find with sending emails is when the page was hosted on my own pc (eg using localhost) i was unable to send emails even when connected to the internet, however when i put the page onto my ISP's site the email was sent.

Hope this helps

Chris



 
Old December 14th, 2003, 08:15 PM
pi pi is offline
Registered User
 
Join Date: Dec 2003
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks, Chris,
I do intend to do this in the actual script I am trying to write, and I have tried it both ways in my test script. Still no luck. Insidently from what else I read about mailing with php, it seems that the procedure for sending mail is more complicated if your webserver runs on top of windows (might that be your case). Some global variables have to be set before sending, or something to that effect. My ISP runs Linux, so I did not bother to go into details of this. I am really counting on them coming to work on Monday finding my email to them, saying "Ah, right" and setting whatever is not set up there correctly. Otherwise, I have no clue as to what else to do about it.
pi

 
Old December 15th, 2003, 03:27 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

Quote:
quote:Sami wrote:

But I think you are missing some curly braces and I think your script should read like this...
Its true that in PHP curly braces become optional if the conditional statement to be executed contains only one line... so...

if (mail($mail_to, $mail_subject, $mailbody))
   echo "Email Sent";
else
   echo "An error has occored while trying to send this email";

...is perfectly acceptable. When the conditional statement contains greater than 1 line the curly braces are required as delimiters since the program won't know where you intend the conditional statement to terminate.

On to Pi's problem..

Pi.. it would seem to me that your ISP doesn't have sendmail set up properly, or the path provided to php.ini to the Sendmail application could be invalid... or worse yet they may have blocked access to the mail function. I've never tried to execute a call to the mail function on a server in which Sendmail was not set up properly so I wouldn't know if that would trigger an error in PHP. One would think so. So at this point its best to check with your ISP or Server Admin. You are setting up the function call just as you should. The from field is irrelavent at this point, of course it implies a well built message, but it is optional.

If for some reason your ISP doesn't allow the mail function, then perhaps you could use an SMTP mailer class. There are several floating about the Net and I beleive one is included in PEAR. Not too difficult to set up. Let us know what your ISP says.. and if you need help setting up an SMTP class just let us know.

You can also do a check on your own to see if the mail function is blocked by running phpinfo() .. and this may appear under disable_functions. You might also make note of what appears under the sendmail_path entry and inquire with your ISP if that is the valid path.

Best of luck!
: )
Rich



:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old December 15th, 2003, 03:39 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

Oh and for Chris's problem:
Quote:
quote:
The one problem i did find with sending emails is when the page was hosted on my own pc (eg using localhost) i was unable to send emails even when connected to the internet, however when i put the page onto my ISP's site the email was sent.
In Windows (Just assuming that's what you're using) you must explicitly enter an SMTP server for PHP to send mail. This can be either a remote SMTP server e.g. smtp.mydomain.com or you may install an SMTP server on Windows and refer to it via localhost or computer name. I like the latter option and use Post Cast Server, which is a free fully featured SMTP server for Windows. Available at.. http://www.postcastserver.com.

Unfortunately specifing an SMTP server in this way does not work for PHP running on Linux.

Snip from php.ini
[mail function]
; For Win32 only.
SMTP = localhost ; for Win32 only

; For Win32 only.
sendmail_from = me@mydomain.com ; for Win32 only

; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
;sendmail_path =

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::




Similar Threads
Thread Thread Starter Forum Replies Last Post
Send mail and attachments with PHP mail function Lofa Beginning PHP 1 June 2nd, 2008 03:24 PM
problems sending mail with java mail gandacuboy J2EE 2 December 20th, 2006 03:05 PM
Sending e-mail to different mail box! Calibus Classic ASP Databases 4 September 3rd, 2004 05:48 PM
Sending both text mail and HTML mail - CDONTS madhukp Classic ASP Basics 1 October 8th, 2003 01:05 AM





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