Can't figure out Mail()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nic
Forum Newbie
Posts: 3
Joined: Thu Mar 23, 2006 11:04 pm

Can't figure out Mail()

Post by nic »

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hey - a seemingly very simple issue is giving me more trouble than it should be. I'm trying to get php to send an email. Here's my code:

Code: Select all

$to = 'example@host.com';
$subject = 'the subject';
$message = 'hello';
$headers = "From: example@host.com\r\n
			Reply-To: example@host.com\r\n
			X-Mailer: PHP/" . phpversion() ;

if(@mail($to, $subject, $message, $headers))
	echo "mail was sent successfully" ;
else
	echo "&%*@!!" ;
I'm running an Apache web server on an OSX machine and I changed the following lines in my php.ini file:

Code: Select all

;sendmail_from = example@host.com
;sendmail_path = /usr/sbin/sendmail -t
The mail function seems to run without any problem, but the message never reaches the destination. Any thoughts?


feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

take the ; off the beginning of the lines and remove the @ infront of mail()

put this at the top of your script

Code: Select all

ini_set("display_errors","on");
error_reporting(E_ALL);
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
nic
Forum Newbie
Posts: 3
Joined: Thu Mar 23, 2006 11:04 pm

Post by nic »

well here's my code now:

Code: Select all

<?php

ini_set("sendmail_path", "/usr/sbin/sendmail -t") ;
ini_set("display_errors", "on") ;
error_reporting(E_ALL) ;

$to = "example@host.com" ;
$subject = "the subject" ;
$message = "testing testing" ;
$headers = "From: example@host.com\r\nReply-To:example@host.com\r\n" ;

if(mail($to,$subject,$message,$headers))
	echo "sent" ;
else 
	echo "not sent" ;

?>
and I can't get it to generate an error, nor can I get mail() to return false with any parameters. What, besides php.ini, needs to be configured in order for the mail() function to work? Why is php seemingly not communicating at all with sendmail?

thanks
Post Reply