Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions lib/render.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let render_html element =
let size_kb = size / 1024 in
Error (Printf.sprintf "Email size %dKB exceeds Gmail's 102KB limit" size_kb)

let render_email element =
let render_email ?(body_background = Color.solid "#ffffff") element =
(* Render the body content *)
let body_html = Element.to_html element in

Expand All @@ -24,6 +24,21 @@ let render_email element =
let size_kb = size / 1024 in
Error (Printf.sprintf "Email size %dKB exceeds Gmail's 102KB limit" size_kb)
else
(* Build body background style and attributes *)
let background_style = Color.to_style body_background in
let base_style = "margin: 0; padding: 0; word-spacing: normal;" in
let full_style = base_style ^ " " ^ background_style in

(* Add bgcolor attribute for Outlook gradient fallback *)
let bgcolor_attr = match body_background with
| Color.Solid hex -> Printf.sprintf " bgcolor=\"%s\"" hex
| Color.Gradient {fallback = Color.Solid fb_hex; _} ->
Printf.sprintf " bgcolor=\"%s\"" fb_hex
| Color.Gradient {fallback = Gradient _; _} ->
(* Should be impossible through API, but handle gracefully *)
" bgcolor=\"#ffffff\""
in

(* Build complete email document *)
let email_html = Printf.sprintf
{|<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Expand All @@ -49,8 +64,8 @@ let render_email element =
</style>
<![endif]-->
</head>
<body style="margin: 0; padding: 0; word-spacing: normal; background-color: #ffffff;">
<body%s style="%s">
%s
</body>
</html>|} body_html in
</html>|} bgcolor_attr full_style body_html in
Ok email_html
8 changes: 7 additions & 1 deletion lib/render.mli
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,16 @@ val render_html : Element.t -> (string, string) Result.t
Includes doctype, HTML wrapper, and email-specific meta tags.
Checks Gmail 102KB limit before returning.

@param body_background Optional background color for the <body> element.
Defaults to solid white (#ffffff).
Gradients are supported with proper fallback.
@param element The email body content
@return Ok complete email HTML on success, Error message if limit exceeded
*)
val render_email : Element.t -> (string, string) Result.t
val render_email :
?body_background:Color.t ->
Element.t ->
(string, string) Result.t

(** Gmail size limit in bytes (102KB) *)
val gmail_limit_bytes : int
Expand Down