问题
This code seems to work in Xampp on my mac localhost but it doesn't seem to work on my free 000webhost account. I already tried getting rid of SSL but it didn't help. Keep in mind that this program get variables from an external form.
<?php
$subject = htmlentities($_POST['subject']);
$email = $_POST['email'];
$message = htmlentities($_POST['message']);
require_once 'PHPMailer/PHPMailerAutoload.php';
$m = new PHPMailer;
$m->isSMTP();
$m->SMTPAuth = true;
//$m->SMTPDebug = 1;
$m->Host = 'smtp.gmail.com';
$m->Username = 'email-adress@gmail.com';
$m->Password = 'password';
$m->SMTPSecure = 'ssl';
$m->Port = 465;
$m->From = 'email-adress@gmail.com';
$m->FromName = 'William Green';
$m->addReplyTo('email-adress@gmail.com', 'William Green');
//$m->addCC('email2-address@gmail.com', 'Willliam green');
########################################
//email code
//$recipient = strip_tags($_POST['mailRecipient']);
//$name = strip_tags($_POST['recipientsName']);
$m->addAddress($email, $email);
//$m->Subject = strip_tags($_POST['mailSubject']);
//$m->Body = strip_tags($_POST['mailBody']);
$m->Subject = $subject;
$m->Body = $message;
//$m->AltBody = 'plain text version!';
###########################################
//var_dump($m->send());
if ($m->send())
{
echo '<h1 class="good">Email Sent!</h1>';
}
else
{
echo '<h1 class="bad">Email Not Sent!</h1>';
}
?>
回答1:
"but it doesn't seem to work on my free 000webhost account"
A: They do not offer SMTP for free hosting. Use PHP's mail() function instead.
Notice the red x
next to SMTP Server on their site => http://www.000webhost.com/ ?

Either use mail() or pay for it in order to use SMTP.
You can do it on their website, or upgrade from within the control panel.
Edit (Sept. 16, 2016): This answer was posted before they revamped their website. The URL where you can see the available options for free/paid services, can be see here https://www.000webhost.com/premium-web-hosting
This also goes for remote connections to a database.
This answer was edited in regards to a question I saw today which was related to my edit.
- https://stackoverflow.com/q/39541929/1415724
回答2:
Check that the 465 port is open. If not, the host provider need to change the settings.
来源:https://stackoverflow.com/questions/25340966/phpmailer-not-working-on-000webhost