0

I have a language.inc file which has a email template that looks like this:

$lang["email_templete"] = '
<style type="text/css">
*....more stylings here...*
</style>
<div>
<table>
*....more elements....*
%s
</table>
</div>
';

My problem is that whenever I'm trying to insert contents into my template using sprintf, it always hits the error "Too few arguments". I've tried removing all the excessive html codes and replaced it with a more simple html code and it works. Do you have any clue on this problem?

function add_to_email_template($pTargetEmail, $pSubject, $pContent) {
  global $lang;
  $vMessage = sprintf($lang["email_templete"], $pContent);
  send_email($pTargetEmail, $pSubject, $vMessage);
}
Keale
  • 3,924
  • 3
  • 29
  • 46
Julian T
  • 11
  • 5
  • Show us what doesn't work instead of the version that does work. – Julie Pelletier Jun 28 '16 at 05:03
  • @HamzaZafeer I`m pretty sure that i`ve inserted only one placeholder and provided value for it. – Julian T Jun 28 '16 at 05:10
  • @JuliePelletier the code above doesnt work. The long version of my code is too long to be insert here. I've tried removing all my template's code and replaced it with a simple HTML code with only a
    and it works. But when i insert more complex html codes, it doesnt. I've even tried hard coding my email template in my function and it works fine too.
    – Julian T Jun 28 '16 at 05:17
  • @JulianT: Please update your question to show a complete, yet simple, example of that code which doesn't work so we can easily test it and point you in the right direction. – Julie Pelletier Jun 28 '16 at 05:22
  • http://sandbox.onlinephpfunctions.com/code/fe7cf9e1b535d5125c8c2b7dc94f7d9d9eb31331 shows that your current code is functional. – Julie Pelletier Jun 28 '16 at 05:24
  • @JuliePelletier http://sandbox.onlinephpfunctions.com/code/5e8697670b630af6746f757a94d589ab07dd7d3d was thinking of a way to show you my code, but it is. – Julian T Jun 28 '16 at 05:39
  • This is not what you showed in the question and now the problem is most obvious. – Julie Pelletier Jun 28 '16 at 05:44
  • @Julian you should have to check your $lang["email_templete"] by printing it like as follows : echo $lang["email_templete"]; You can able to find the other placeholder containing % sign – Hardik Kanjariya ツ Jun 28 '16 at 05:59

1 Answers1

1

Read the manual page for sprintf() and you'll see that the % sign is a format modifier of which your HTML and CSS code is filled with.

If you want to include % inside the string without getting it parsed, you need to escape it with itself such as %% will output %.

Note that it would probably be much simpler to user variables in your string definition than to use sprintf() the way you do, such as:

$variable = 'abc';
$myHTML = "Here is my variable: $variable";
Julie Pelletier
  • 1,740
  • 1
  • 10
  • 18