0

我有以下 PHP 脚本查询数据库中的某些字段,然后应该将字段作为变量并使用这些变量发送 HTML 格式的电子邮件。电子邮件应该循环并为每个问题发送一封电子邮件。我遇到的问题是我收到了电子邮件,但变量只是说“数组”并且只发送了一封电子邮件。这是我的脚本:

<?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 = 'xxx@xxx.com';// 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 = 'xxx@xxx.com';
    $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!');
     }

}

 ?>
4

2 回答 2

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

它们都被添加为数组,请注意[]

您应该像这样添加它们:

 $pkey = $row['pkey'];
 $summary = $row['summary'];
 $customvalue = $row['customvalue'];
于 2013-09-24T15:42:14.733 回答
1
if (mail($to,$email_subject,$email_body,$headers)) {
   die('Mail Sent!');
}

这意味着:“如果邮件发送成功,结束脚本”。因此,您的第一封邮件已发送,仅此而已,即使循环可以正常工作,也不会再发送电子邮件。

根据PHP 文档:“死——相当于退出”

编辑:

关于你的数组问题,你通过添加实例$customvalue化为一个数组[],所以当你回显时,$customvalue你不是在回显数组中包含的值,而是数组本身。

这样做来分配你的 3 个变量:

$pkey = $row['pkey'];
$summary = $row['summary'];
$customvalue = $row['customvalue'];
于 2013-09-24T15:46:36.310 回答