user2497586
user2497586

Reputation:

Send HTML email with SKPSMTP iOS

I am trying to send an HTML email from my SKPSMTP code in iOS. Right now, I'm just sending plain text, but I'm trying to upgrade that a little. I've included that code below.

I can't find any documentation. How can I upload an HTML file and include that as it's body. Also, there's an image that's being loaded from the same directory as the HTML file, if that makes a difference in the answer. Thanks.

 NSMutableString *emailBody = [NSMutableString stringWithFormat:@"Here's your code again, "];
                [emailBody appendString:userCode];
                SKPSMTPMessage *email = [[SKPSMTPMessage alloc] init];
                email.fromEmail = @"[email protected]";
                NSString *toEmail = [NSString stringWithFormat:@"%@", self.loginInput.text];
                email.toEmail = toEmail;
                email.relayHost = @"smtp.gmail.com";
                email.requiresAuth = YES;
                email.login = @"[email protected]";
                email.pass = @"myPass";
                email.subject = @"Your Validation Code";
                email.wantsSecure = YES;
                email.delegate = self;
                NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/plain",kSKPSMTPPartContentTypeKey,
                                           emailBody,kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey, nil];
                email.parts = [NSArray arrayWithObjects:plainPart, nil];
                // Send it!
                [email send];

Upvotes: 2

Views: 844

Answers (1)

Alex Chumbley
Alex Chumbley

Reputation: 774

So, here's the answer I came across, just so everyone else can get the benefit of me struggling through:

 //Send them an e-mail
NSError* error = nil;
NSString *path = [[NSBundle mainBundle] pathForResource: @"loginEmail" ofType: @"html"];
NSString *result = [NSString stringWithContentsOfFile: path encoding:
                    NSUTF8StringEncoding error: &error];
NSRegularExpression *regex = [NSRegularExpression
                              regularExpressionWithPattern:@"<!--INJECT CODE HERE -->"
                              options:0
                              error:&error];
NSString *emailBody = [regex stringByReplacingMatchesInString:result options:0 range:NSMakeRange(0, [result length]) withTemplate:code];
NSLog(@"%@", [emailBody class]);
SKPSMTPMessage *email = [[SKPSMTPMessage alloc] init];
email.fromEmail = @"[email protected]";
NSString *toEmail = [NSString stringWithFormat:@"%@", self.loginInput.text];
email.toEmail = toEmail;
email.relayHost = @"smtp.gmail.com";
email.requiresAuth = YES;
email.login = @"[email protected]";
email.pass = @"myPass"
email.subject = @"Your Validation Code";
email.wantsSecure = YES;
email.delegate = self;
NSDictionary *htmlPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/html",kSKPSMTPPartContentTypeKey,                    emailBody,kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey, nil];
email.parts = [NSArray arrayWithObjects:htmlPart,  nil];
// Send it!
NSLog(@"ABOUT TO SEND");
[email send];

So, I had to write an HTML file, host all my images on tinypic to include in the HTML, write some text to regex switch out my code variable, load in it in here and attach it as the part of my email with key "text/html". This code works, but if anyone has any other suggestions that are helpful, I'm willing to mark them as the right answer!

Upvotes: 3

Related Questions