PHP mail() function

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

toniknik1982
Forum Newbie
Posts: 12
Joined: Mon Oct 17, 2011 7:01 pm

PHP mail() function

Post 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>
Last edited by Benjamin on Tue Nov 15, 2011 2:26 am, edited 1 time in total.
Reason: Added [syntax=php||htm||css||javascript||sql||etc] - Please use [syntax] tags when posting code in the forums! Thanks.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP mail() function

Post 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
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
toniknik1982
Forum Newbie
Posts: 12
Joined: Mon Oct 17, 2011 7:01 pm

Re: PHP mail() function

Post 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...
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP mail() function

Post 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?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
toniknik1982
Forum Newbie
Posts: 12
Joined: Mon Oct 17, 2011 7:01 pm

Re: PHP mail() function

Post by toniknik1982 »

only blank page and as i checked on the recipient's email no email received....
mikeashfield
Forum Contributor
Posts: 159
Joined: Sat Oct 22, 2011 10:50 am

Re: PHP mail() function

Post 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
toniknik1982
Forum Newbie
Posts: 12
Joined: Mon Oct 17, 2011 7:01 pm

Re: PHP mail() function

Post by toniknik1982 »

thanks mike, how am i going to do that?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP mail() function

Post 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.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
toniknik1982
Forum Newbie
Posts: 12
Joined: Mon Oct 17, 2011 7:01 pm

Re: PHP mail() function

Post by toniknik1982 »

on my page it display "Sent", I checked on the mail recipient nothings arrived.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP mail() function

Post by social_experiment »

Then the code is working; follow the advice from mikeashfield, have a look at your junk mail folder
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
mikeashfield
Forum Contributor
Posts: 159
Joined: Sat Oct 22, 2011 10:50 am

Re: PHP mail() function

Post 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. :)
toniknik1982
Forum Newbie
Posts: 12
Joined: Mon Oct 17, 2011 7:01 pm

Re: PHP mail() function

Post 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?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP mail() function

Post by Celauran »

The mail function is returning TRUE, so PHP isn't the problem. I recommend checking your mail server's error logs.
toniknik1982
Forum Newbie
Posts: 12
Joined: Mon Oct 17, 2011 7:01 pm

Re: PHP mail() function

Post 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
mail settings in joomla
. Here's my global settings in joomla you might see for reference
toniknik1982
Forum Newbie
Posts: 12
Joined: Mon Oct 17, 2011 7:01 pm

Re: PHP mail() function

Post 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.
Post Reply