Replies: 10 comments 4 replies
-
Do you still need mso tag's ? like for styling reason or the structure is not correct in outlook ? |
Beta Was this translation helpful? Give feedback.
-
+1 to this. The structure does not cater for outlook |
Beta Was this translation helpful? Give feedback.
-
Adding a way to include comments in the final output would be ideal 🙌 |
Beta Was this translation helpful? Give feedback.
-
This seems like a must have to me. It could be due to my lack of experience, and there may be ways to handle it I'm not aware of, but the only thing I can think of right now would be to manually add the code afterwards which doesn't seem like a great workflow. |
Beta Was this translation helpful? Give feedback.
-
I managed to do it this way: import React, { PropsWithChildren } from "react"
import { renderToString } from "react-dom/server"
type TargetProps = PropsWithChildren<{
logic: string
}>
export function Target({ logic, children }: TargetProps) {
return (
<span
dangerouslySetInnerHTML={{
__html: `
<!--[if ${logic}]>
${renderToString(children as any)}
<![endif]-->
`,
}}
/>
)
} then: <Target logic="mso">
<Text>this is outlook</Text>
</Target> |
Beta Was this translation helpful? Give feedback.
-
@mattiaz9 Can you provide it in a CodeSandbox or show the html output? I tried this but the comments are not included in the output HTML. (Tried it with the render utility and the dev environment) |
Beta Was this translation helpful? Give feedback.
-
If it helps anyone else trying to do this, I managed to accomplish this by simply running an additional post-processing step on the string that react email's My approach: I wrote a simple helper function that looks for data attributes and replaces with html comments in the final string output. function renderComments(htmlString: string): string {
const updatedHtmlString = htmlString
.replace(
/<div\s+data-comment-start="([^"]+)"\s*><\/div>/g,
'<!--[if $1]><!-- -->',
)
.replace(/<div\s+data-comment-end="([^"]+)"\s*><\/div>/g, '<!--<![$1]-->')
.replace(
/<div\s+data-comment-mso-start="([^"]+)"\s*><\/div>/g,
'<!--[if $1]>',
)
.replace(
/<div\s+data-comment-mso-end="([^"]+)"\s*><\/div>/g,
'<![$1]-->',
);
return updatedHtmlString;
} combine this with the render call and you're in business. const initialMarkup = render(<Email />, { pretty: true });
const markup = renderComments(initialMarkup); for additional convenience I added the following helper component import React from "react";
interface Props {
children: React.ReactNode;
comment: string;
msoOnly?: boolean;
}
export default function HtmlConditionalComment({
children,
comment,
msoOnly,
}: Props) {
return msoOnly ? (
<>
<div data-comment-mso-start={comment} />
{children}
<div data-comment-mso-end="endif" />
</>
) : (
<>
<div data-comment-start={comment} />
{children}
<div data-comment-end="endif" />
</>
);
} Then in your email template just <HtmlConditionalComment comment="!mso">
{/* non mso stuff */}
</HtmlConditionalComment>
<HtmlConditionalComment comment="mso" msoOnly={true}>
{/* mso stuff */}
</HtmlConditionalComment> Hope that helps |
Beta Was this translation helpful? Give feedback.
-
Would be nice if we had another component to render specifically on outlook. |
Beta Was this translation helpful? Give feedback.
-
hi guys, Do you have any news on this, or do we think we should implement some of the suggestions above? |
Beta Was this translation helpful? Give feedback.
-
Not yet anything new on this. Still something we have in mind though. I'd recommend you go with one of the solutions mentioned. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
The problem
I'm trying to add Outlook specific markup to my email templates, and so far have been unable to find any way to render MSO rules like
<!--[if mso]><![endif]-->
in the final HTML output.Including Outlook rules within brackets (i.e.,
{'<!--[if mso]><![endif]-->'}
) just prints the text of the comment, and trying suggestions online like usingdangerouslySetInnerHTML
just results in an empty<div></div>
.Is there any currently accessible way to include rules like this in templates?
Why not do this manually?
Results in comment being printed:
Results in empty
<div></div>
:Beta Was this translation helpful? Give feedback.
All reactions