0

I have the following PHP script that queries the database for certain fields and then should take the fields as variables and send an HTML formatted email using those variables. The email should loop and send an email for every issue. The problem that I am having is that I get the email but the variables just say "Array" and only one email is sent. Here is my script:

<?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!');
     }

}

 ?>
2
  • 1
    I think a php script stops if there is a die("...") Commented Sep 24, 2013 at 15:42
  • If you use an array in a string context, you'll get the string value Array. Commented Sep 24, 2013 at 15:53

2 Answers 2

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

They are all being added as Arrays, note the []

You should instead add them like this:

 $pkey = $row['pkey'];
 $summary = $row['summary'];
 $customvalue = $row['customvalue'];
Sign up to request clarification or add additional context in comments.

Comments

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

This means: "If mail is sent successfully, end script". So your first mail is sent but that's it, no more emails will be sent even if the loop would otherwise work.

As per PHP documentation : "die — Equivalent to exit"

Edit:

Regarding your array problem, you instantiate $customvalue as an array by adding [], so when you echo $customvalue you are not echoing the value contained in the array but the array itself.

Do this to assign your 3 variables:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.