2012-09-13 154 views
0

我是新來的PHP。我正嘗試使用以下代碼發送郵件。我的參考文件是this。我認爲它與我的結果部分有關。有人能幫忙嗎?下面無法使用快速郵件發送電子郵件

Warning: Invalid argument supplied for foreach() in /home/mysite/public_html/sendmail.php on line 50 

Warning: Invalid argument supplied for foreach() in /home/mysite/public_html/sendmail.php on line 70 

給出我的錯誤MY全碼下面給出PLEASE GUID ME

require_once 'lib/swift_required.php'; 
include('database.php'); 
// Create the Transport 
$transport = Swift_SmtpTransport::newInstance('smtp.mydomain.com', 25) 
    ->setUsername('[email protected]') 
    ->setPassword('mypassword'); 

$transport = Swift_MailTransport::newInstance(); 
// Create the Mailer using your created Transport 
$mailer = Swift_Mailer::newInstance($transport); 

// Send the message 
//$result = $mailer->send($message); 

//database connection part 
$con = mysql_connect("localhost","username","password"); 
if (!$con) { 
    die('Could not connect: ' . mysql_error()); 
} 
mysql_select_db("mydb", $con); 

$sql = "SELECT email,name FROM email_test";  
$result = mysql_query($sql); 

//tested the results, works fine 
while($row = mysql_fetch_array($result)) { 
    echo "--".$row['email']."---"; 
} 

//using result to get enail and user name 
$replacements = array(); 
foreach ($result as $user) { 
    $replacements[$user['email']] = array(
     '{email}'=>$user['email'], 
     '{name}'=>$user['name'] 
    ); 
} 

$decorator = new Swift_Plugins_DecoratorPlugin($replacements); 
$mailer->registerPlugin($decorator); 

$message = Swift_Message::newInstance() 
    ->setSubject('Important notice for {name}') 
    ->setBody("Hello {name}, we have reset your password"); 

foreach ($result as $user) { 
    $message->addTo($user['email']); 
} 
+0

'$ result'不是'foreach'陣列,使用'而($行= mysql_fetch_array($結果))' –

+0

,謝謝,我已經改變了,但仍是無法發送郵件 – minutoms

回答

2
  1. 正如註釋:$resultmysql_resource,不是一個數組。您需要在該資源上運行fetch_row以實際獲取詳細信息。在評論中提到(你可以更新代碼到你現在擁有什麼?)

  2. 當你第二次運行結果時,你需要「重置」結果集從頂部開始。你可以用mysql_data_seek($result, 0)來做到這一點,說回到頂部。