Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP mail() function problem

Status
Not open for further replies.

white605

Technical User
Jan 20, 2003
394
US
I'm having a problem using the php mail() function. I've been googling and experimenting for two days trying to figure it out to no avail.

Basically, I'm unable to send an email using the mail() function. The interesting part is that our company website (which we bought from a third party) uses the function and it works just fine. To make matters even more fun, my code works on another webserver. I've checked the php.ini sections on mail, and there dont seem to be any relevant differences.

Here is the relevant part of the company website that works:
Code:
while(list($SID, $Email) = mysql_fetch_array($sql2)) {
				$unsubscribe_link = "<a href='$siteurl/obituaries.php?op=unsubscribe&Email=$Email'>here</a>";
				if(ereg("\[Unsubscribe_Link\]", $mailbody)) $mailbody = ereg_replace("\[Unsubscribe_Link\]", $unsubscribe_link, $mailbody);
					if((@mail($Email, $subject, $mailbody, $mailheader))) {
						//echo "<font color=green>Emailed to $Email</font><br>";
					} else {
						//echo "<font color=red>Email failed to $Email</font><br>";
					}
				}
			}

And here is the code that won't work:
Code:
<?php

$to = "myemailaddress";
$subject = "hello this is a test";
$message = "this is a test email";
$from = "myemailaddress";
$headers = "From: $from";
@mail($to,$subject,$message,$headers);
echo "Mail Sent.";

?>


Any suggestions?

This old world keeps spinning round - It's a wonder tall trees ain't layin' down
 
When you say it doesn't work what do you mean?
Does it not send an email or do you not receive an email?
Does it work if you send the mail to a different address?

Try removing the @ from the mail() function as it's hiding error messages.

You might also want to set error_reporting(E_ALL) temporarily to show what's going on.

--
Tek-Tips Forums is Member Supported. Click Here to donate

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.


 
Removing the drop-dead sign (as in "drop dead, I don't want to see what's wrong) may off course help. Also, you don't check the output as the example did.

There may be a lot of reasons why mail() does not work. For instance, because your mail server requires TLS, passwords, etc. Do a net search for PHPMailer if that is the case.

But first of all, do not suppress the error messages to ask us what goes wrong. Please enable them and tell us.


+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
FoamCow - The simple script does not send out email,changing the address does not help.

Don - Since the mail() works in the first case and sends mail to the intended recipients i dont see any passwords passed to it.

All - Sorry for not giving the errors. I changed the code to:
Code:
<?php

ini_set('display_errors',1);

error_reporting('E_ALL');

echo phpinfo();

$to = "emailaddress@website.com";

$subject = "hello this is a test";

$message = "this is a test email";

$from = "emailaddress@website.com";

$headers = "From: $from";

@mail($to,$subject,$message,$headers)or die("failed");

echo "Mail Sent.";
?>

Php info reports error_reporting E_ALL
and display_errors ON
The return from the code is:
1failed

Im not sure how to intrepret the 1
thanks in advance
wjwjr

This old world keeps spinning round - It's a wonder tall trees ain't layin' down
 
i would guess that this relates to your smtp settings in php.ini. do you have a default smtp server set? or if you are using linux, is the path to the default mta set?

this is the relevant section

Code:
[mail function]
; For Win32 only.
SMTP = localhost
smtp_port = 25

; For Win32 only.
;sendmail_from = me@example.com

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

most likely you're using windows and have left the defaults as is. that means that php is assuming that there is an smtp server active on the webserver and that it should accept outbound traffic. you can test the presence of an smtp server by using telnet. or just examine the services.

if it's a linux box then similar issues arise. you need to configure sendmail and ensure it is running, and then make sure that the outbound smtp server is accepting traffic.
 
For your information, I call @ the drop-dead sign. You are still supressing errors for the most critical part of your problem.

I know 'drop dead sign' does not sound friendly, but I had a colleague who put it in front of about anything. That really is a "drop dead, somebody else can fix it" situation. Hence the name. Remove them. During development at least.


+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
Code:
[!]@[/!]mail($to,$subject,$message,$headers)or die("failed");


As DonQuichote says, remove the [!]@[/!] as it is suppressing errors from that function.

This will then show you if [bjpadie[/b] is correct (as he normally is) ;-)

--
Tek-Tips Forums is Member Supported. Click Here to donate

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.


 
Ok, Thanks for the guidance so far.
I changed the line to remove the @ to read
Code:
mail($to,$subject,$message,$headers) or die("failed email")

and i get nothing returned but:
failed email

There must be another setting in the ini_set's i need to change to display the error message in my browzer. From searching i suspect it may have something to do with
error_log and related setting but not sure.
thanks
wjwjr

This old world keeps spinning round - It's a wonder tall trees ain't layin' down
 
not really. the error suppressor is unlikely to make much difference for the mail() command as the argument's are validly formed. because of the way the mail() command works (and the different manners between different OS's) php does not return particularly helpful messages about the act of sending the mail. although you will be told if you have failed to specify certain ini settings.

in this case, your code will provide as informative a response as not suppressing the error; although in general i agree with the other posters about the use of the @ suppressor in debugging code.

re-read my last post and test the network connectivity and configuration of your mta/smtp gateway. 99.5% sure that this is where your problem lies.
 
jpadie - Using ssh i created a text file
Code:
to:email@address.com
from:email@address.com
subject:testing

This is my message.
and executed it like
/usr/sbin/sendmail -t -i <mail.txt
The mail was sent.
The sendmail_path in php.ini corresponds with the above path
Do this verify that sendmail is configured properly and accepting traffic?
thanks,
wjwjr


This old world keeps spinning round - It's a wonder tall trees ain't layin' down
 
I frequently get the SMTP error messages kicked out to my PHP error log, even when I have the server set to display errors, so you might check those.

You said that this is running on the same server as your website, is it internal to your network or co-located? If it is co-located, is your mail target an address on your mail server or one external to you? I ask because this happened to me: using a server on an external network, I tried to send to an external e-mail (at one of our remote sites on a different domain) using our internal mail server. All my mail was rejected as a relay request since it was from outside to outside.
 
We have tried turning error logging on and did not find any errors displayed in the log files.

We have tested using internal and external email addresses with no success either way.

I have managed to get this error to display along with the phpinfo on the returned page
Code:
Warning: mail() [function.mail]: Could not execute mail delivery program '/usr/sbin/sendmail -t -i' in /var/[URL unfurl="true"]www/vhosts/mydomain.com/httpdocs/mailtest3.php[/URL] on line 23
failed email


This old world keeps spinning round - It's a wonder tall trees ain't layin' down
 
permissions? is the sendmail application executable by the php process?
 
I restarted the server and things came into place and the script works.

I am concerned and do not understand why we could execute the
/usr/sbin/sendmail -t -i <mail.txt command from ssh and it worked but the code we sent with php did not until i restarted apache and all the services. Doesnt make sence to me yet.

Thanks for all the replys and help as i have learned a lot from this exercise
WJWJr.

This old world keeps spinning round - It's a wonder tall trees ain't layin' down
 
the most likely answer is that you (or someone) changed the settings in php.ini for the path to sendmail and did not restart the web server after the change was made. for sapi based use of php the ini file is read only at the server start up. hence the need to restart the server after every change.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top