This is an alternate solution to sending HTML email from Cocoa, that takes advantage of python's email capabilities.
The python email package allows a much cleaner source when writing the code for sending the HTML email:
==================== htmlemail.py ==================
To use the Python code from Cocoa, we use the Python framework:
==================== htmlemail.m ==================
The python email package allows a much cleaner source when writing the code for sending the HTML email:
==================== htmlemail.py ==================
from email.mime import Multipart, Text, Image
import smtplib
email_from = "sender@domain.com"
email_to = "recipient@domain.com"
msg = Multipart.MIMEMultipart('alternative')
msg['Subject'] = "HTML email"
msg['From'] = email_from
msg['To'] = email_to
text = "Plain text version"
html = "<html><p>A little bit in <b>bold</b> and some <i>italic</i></p><p><img src="cid:55665566" /></p><p>The end</p></html>"
part1 = Text.MIMEText(text, 'plain')
# the embedded image
fp = open("/Users/diciu/Desktop/test.png")
# assemble the message based on the diagram
part2 = Multipart.MIMEMultipart('related')
part21 = Text.MIMEText(html, 'html')
part22 = Image.MIMEImage(fp.read(), _subtype='PNG')
part22.add_header('Content-ID', '55665566')
msg.attach(part1)
part2.attach(part21)
part2.attach(part22)
msg.attach(part2)
s = smtplib.SMTP('some.smtp.server')
s.sendmail(email_from, email_to, msg.as_string())
s.quit()
To use the Python code from Cocoa, we use the Python framework:
==================== htmlemail.m ==================
// build with:
// gcc htmlemail.m -framework Cocoa -framework Python
//
#import <Cocoa/Cocoa.h>
#import <Python/Python.h>
int main()
{
NSAutoreleasePool * m = [NSAutoreleasePool new];
NSError * err = nil;
NSString * pycode = [NSString stringWithContentsOfFile:@"./htmlemail.py"
encoding:NSASCIIStringEncoding error:&err];
Py_Initialize();
PyRun_SimpleString([pycode cStringUsingEncoding:NSASCIIStringEncoding]);
Py_Finalize();
[m release];
return 0;
}