2013-09-24 51 views
0

我有以下PHP腳本查詢數據庫中的某些字段,然後應該將這些字段作爲變量並使用這些變量發送HTML格式的電子郵件。電子郵件應循環發送每封郵件的電子郵件。我遇到的問題是我收到電子郵件,但變量只是說「數組」,只發送一封電子郵件。這裏是我的腳本:數組變量在PHP電子郵件中顯示爲「數組」

<?php 
$serverName = "SERVER"; //serverName\instanceName 

// connection will be attempted using Windows Authentication. 
$connectionInfo = array("Database"=>"DB"); 
$conn = sqlsrv_connect($serverName, $connectionInfo); 

if($conn) { 
    echo "Connection established.<br />"; 
}else{ 
    echo "Connection could not be established.<br />"; 
    die(print_r(sqlsrv_errors(), true)); 
} 

//begin sql statement 
$tsql = " 
select ji.pkey, ji.summary, cfo.customvalue, ji.resolutiondate 
from jiraschema.customfieldvalue cfv 
left outer join jiraschema.customfieldoption cfo 
on cfv.STRINGVALUE = cfo.id 
inner join jiraschema.jiraissue ji 
on cfv.issue = ji.id 
where cfv.CUSTOMFIELD = 10252 and ji.issuestatus = 10002 and ji.RESOLUTIONDATE > '2013-09-18' 
order by ji.pkey"; 

//call the query 
$query = sqlsrv_query($conn, $tsql); 
if($query) 
{ 
    echo "Statement executed. \n"; 
} 
else 
{ 
    echo "Error in statement execution. \n"; 
    die(print_r(sqlsrv_errors(), true)); 
} 


while($row = sqlsrv_fetch_array($query)){ 
    $pkey[] = $row['pkey']; 
    $summary[] = $row['summary']; 
    $customvalue[] = $row['customvalue']; 
    //$clientEmail[] = $row['clientEmail']; 
    $email_from = '[email protected]';// update the email address 
    $email_subject = "Follow Up on $pkey"; 
    $email_body = '<html> 
    <body> 
    Dear ' .$customvalue.', 
    <br></br> 
    In response to your recent call to our support department, your ticket ' .$pkey. ' shows that it has been resolved. Please let me know if you need any additional assistance with this issue. If the issue has been completely resolved to your satisfaction, we will close the issue. 
    <br></br> 
    Thank you for contacting our support department. We hope we brightened your day! 
    </body> 
    </html>'; 

    $headers = 'MIME-Version: 1.0' . "\r\n"; 
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
    $to = '[email protected]'; 
    $headers .= "From: $email_from \r\n"; 
    $headers .= "Reply-To: $to \r\n"; 

    //Send the email! 

    if (mail($to,$email_subject,$email_body,$headers)) { 
     die('Mail Sent!'); 
    } else { 
      die('Error:Delivery Failed!'); 
    } 

} 

?> 
+1

我想,如果有一個模具一個PHP腳本停止(「...」) – Wikunia

+0

,你會獲取字符串值'Array'。 – leftclickben

回答

1
if (mail($to,$email_subject,$email_body,$headers)) { 
    die('Mail Sent!'); 
} 

這意味着:「如果郵件發送成功,結束腳本」。因此,您的第一封郵件已發送,但就是這樣,即使循環可以正常工作,也不會再發送更多電子郵件。

PHP documentation:「死了 - 這相當於退出」

編輯:

關於你的陣列的問題,您可以通過添加[]實例$customvalue作爲一個數組,所以當你迴音$customvalue你是不是回顯數組中包含的值,但是數組本身。

這樣做是爲了您的分配3個變量:如果您在字符串上下文中使用數組

$pkey = $row['pkey']; 
$summary = $row['summary']; 
$customvalue = $row['customvalue']; 
+0

這工作。非常感謝你! – ghoston3rd

2
$pkey[] = $row['pkey']; 
$summary[] = $row['summary']; 
$customvalue[] = $row['customvalue']; 

他們都被添加作爲數組,注意[]

而應該添加它們是這樣的:

$pkey = $row['pkey']; 
$summary = $row['summary']; 
$customvalue = $row['customvalue'];