Page 1 of 2
PHP mail() function
Posted: Tue Nov 15, 2011 12:42 am
by toniknik1982
mail() function in PHP won't work, PLEASE help me need this for my website form. I just created a simple code for this function and save it as mymailer.php, on my system I have ubuntu11.04 with LAMP installed for my webserver. Here's my code.
mymailer.php
Code: Select all
<html>
<body>
<?php
$to = "myemail@yahoo.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "somebody@gmail.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
</body>
</html>
Re: PHP mail() function
Posted: Tue Nov 15, 2011 12:54 am
by social_experiment
toniknik1982 wrote:mail() function in PHP won't work
I'm guessing the mail wont send? And since there is no error reported by you, your mail server must be setup correctly. Just incase i'm wrong, do you receive any error messages?
Code: Select all
<?php
// to test if the mail function does indeed function
mail($to,$subject,$message,$headers) or die('Mail not sent');
//
$to = "myemail@yahoo.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "somebody@gmail.com";
// changed this header
$headers = "From: " . $from;
mail($to,$subject,$message,$headers) or die('Mail not sent');
?>
Try the above code; you'll see i only modified the
$headers variable by adding a space after the colon, this is what most of the examples look like (ones i found). Lastly, use the PHP Code button to wrap your php code, it makes reading the code alot easier than a different color
Hth
Re: PHP mail() function
Posted: Tue Nov 15, 2011 4:21 am
by toniknik1982
I try your code and it says that "Undefined variable: headers in /home/user/Documents/php_test/mymailer.php on line 6". What should I do next? I'm a newbie on this...
Re: PHP mail() function
Posted: Tue Nov 15, 2011 4:31 am
by social_experiment
Code: Select all
<?php
$to = "myemail@yahoo.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "somebody@gmail.com";
// changed this header
$headers = "From: " . $from;
mail($to,$subject,$message,$headers) or die('Mail not sent');
?>
Use this only, it works properly. Do you receive any error messages if you use this code?
Re: PHP mail() function
Posted: Tue Nov 15, 2011 4:48 am
by toniknik1982
only blank page and as i checked on the recipient's email no email received....
Re: PHP mail() function
Posted: Tue Nov 15, 2011 4:55 am
by mikeashfield
Make sure it hasn't been filtered by any spam filter. I spent like 3 nights trying to work out why it seemed like it was working, and it didn't show up in the spam counter in Gmail
Re: PHP mail() function
Posted: Tue Nov 15, 2011 5:03 am
by toniknik1982
thanks mike, how am i going to do that?
Re: PHP mail() function
Posted: Tue Nov 15, 2011 5:07 am
by social_experiment
toniknik1982 wrote:only blank page and as i checked on the recipient's email no email received....
Yes it will show a blank page on success as there is no option for a success message to be echoed. To achieve this assign the mail function to a variable and check that
Code: Select all
<?php
$mail = mail($to, $subject, $message, $headers);
if ($mail) { echo 'Sent'; }
else { echo 'Not sent'; }
?>
mikeashfield wrote:Make sure it hasn't been filtered by any spam filter. I spent like 3 nights trying to work out why it seemed like it was working, and it didn't show up in the spam counter in Gmail
Good point, the test email i sent went straight into my junk mail folder.
Re: PHP mail() function
Posted: Tue Nov 15, 2011 5:15 am
by toniknik1982
on my page it display "Sent", I checked on the mail recipient nothings arrived.
Re: PHP mail() function
Posted: Tue Nov 15, 2011 5:20 am
by social_experiment
Then the code is working; follow the advice from mikeashfield, have a look at your junk mail folder
Re: PHP mail() function
Posted: Tue Nov 15, 2011 5:35 am
by mikeashfield
Search your mail client for the term "Hello! This is a simple email message." seeing as that's the body of your e-mail.

Re: PHP mail() function
Posted: Tue Nov 15, 2011 5:53 am
by toniknik1982
I checked on recipients end and sender's end no email such as that... how about on the php.ini file is there anything that i need to configure there?
Re: PHP mail() function
Posted: Tue Nov 15, 2011 5:55 am
by Celauran
The mail function is returning TRUE, so PHP isn't the problem. I recommend checking your mail server's error logs.
Re: PHP mail() function
Posted: Tue Nov 15, 2011 6:02 am
by toniknik1982
BTW... I tried JOOMLA(which i'm not prefer bcoz what's in there is already predefined, i want to start from a scratch) to create my test website and create a test account, the validation was successfully sent to the recipient

- mail settings in joomla
. Here's my global settings in joomla you might see for reference
Re: PHP mail() function
Posted: Tue Nov 15, 2011 6:12 am
by toniknik1982
@celauran: where can i find that? is it something to do with my sendmail? You mean to say log file for my sendmail? because i think that's the mailer i used in ubuntu.... it's my first time doing this code.