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
PHP hyperlinks generator - HELP plz
Hi
I need some help to get this done using php:
1 - I have few hyperlinks say 500
Transport data between itab and textfield on ALV event
Hallo,
I have a ALV Grid ( cl_gui_alv_grid ) and I also have hotspot click event with a h
mySQL and PHP search
Hello,
I am trying to code a project and ran into a brick wall with one of my pages. I am pretty
Help on code output
My CODE:
Code: [Select] echo "<phone>".$line["phone"].&qu
PHP Programming error. Please help!
Hi there,
Am making a website for a friend and have encountered a problem that i need fixing
I need some help with the IF command...
I have a large number of images in one section of my website with the .jpg format. I'm adding a bun
Files in current folder. Should be an easy fix.
Never mind. I've asked about this before and just found my answer. Anyway to delete this?
uploading video files into mysql in php
hi,
I have the video files which i have to upload into mysql db. along with creating thumbnails f
Xacute search within SQL results
I have a query that is pulling data, and I want to return a specific value from the results of that
Executing an exe
Hi. I have some bizarre problem with exec (other any other method) to launch exe's. I am making a ga