2014-07-13 893 views
0

我正嘗試從我的用戶數據庫中向選定的電子郵件發送郵件。我得到的身體內容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; 
} 
+0

我猜你的數據庫代碼失敗,或者通過拋出一個異常,這將導致變量'$ message'進行設置(和忽略)或根本就沒有取任何值,所以'$ user_mail'是空。 – nonchip

+0

不,我在$ mail-> Body部分獲得$ user_mail值。 – user3588059

+0

究竟在哪裏?在任何地方都看不到。 – nonchip

回答

1

正確添加收件人地址

$address = "[email protected]"; 
$mail->AddAddress($address, "John Doe"); 

,並努力編碼$地址提到的,如果它的工作原理,那麼你可能有例外,在你的代碼沒有按的方式't讓你設置$ user_email

更改你的catch塊爲:

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"'; 
    echo $message; 
    echo $e; 

} 
+0

其與$ address =「[email protected]」一起工作;但在放置$ address =「$ user_email」時不起作用; – user3588059

+0

然後你的$ user_email沒有填充.. echo異常塊中的$ message變量,並使其包含$ e – nsthethunderbolt

+0

更改您的catch塊,如編輯 – nsthethunderbolt

0

我不知道你的情況和我的一樣。

我嘗試把$ mail-> AddAddress($ user_mail);進入一個while循環,並且工作。

例如:

$email_res = mysql_query($email); 

while($email_row = mysql_fetch_assoc($email_res)) { 

    $mail->AddAddress($email_row[email]); 

} 

我試圖把2個電子郵件串在(),但沒有奏效。

我認爲當$ mail-> AddAddress()中只有一個郵件地址時,$ mail-> AddAddress()可以工作,所以我嘗試將$ mail-> AddAddress()放入while循環中。

每次while循環運行時,它將生成一個電子郵件地址,並且電子郵件地址位於$ mail-> AddAddress()中,因此該程序可以正常工作。

+0

如果你有什麼要回答詳細說明並用一些代碼解釋 – Prasad

+0

感謝您的建議。我很難用英語解釋一些東西。我會盡力。 – Jean

0

爲了讓Jean的回答更加強調一下,他是絕對正確的,「$mail->AddAddress」只能在一次使用一個電子郵件地址。此課程不接受用逗號分隔的電子郵件地址列表。

Jean顯示瞭如何從MySQL查詢中循環。

如果您手頭已經有逗號分隔的地址列表,那麼您當然可以使用PHP的爆炸函數來遍歷電子郵件地址。所以:

$_to=explode(',',$to); 

foreach ($_to as $sendto)   
{ 
    $mail->AddAddress($sendto); 

    if(!$mail->Send()) 
    { 
     echo '<p class="center">Mailer Error: <strong style="color:#f00">' . $mail->ErrorInfo .'</strong></p>  
    } 
    else 
    { 
     echo '<p style="text-align:center">Message sent to: <strong>'.$sendto.'!</strong></p>'; 
    } 
} 
相關問題