Jump to content

PHP mail() returns true but doesn't work


jmajeremy

Recommended Posts

First off I apologize if this is a newbie question, and I generally don't like asking questions that have already been asked 1,000,000 times, but I've searched far and wide and cannot find a solution to my problem.

 

Basically I run an experimental server on my home network. I use LAMP (Ubuntu Server, Apache2.2, MySQL5, PHP5).

 

I simply want to take the data from an HTML form and submit it as an e-mail. I tried using the mail() function like this:

$email = $_POST['email'];
$message = $_POST['message'];
$subject = $_POST['subject'];

$sentmail = mail("example@gmail.com",$subject,$message,"FROM: ".$email."\r\n");
if($sentmail) {
	echo 'Your message has been sent!';
} else {
	echo 'Cannot send message.';
}

 

After some googling, I found one solution that said to add this line:

ini_set("sendmail_from", "example@gmail.com");

but it made no difference.

 

I finally decided to try a different approach using SMTP:

require_once "Mail.php";

$from = "Form Data <".$_POST['email'].">";
$to = "My Name <example@gmail.com>";
$subject = $_POST['subject'];
$body = $_POST['message'];

$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "foo@gmail.com";
$password = "bar";

$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}

 

Still not working!

 

This is quite frustrating. It always returns a value of "true" but never actually sends the messages!

 

Maybe it would help if I had a better understanding of what the mail() function actually does. Where does the message go when handled by mail()? Mustn't it go through a mail server at some point? Port 25 is iffy on my setup, so that could be the issue with mail().

 

When using the PEAR method though, I specifically indicate port 465, so that can't be the issue.

 

What am I missing here?

Link to comment
Share on other sites

This is always a hard question to solve when people's mail() function isn't working.

 

It can fall under many categories including, but not limited to, server misconfiguration (it just doesn't send), ISP limitations (can only send in certain ways allowed by ISP) and spam filtration (email was sent, but was, for some reason, blocked).

 

I've no idea what your problem is, but hopefully I've given you some more ideas, or perhaps annoyances, when it comes to worrying about why it's not working. Sorry I can't help out more, but I've never been good at troubleshooting these mail() issues.

Link to comment
Share on other sites

server misconfiguration (it just doesn't send),

I have a fairly standard and basic server configuration. Haven't tried to modify anything to do with e-mail.
ISP limitations (can only send in certain ways allowed by ISP)
That's a distinct possibility. As I said, my ISP doesn't seem to like me using port 25 (I've had issues in the past running mail servers).
and spam filtration (email was sent, but was, for some reason, blocked).

Gmail has a pretty good spam filter... I of course checked my junk mail. Could it be getting blocked altogether at some point and not even show up in my junk mail?

 

 

Thanks for the input.

 

If it makes any difference, I have a dynamic IP address, and I use dyndns. Other ports in use include 21 (FTP), 23 (SSH), 80 (HTTP), 69 (VNC), 50000 (UPnP).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.