Scott Frank
Scott Frank

Reputation: 55

how to pass in subject and body text in swiftui mail app?

I am trying to pass in subject and body text when I open the mail app but for some reason the mail wont happen hen I pass in parameters . can anyone see in my code of what I am doing wrong? or if guys have another method for me to work in swiftui that will be awesome. thanks for the help.

here is my code:

Button(action: {
                         
     let email = "[email protected]"
     let subject = "Feedback"
    let body = "Please provide your feedback here, and we will contact you within the next 24-48 hours."
   guard let url = URL(string: "mailto:\(email)?subject=\(subject)&body=\(body)") else { return }

   UIApplication.shared.open(url)
}) {

  Text("Contact Us - ")
 }

Upvotes: 2

Views: 1724

Answers (1)

Asperi
Asperi

Reputation: 257869

In general you need to add percent escaping for components of composed URL, like

let email = "[email protected]"
let subject = "Feedback"
let body = "Please provide your feedback here, and we will contact you within the next 24-48 hours."
guard let url = URL(string: "mailto:\(email)?subject=\(subject.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? "")&body=\(body.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? "")") else { return }

Upvotes: 7

Related Questions