468,278 Members | 1,580 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,278 developers. It's quick & easy.

Sending mail with php

php mail... I did this ages before but I'm having problems with it.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. //--- Start mail script ---
  4.  
  5. $to = "address@gmail.com";
  6.  
  7. $from = "address@gmail.com";
  8.  
  9. $subject = "Please work";
  10.  
  11. $body = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.";
  12.  
  13. $header = "Blah blah";
  14.  
  15. $success = mail($to, $subject, $body, $header);
  16.  
  17. if(!$success) {
  18.     echo "error";
  19. } else {
  20.     echo "success";
  21. }
  22.  
  23. //--- End mail script ---
  24.  
  25. ?>
  26.  
  27.  
Can anyone explain why this won't work. I keep getting my "error" echo.
Oct 27 '06 #1
10 1871
ronverdonk
4,258 Expert 4TB
The from should go into the 4th parameter (also called the header).
[PHP]<?php
//--- Start mail script ---
$to = "address@gmail.com";
$from = "address@gmail.com";
$subject = "Please work";
$body = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.";
$header = "Blah blah";
$success = mail($to, $subject, $body, $from);
if(!$success) {
echo "error";
} else {
echo "success";
}
//--- End mail script ---
?>[/PHP]

Ronald :cool:
Oct 27 '06 #2
For some reason this still doesn't seem to work.

I even tried putting in the parameter without using variables it just won't work : (
Oct 27 '06 #3
LacrosseB0ss
113 100+
I've never seen it assigned to a variable before. Whenever I've sent an e-mail or seen code for it it's always looked like:

[php]
$to = someone@domain.com
$subject = //whatever the subject is
$text = //body text of the e-mail
$headers = //e-mail headers

mail($to, $subject, $text, $headers);
[/php]

As I said, I haven't seen it been assigned to a variable before. I find it hard to believe that could be the problem. But I also find it hard to believe ronverdonk can be wrong.

Hope this helps
LB
Oct 27 '06 #4
ronverdonk
4,258 Expert 4TB
I am very sorry, I made a mistype in the code I showed. I forgot to prefix the sender with the literal 'From: '. The following is correct, so it should be okay for you now.
[php]<?php
//--- Start mail script ---
$to = "address@gmail.com";
$from = "From: address@gmail.com";
$subject = "Please work";
$body = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.";
$header = "Blah blah";
$success = mail($to, $subject, $body, $from);
if(!$success) {
echo "error";
} else {
echo "success";
}
//--- End mail script ---
?>
[/php]
Ronald :cool:
Oct 27 '06 #5
sender's email address should be appended in header
so you code should look like this
Expand|Select|Wrap|Line Numbers
  1. $to="email@domain.com
  2. $subject = "this i subjec\r\n";
  3. $headers = "From: receipt@domain.com\r\n";
  4. $headers .= "Reply-To:receipt@domaincom\r\n";
  5. $headers .= "Return-Path: receipt@domain.com\r\n";
  6. $body= "type your messege here ";
  7.  
  8. mail("$to","$subject","\n$body", "$headers\r");
Oct 27 '06 #6
ronverdonk
4,258 Expert 4TB
$to="email@domain.com
$subject = "this i subjec\r\n";
$headers = "From: receipt@domain.com\r\n";
$headers .= "Reply-To:receipt@domaincom\r\n";
$headers .= "Return-Path: receipt@domain.com\r\n";
$body= "type your messege here ";

mail("$to","$subject","\n$body", "$headers\r");
From the mail settings in this case only the 'From: ' is required in the 4th mail parameters (also called headers in the docs). The other header content is nice but NOT mandatory.

Ronald :cool:
Oct 27 '06 #7
IT WON'T WORK!!! Aaargh! why?!?
even copying and pasting, won't work!?!

Expand|Select|Wrap|Line Numbers
  1.  
  2. $to = "email@gmail.com";
  3. $subject = "Subject";
  4. $body = "Body text and such stuff";
  5. $header = "From: email@gmail.com";
  6.  
  7. $success = mail($to, $subject, $body, $header);
  8.  
  9. if(!$success) {
  10.     echo "error";
  11. } else {
  12.     echo "sent";
  13. }
  14.  
  15.  
Now from what I gather this should work provided that "email@gmail.com" is replaced with a valid address. But it doesn't! I keep getting "error". I even tried it without assigning the function to variable $success... same story. I don't understand what sort of minute syntax tithle is missing here.

I appriciate all the help, guys though this is UNIMAGINABLY trying :(
Oct 29 '06 #8
Okay... I ran phpinfo() and under the section called Configuration PHP CORE I got this...

disable_functions: getmyinode, getopt, getrusage, extension_loaded, dl, mysql_pconnect, crack_check, crack_closedict, crack_getlastmessage, crack_opendict, fsockopen, pfsockopen, mysql_list_dbs, mysql_stat, ini_get, ini_get_all, ini_alter, ini_set, get_current_user, get_defined_constants, get_include_path, php_ini_scanned_files, php_uname, phpcredits, restore_include_path, set_include_path, set_time_limit, version_compare, zend_version, getmypid, getmyuid, getmygid, assert_options, assert, fopen, fwrite, fread, file, fpassthru, file, mail, opendir, readdir, closedir

This seems to be getting more complicated than i care for. Can anyone please tell me why are some functions disabled and why is mail one of those functions and how can i enable some of these function or can i not.

ronverdonk?
Oct 29 '06 #9
ronverdonk
4,258 Expert 4TB
You most probably are running PHP in safe mode. That mode is set at installation of PHP. This mode disables the functions you mention, see also the list of disabled functions at Functions restricted/disabled by safe mode
The PHP safe mode is an attempt to solve the shared-server security problem. It is architecturally incorrect to try to solve this problem at the PHP level, but since the alternatives at the web server and OS levels aren't very realistic, many people, especially ISP's, use safe mode for now.
See the PHP manual for a discussion on safe mode at Safe Mode

If you installed PHP yourself, remove the safe mode. If not, you'd better have a talk with your server provider.

Ronald :cool:
Oct 29 '06 #10
Okay, thanks. I've emailed my server provider... they've promised a quick response so I'll see.

At least I can now stop bashing my head against the wall.
Oct 29 '06 #11

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by praba kar | last post: by
10 posts views Thread by Stuart Mueller | last post: by
2 posts views Thread by Mr. x | last post: by
3 posts views Thread by mfleet1973 | last post: by
4 posts views Thread by =?Utf-8?B?R3V5IENvaGVu?= | last post: by
reply views Thread by zattat | last post: by
1 post views Thread by MrBee | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.