PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, July 7, 2022

[FIXED] How to output a forloop in a string?

 July 07, 2022     php, phpmailer     No comments   

Issue

I am trying to output data object with multiple properties into a string within phpmailer how do I achieve this?

function sendMailTo($mail, $data, $subject_, $toEmail) {
    $body = 'all data:' foreach ($data as $obj) {
        $obj->property;
    }

    $mail->ClearAllRecipients();
    $mail->AddAddress($toEmail);
    $mail->FromName = $data->inputName;
    $mail->From = $email;
    $mail->Subject = $subject_;
    $mail->IsHTML(true);
    $mail->Body = $body;
    $mail->AddReplyTo($toEmail, $data->inputName);
    $mail->send();
}

Solution

This code is a bit nonsensical, unless $data is really an array of objects, each having a property called property.

If you want to extract all the properties of an object, iterate over them, because PHP objects are iterable.

Also there's no need to use a local variable; assemble the string directly into $mail->Body:

$mail->Body = "All data:\r\n";
foreach ($data as $property => $value) {
    $mail->Body .= "$property = $value\r\n";
}

I've assumed that you want both the property names and values.

(and you can remove the later line that says $mail->Body = $body;)



Answered By - Synchro
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing