0

Hi can someone help me about sending a email with the array data I gathered? I'm trying to fetch records on the DB using array and then I'll email it

Here's my code: I will get the records I want first:

$queclient_credit = "SELECT company, credits FROM clients WHERE credits <= 20 and company !='' ORDER BY credits ASC ";
    $getque = mssql_query($queclient_credit) or die();
    while ($rowclient_credit=arrayfetch($getque)){
            $rowcompany = $rowclient_credit['company'];
            $rowcredits = $rowclient_credit['credits'];
Then put it on array:
        $data = array(
                'company'=>$rowcompany,
                'credits'=>$rowcredits
        );


   $dat = implode(' : ', $data);
    }
        if($rowcredits<= 20){
        $from='[email protected]';
        $to = '[email protected]';


    $gmailPass = 'password';
    require('phpmailer/5.1/class.phpmailer.php');
    $mail = new PHPMailer();
    $mail->IsSMTP();

    $mail->SMTPAuth = true;

    $mail->SMTPSecure = "ssl";

    $mail->Host = 'smtp.gmail.com';

    $mail->Port = '465';

    $mail->Username = $from;

    $mail->Password = $gmailPass;
    $mail->From = $from;
    $mail->FromName = $from;
    $mail->AddReplyTo($from, $from);
    $mail->Subject = 'Credit System ';
    $mail->Body = $message;
    $mail->MsgHTML('<b><p>'.$dat.' credits<p><b>');
    $mail->IsHTML(true);
    $mail->AddAddress($to, $to);
if(!$mail->Send()){
     // $mail->ErrorInfo;
}else{
    echo 'Message Successfully Sent!';
    $mail->ClearAddresses();
    $mail->ClearAttachments();
    }
}

Thank you for who will answer

4
  • You don't have $message defined in your code from line $mail->Body = $message; Commented Jun 3, 2016 at 5:43
  • Thank you..I've already successfully sending a email But the problem is I can only send one record only.What i want is I can email all of the records I will fetched from the Db Commented Jun 3, 2016 at 5:44
  • is your content is same for all email ? Commented Jun 3, 2016 at 5:49
  • I guess you won't need if($rowcredits<= 20) your database query will take care of that. Commented Jun 3, 2016 at 5:50

3 Answers 3

1

I am not a pro but i can give you suggestion. Every thing in your code is ok but Yes, your code will send only one record, Just make two changes, your code will start sending all the records if array.

$data = array(
            'company'=>$rowcompany,
            'credits'=>$rowcredits
    );

to

$data[] = array(
            'company'=>$rowcompany,
            'credits'=>$rowcredits
    );

2nd change is move $dat = implode(' : ', $data); out of your while loop.

you can format your result using different html tags as per your requirement if you want some styling in your email.

hope this will help.

Sign up to request clarification or add additional context in comments.

Comments

0

If you want display all the details through table in mail use like this,

  $data="<table><tr>
                <th></th>
                <th></th>
                </tr>";

  while ($rowclient_credit=arrayfetch($getque)){
            $rowcompany = $rowclient_credit['company'];
            $rowcredits = $rowclient_credit['credits'];
            $data.="<tr> <td>".$rowcompany."</td>
                         <td>".$rowcredits."</td>
                    </tr>";
    }

  $data.="</table>";


    $mail->MsgHTML($data);

Comments

0

You have to create a variable (you already have it anyway: $message), and fill it with your array data by using foreach function to concatenate string. For example:

$message = "";
foreach($getque as $g)
{
       $message .= "</br>".$g["table_column_name"];
}
...

2 Comments

Uhm I've already sending email but i can only send one record only although i defined it in array.How can i send all records to email?
I have edited my answer. Its not as complete as you need but try to put your query result in there.

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.