How to send an email from an app in Swift

A step by step tutorial on presenting a user with a preset email from an UIView or UIViewController in Swift.

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 11/01/2023 at 14:14
Last Updated on 11/01/2023 at 15:00
<p>Two flowers that represent Swift and Xcode side by side. Beneath them sits the text "Send an Email."</p>

A step by step tutorial on presenting a user with a preset email from an UIView or UIViewController in Swift.

SubscribeDownload Open Source Project

We recommend that you clone our Open Source Swift Starter Project, checking out the main branch and carrying out the steps below. The changes can be found on the tutorial/email branch.

git clone git@github.com:delasign/swift-starter-project.git
View Repository

Step One: Create the UI

<p>A screenshot of Xcode showing the functionality that we added to make it show a button with the text "Send Email."</p>

Make sure to add a button that will trigger the email function.

How to make a custom button in SwiftHow to localize iOS, MacOS, TVOS & WatchOS applications in XCode and SwiftHow to create a styleguide in XCode and Swift

Step Two: Create the email functionality

<p>A screenshot of Xcode showing the Email extension for the CustomUIView.swift. Code available below.</p>

Create a new email extension for your UIView or UIViewController and paste in the code below.

Please note that if you are doing this within a UIViewController, you will not need the "vc" variable, as the UIViewController will be the vc variable.

Step Three: Call Send Email

<p>A screenshot of Xcode highlighting how we have called send email from the callback of a button within a UIView.</p>

Call the sendEmail function created in Step Two from the action created in Step One.

Step Four: Test

<p>A screenshot of an iPhone demonstrating how an email has been drafted that matches the code shown in this tutorial.</p>

Run the app in Xcode and make sure that the functionality works as expected.

Looking to learn more about things you can do with Swift and XCode ?

Search our blog to find educational content on learning how to use Swift and XCode.

Search our Blog

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
SubscribeContact UsVisit our BlogView our ServicesView our Work

Partner with us

We would love to get to know you and see how we can help your organization with its goals and needs.
Let's Talk

Stay Informed

Get occasional updates about our company, research, and product launches.
Subscribe