Reputation: 83
I have a TableView that selects and deselects clients in an effort to build up an array of email addresses that one can then send a group message to.
I then want the user to be able to, via a button, open up Apple's default mail app, with a message already addressed to the members in the previously built up list.
I can do it with one address via:
let email = client.value
if let url = URL(string: "mailto:\(email)") {
UIApplication.shared.open(url)
}
I have also built up a string of comma separated emails, to copy to the clipboard if the user just wants this list to, use a different email app on their phone or whatever, just give them more options.
for client in clientsToEmail {
emails.append(client.value)
}
emailsCommaSeperated = emails.joined(separator: ", ")
This works fine, however, I cannot give this comma separated list to "mailto:()", it seems as though this method checks for a valid email, [email protected]. Which a comma separated list of emails is obviously not.
Is there another way to do this that anyone knows about? Some way to open the mail app, from another app, sending a list of email addresses, and creating a new email addressed to all of these addresses?
Any help greatly appreciated!
Upvotes: 3
Views: 2007
Reputation: 62
I just tried this today on iOS 14 (not sure if it works in previous versions) and you can simply separate the email addresses using commas without spaces.
Swift code:
let mailToRecipients = ["[email protected]", "[email protected]"].joined(separator: ",")
guard let mailtoUrl = URL(string: "mailto:\(mailToRecipients)") else { return }
UIApplication.shared.open(mailtoUrl)
Upvotes: 1
Reputation: 53
I read online from multiple sources that you cannot send more that one email to mailto unless you add the others as cc. So the mailto format should look something like this:
"mailto:[email protected][email protected],[email protected]"
Coming back to your example, first of all the space in the comma separator should be removed and the first email should be stored separately, and later you should combine the two in the format I mentioned above.
So in your case the code should look something like this:
var firstEmail = ""
var emails: [String] = []
for client in clientsToEmail {
if firstEmail == "" {
firstEmail = client.value
} else {
emails.append(client.value)
}
}
let ccEmails = emails.joined(separator: ",")
let fomattedRecipents = firstEmail + "?cc=" + ccEmails
let url = URL(string: "mailto:\(fomattedRecipents)")!
if #available(iOS 10, *) {
UIApplication.shared.open(url)
} else {
UIApplication.shared.openURL(url)
}
Upvotes: 1
Reputation: 747
You gotta percent encode those commas.
let recipients = ["[email protected]","[email protected]","[email protected]"]
UIApplication.shared.open(URL(string: "mailto:\(recipients.joined(separator: "%2C"))")!)
%2C
is the percent encoding for a comma according to https://www.w3schools.com/Tags/ref_urlencode.asp
Upvotes: 1
Reputation: 820
You can try like this mailto:[email protected][email protected]&subject=Greetings%20from%20Cupertino!&body=Wish%20you%20were%20here!
as stated in here and here but i dont think you can send mails to multiple users using mailTo() on a single tap. try adding cc
Upvotes: 1