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.
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
Step One: Create the UI
Make sure to add a button that will trigger the email function.
Step Two: Create the email functionality
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
Call the sendEmail function created in Step Two from the action created in Step One.
Step Four: Test
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.