📜 ⬆️ ⬇️

We write letters from iOS applications

Problem

I want to send a letter from an iOS application without exiting the application (do not use the mailto URL) and without opening an additional screen (do not use MFMailComposeViewController).

Solutions

Expensive: raise on your server a web-service that will be engaged in sending out emails, from the application to access it.
Cheap: use SMTP client inside your application.

Problem

Need to write your SMTP client
')

Decision

Easy to use SMTP client for iOS - SKPSMTPmessage

Let's see how to send messages with it - we will create a simple application with a field for entering text of the letter and the button “Send”.

Go to Xcode, create a new View-based application for iPhone. Call it EmailSender ( source code )



We do checkout of source codes of SKPSMTPmessage
svn checkout skpsmtpmessage.googlecode.com/svn/trunk skpsmtpmessage-read-only
And copy the following files into our project from there (you can simply drag and drop files from the Finder into the project tree in Xcode):
Base64Transcoder.h
Base64Transcoder.m
HSK_CFUtilities.h
HSK_CFUtilities.m
NSData + Base64Additions.h
NSData + Base64Additions.m
NSStream + SKPSMTPExtensions.h
NSStream + SKPSMTPExtensions.m
SKPSMTPMessage.h
SKPSMTPMessage.m





We also need to connect to the project CFNetwork.framework





We need a field for the text of the message and a button to send a letter.
Let's edit our EmailSenderViewController.h file
#import <UIKit / UIKit.h>

@interface EmailSenderViewController : UIViewController {

IBOutlet UITextView * txtMessage;
}

- ( IBAction ) sendEmail;

@end

Add to EmailSenderViewController.xib TextView (associate with txtMessage) and Button (click on send SendEmail)


We implement the function of sending emails to EmailSenderViewController.m
- ( IBAction ) sendEmail
{
SKPSMTPMessage * testMsg = [ [ SKPSMTPMessage alloc ] init ] ;

testMsg.fromEmail = @ "your.sender@gmail.com" ; // address, from whom we send the letter
testMsg.toEmail = @ "your.receiver@gmail.com" ; // address where we send the letter
testMsg.relayHost = @ "smtp.gmail.com" ; // smtp server you use
testMsg.requiresAuth = YES ; // whether authentication is required
testMsg.login = @ "your.login@gmail.com" ; // login to smtp server
testMsg.pass = @ "yourpassword" ; // password for smtp server
testMsg.subject = @ "Mail from habr" ; //topic of the letter
testMsg.bccEmail = @ "" ;
testMsg.wantsSecure = YES ; // smtp.gmail.com doesn't work without TLS!



NSDictionary * plainPart = [ NSDictionary dictionaryWithObjectsAndKeys : @ "text / plain" ,
kSKPSMTPPartContentTypeKey,
txtMessage.text,
kSKPSMTPPartMessageKey,
@ "8bit" ,
kSKPSMTPPartContentTransferEncodingKey,
nil ] ;

testMsg.parts = [ NSArray arrayWithObjects : plainPart, nil ] ;


[ testMsg send ] ;
}


And do not forget to add the connection SKPSMTPMessage.h
#import "EmailSenderViewController.h"
#import "SKPSMTPMessage.h"

I advise you to have a separate gmail box for your application and use it to send emails (and your.sender = your.loging), as shown in the example. But, of course, you can use any other smtp server.

And finally, I'll show you how to make our TextView a little prettier (round the corners).

Connecting QuartzCore.h
#import "EmailSenderViewController.h"
#import "SKPSMTPMessage.h"
#import <QuartzCore / QuartzCore.h>

And in ViewDidLoad add
- ( void ) viewDidLoad
{
txtMessage.clipsToBounds = YES ;
txtMessage.layer.cornerRadius = 10.0f;
[ super viewDidLoad ] ;
}


The source code of the sample application can be downloaded here.
Write letters!:)

Source: https://habr.com/ru/post/127229/


All Articles