This topic is locked
[SOLVED]

HTML Formatting in Email Body

7/25/2024 5:33:51 AM
PHPRunner General questions
P
PK author

I am trying to send an email with a dislaimer at the bottom

$msg.= ' style="background-color:rgb(240, 240, 240); font-size: 10px;">'.$_SESSION["maildiscl"].'
';

The result I want:
img alt

The result I am getting:
img alt

I know I am not getting the right quotes in the right places.
Can some please help me out

Thanks in advance
PK

F
FR 7/25/2024

Post the actual html so that we can put it in a checker.
Did you verify the html?

If it is not that, then - precisely how are you sending the email? Depending on what you are using, you need to indicate that the body is html.

P
PK author 7/25/2024

Hi FR,
The code is definately HTML. All other parts of the body are working fine as shown below:
img alt

Its just the discalimer bit of it at the bottom. However as requested, this is the entire code:

$msg="Hello ".$authorizerName.", "."\n";
$msg.= "A loan application requires your attention:
";
$msg.= "Employee ID: <b>".$curEmpl."</b>
";
$msg.= "Employee Name: <b>".$_SESSION["curUserName"]."</b>
";
$msg.="Amount Requested: <b>".$data['Currency']." ".$loanAmt."</b>
";
$msg.="Loan Purpose: <b>".$data['Purpose']."</b>";
$msg.= "Please log on to the ";
$msg.= $_SESSION["svrLink"]; //server link
$msg.= " to take the necessary action
";
$msg.= "Best Regards,
".$_SESSION["sign"];
//if add add image then
//$msg.= "
";
//$msg.= "
";
//$msg.= "
";
//$msg.= "<img height='50' width='100' src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=99258&image=2&table=forumreplies'>;";
$msg.= "
";
$msg.= "
";
$msg.= ' style="background-color:rgb(240, 240, 240); font-size: 10px;">'.$_SESSION["maildiscl"].'
';

Thanks
Percy

L
Leonardo 7/25/2024

Hi PK,

I has a similar case, i solved including the html style tag, i acomplish that in a separated file to load the style using css classes. For example:

My file that contains the style classes looks like the following:

styles.php

$style_1 = "
<html>
<head>

<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head>
<style>
.note {
font-style: italic;
font-size: 10px;
background-color:rgb(240, 240, 240);
}
</style>
</head>";

$styles_list = [ 'style_1' => $style_1];

return $styles_list;

I use an array to store multiple styles, but in this case i only use 1 for demonstration purposes.

now you can load the style in the email code and start to use css classes:

// code to generate email message

// include style.php

$load_styles = require 'path_to_the_styles_file/styles.php';

$style_head = $load_styles['style_1'];

$msg = $style_head;

$msg .= "<body>";

// use css class instead style, for example in a div
$msg .= "<div class='note'>" . $_SESSION["maildiscl"] . "</div>";
...
$msg .= "</body>";

ps: I prefer to use separate file to manage the email styles, but you can insert the $style_1 value on your main code

C
cristi 7/25/2024

Use 'htmlbody' => $msg

P
PK author 7/25/2024

Leonardo, thank you.
I think I am most there...I followed your steps: the syles.php file I stored in the syles folder

$email=$authorizerEmail;
$from = $_SESSION["fromEmail"];
$fromName = $_SESSION["fromName"];
$subject="Loan Approval Required!";
//get style
$load_styles = require 'styles/styles.php';
$style_head = $load_styles['style_1'];
$msg = $style_head;
$msg.= "<body>";
$msg.="Hello ".$authorizerName.", "."\n";
$msg.= "A loan application requires your attention:
";
$msg.= "Employee ID: <b>".$curEmpl."</b>
";
$msg.= "Employee Name: <b>".$_SESSION["curUserName"]."</b>
";
$msg.="Amount Requested: <b>".$data['Currency']." ".$loanAmt."</b>
";
$msg.="Loan Purpose: <b>".$data['Purpose']."</b>";
$msg.= "Please log on to the ";
$msg.= $_SESSION["svrLink"]; //server link
$msg.= " to take the necessary action
";
$msg.= "Best Regards,
".$_SESSION["sign"]."
";
$msg.= "
";
$msg.= "
";
// use css class instead style, for example in a div
$msg .= "<div class='note'>" . $_SESSION["maildiscl"] . "</div>";
$msg .= "</body>";

But the format is still not applied to the email when I receive it. I get an error:

$style_1 = " "; $styles_list = [ 'style_1' => $style_1]; return $styles_list;{"Err":"ok"}

Did I miss something?
Thanks again

P
PK author 7/25/2024

cristi
Use 'htmlbody' => $msg

cristi, I am not using the in built mailer as it was giving lots of trouble which I posted here but unfortunately didnt get any solution. So I created a separate phpMailer file and installed the other dependencies using composer. So in my phpMailer file I have:

$mail->isHTML(true); //Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $body;

And all the html works with normal tags except when I used a syle for the footer (thats the only part I need help with)
Thank you

L
Leonardo 7/25/2024

Hi PK,

  • Ensure that the path 'styles/styles.php' exists, is your email script in the same level that the styles folder? . IE:
    I-emailscript.php
    I-styles
    I--styles.php


  • Replace <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head> by <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"> i made mistake, please delete closing </head> tag


  • Also you can test by adding the style in your main script without loading external script , IE:

    $msg = "<head>
    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head>
    <style>
    .note {
    font-style: italic;
    font-size: 10px;
    background-color:rgb(240, 240, 240);
    }
    </style>
    </head>";

    $msg .= "<div class='note'>" . $_SESSION["maildiscl"] . "</div>";


  • Try to use br tag instead of \n for new lines


  • Test sending email only with $msg .= "<div class='note'> Content of this email is confidential and ... </div>"; Or: $msg .= "<span class='note'> Content of this email is confidential and ... </span>"; maybe another content is breaking something.


  • Finally you can try again with inline style but with span tag: $msg.= " <span style='background-color:rgb(240, 240, 240); font-size: 10px;'>".$_SESSION["maildiscl"]."</span>";



P
PK author 7/26/2024

Leonardo,

$msg.= " <span style='background-color:rgb(240, 240, 240); font-size: 10px;'>".$_SESSION["maildiscl"]."</span>";

This worked. Thank you so much

PK