PHP mail() returns true but doesn't work
Posted on
16th Feb 2014 07:03 pm by
admin
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:
Code: $email = $_POST['email'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$sentmail = mail("example@gmail.com",$subject,$message,"FROM: ".$email."rn");
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:
Code: ini_set("sendmail_from", "example@gmail.com");but it made no difference.
I finally decided to try a different approach using SMTP:
Code: 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?
No comments posted yet
Your Answer:
Login to answer
314
21
Other forums
Ignore html and bbcode?
I've created a function to limit the amount of text according to the amount of words.
However
Php - mysql store data and use it later?
Hi, i' making a login page at the moment, however my username + password is stored many different pl
Form a inline view based on the results of the previous query?
Hi Experts,
Can we form a inline view on the results of a previous query (another inline view)?
file_put_contents and createimagefromjpeg
All,
I do somethings and then I have the following code:
file_put_contents('./test/'.$filename
Members Only
Hi all, for my website i have a members area only which on members can veiw, but at the moment anyon
default SAP userid
hi,, I just like to know if it is ok to use the default SAP user id (SAP*)?
read integers that returns a negative or prints the average
Im trying to finish this homework assignment by tommarow afternoon and I keep getting stuck.
Wr
Convert Binary String to Decimal
Trying to Get:
Decimal: 305419896
Out of:
Binary String: xV4
Unifying logins of two different scripts
I have two different game scripts which I want to embed on my own site. The problem is I don't want
Please help understand this code
I noticed the index page on my site was modified this morning and found this code inserted at the bo