Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 29755

Sending Emails that contain HTML using NSSharingService in Swift MacOS

$
0
0

I am attempting to compose an Email on a MacOS Program that I am creating using Swift and embed HTML in the Email. After searching the internet, I found that you can send attributed Strings through NSSharingService, which can contain much of the same information that HTML does and you can convert the HTML to an NSAttributedString.

class SendEmail: NSObject {    static func send(txt: String) {        let service = NSSharingService(named: NSSharingService.Name.composeEmail)!        service.subject = "Subject Line"        let htmlData = txt.data(using: .unicode)!        let attribStr = NSAttributedString(html: htmlData, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding:String.Encoding.utf16.rawValue], documentAttributes: nil)        service.perform(withItems: [attribStr])    }}

Though the original HTML email I wanted to compose is fairly complex, I created a simplified example that still demonstrates the issue.

        let html = """<html><head><style type="text/css">                #outerBorder {                    width: 650px;                    border: 1px solid black;                    font-style: italic;                    margin: auto;                    padding: 10px;                }                #mainHeader {                    width: 650px;                    background-image: linear-gradient(to bottom, rgb(15, 102, 250), rgb(0, 77, 209));                    border: 1px solid black;                    color: black;                    box-shadow: 2px 2px 3px black;                }</style></head><body><div id="outerBorder"><div id="mainHeader"><h1 align="center">Post-Game Report</h1><h6 align="center">Generated by Swift</h6><p align="center" ; margin-bottom: 20px>HomeTeam vs AwayTeam</p></div></div></body>"""        SendEmail.send(txt: html)

When I run this code, it does compose an email, but it does not display the borders and background colors that are supposed to be in the HTML.

While the actual HTML looks like this:

https://i.stack.imgur.com/wYPXv.png

The composed email looks like this:

https://i.stack.imgur.com/Bukw5.png

I was wondering if there was a way to fully embed HTML in email compositions using swift in a MacOS program, without giving up borders and backgrounds.

Thanks!


Viewing all articles
Browse latest Browse all 29755

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>