PHP E-mail Script not working. What's wrong?
Any help would be much appreciated. Here is the code:
<?php include 'header_admin.php'; ?>
<?php
include 'dbc.php';
page_protect();
checkAdmin();
?>
<?PHP
require_once "Mail.php";
$con = mysql_connect("host","username","pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db_name", $con);
$elist = mysql_query("SELECT开发者_开发知识库 cEmail FROM tblUsers WHERE intUserID = '20'");
$from = "FROM E-MAIL";
$subject = $_POST['eSubject'];
$body = $_POST['eMessage'];
$host = "smtp.domain.com";
$port = "465";
$username = "username";
$password = "password";
echo "<div class='entry'>";
if(mysql_num_rows($elist) > 0)
{
while($eresult = @mysql_fetch_array($elist)) {
$headers = array ('From' => $from, 'To' => $eresult['cEmail'], 'Subject' => $subject);
$smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => true, 'username' => $username, 'password' => $password));
$mail = $smtp->send($elist_result['cEmail'], $headers, $body);
}
}
echo "<table align='center'><p>Message successfully sent!</p></table></div>";
mysql_close($con);
?>
I made sure that the smtp settings were correct and I manually checked my mysql query to make sure it's grabbing the e-mail address. also, I did a separate "echo" command to make sure $body and $subject were correct.
Try using the PHP mail function: http://php.net/manual/en/function.mail.php
You use there $elist_result['cEmail']
, if it differs from $eresult['cEmail']
, where does it come from?
Its not obvious from the code posted above.(guess it's just a typing error)
The $smtp->send()
method returns a PEAR_Error object if something blows up (or boolean TRUE if the call succeeded). Change your code to something like this:
$ret = $smtp->send();
if ($ret !== TRUE) {
echo "Mail send failed: ", $err::getMessage();
}
which should echo out any details of the failure for you.
精彩评论