我正嘗試從我的用戶數據庫中向選定的電子郵件發送郵件。我得到的身體內容user_mail完美的價值,當我發送到「[email protected],但我收到錯誤」消息無法發送。郵件錯誤:您必須提供至少一個收件人「,當我我試圖發送到「user_email。有什麼建議麼?郵件無法發送。郵件錯誤:您必須提供至少一個收件人電子郵件地址
代碼:
<?php require_once("connection.php");?>
<?require_once('libraries/PHPMailer.php');?>
/*** select the users name from the database ***/
$dbh = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password);
/*** $message = a message saying we have connected ***/
/*** set the error mode to excptions ***/
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbh->prepare("SELECT user_email FROM users
WHERE user_id = :user_id");
/*** bind the parameters ***/
$stmt->bindParam('user_id', $_SESSION['user_id'], PDO::PARAM_INT);
/*** execute the prepared statement ***/
$stmt->execute();
/*** check for a result ***/
$user_email = $stmt->fetchColumn();
}
catch (Exception $e)
{
/*** if we are here, something is wrong in the database ***/
$message = 'We are unable to process your request. Please try again later"';
}
$mail = new PHPMailer;
$mail->IsSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.mandrillapp.com'; // Specify main and backup server
$mail->Port = 587; // Set the SMTP port
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '[email protected]'; // SMTP username
$mail->Password = 'key'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = '[email protected]';
$mail->FromName = 'App';
$mail->AddAddress ($user_mail); // Add a recipient
$mail->IsHTML(true); // Set email format to HTML
$mail->Subject = ' Subject';
$mail->Body = ' working fine';
$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;
exit;
}
我猜你的數據庫代碼失敗,或者通過拋出一個異常,這將導致變量'$ message'進行設置(和忽略)或根本就沒有取任何值,所以'$ user_mail'是空。 – nonchip
不,我在$ mail-> Body部分獲得$ user_mail值。 – user3588059
究竟在哪裏?在任何地方都看不到。 – nonchip