If the message Email sending failed
always appears and the false
to which you refer is the mail
function, then the reason is the configuration of your server.
Note that if you are trying to use this in http://localhost
and not a real server the mail
function will not work and will always always return false
, if you are trying to configure your own server you will need to configure SMTP in php .ini:
-
Windows
If it is Windows you will need an application called sendmail
which is usually already installed with linux (in Windows sendmail
comes with the Xampp package (Wamp / zwamp / easyphp does not have such software in the package) p>
-
Search and edit sendmail.ini
(usually in Xampp for windows C:\xampp\sendmail
):
[sendmail]
smtp_server=smtp.gmail.com
smtp_port = 587
default_domain = gmail.com
auth_username=[seuemail]@gmail.com
auth_password=[suasenha]
-
Edit php.ini
:
[mail function]
sendmail_path = "C:\xampp\sendmail\sendmail.exe -t"
SMTP = smtp.gmail.com
smtp_port = 587
-
Mac OSX
In Mac OSX Yosemite I think it's something like (as is the response from SOen ):
[mail function]
sendmail_path = "env -i /usr/sbin/sendmail -t -i"
SMTP = smtp.gmail.com
smtp_port = 587
-
Setting up Gmail
To use Gmail, simply go to the link link and to free access select Active strong> where Access to less secure applications is written.
For more details on Gmail follow the instructions in this link .
Alternative
Alternatively, you can use PHPMailer (you must release the access as described above), for example:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '[email protected]'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, 'ssl' also accepted
$mail->Port = 587; // TCP port to connect to
$mail->From = '[email protected]';
$mail->FromName = 'Mailer';
$mail->addAddress('[email protected]', 'Joe User'); // Add a recipient
$mail->addAddress('[email protected]'); // Name is optional
$mail->addReplyTo('[email protected]', 'Information');
$mail->addCC('[email protected]');
$mail->addBCC('[email protected]');
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
For other issues with PHPMailer, read the link PHPMailer / wiki