ahanssen / skpsmtpmessage

Automatically exported from code.google.com/p/skpsmtpmessage
0 stars 0 forks source link

How to embed an image? #30

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
If I want to embed a local image as part of the email body (thus making the 
email an HTML mail), 
what change in code should be done? The image should not go as an attachment, 
but embedded 
or inline in the message body.
Some changes that I did are:
NSDictionary *plainPart = [NSDictionary 
dictionaryWithObjectsAndKeys:@"text/html",kSKPSMTPPartContentTypeKey,
                               @"This is a tést messåge. <img src=\"mail.jpg\" width=\"638\" height=\"425\" 
/>",kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil];
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"mail" 
ofType:@"jpg"];
NSData *imageData = [NSData dataWithContentsOfFile:imagePath];
NSDictionary *imagePart = [NSDictionary 
dictionaryWithObjectsAndKeys:@"image/jpeg;\r\n\tx-
unix-mode=0644;\r\n\tname=\"mail.jpg\"",kSKPSMTPPartContentTypeKey,         
@"attachment;\r\n\tfilename=\"mail.jpg\"",kSKPSMTPPartContentDispositionKey,[ima
geData 
encodeBase64ForData],kSKPSMTPPartMessageKey,@"base64",kSKPSMTPPartContentTransfe
rEnco
dingKey,nil];
Rest of the code remains unchanged. The mail.jpg file is added in Resources. 
But this is not 
sending the image embedded. It goes as an attachment. The image placeholder on 
the received 
HTML-format mail is empty.
Pls give some suggestions.

Thanks,
Raktim.

Original issue reported on code.google.com by raktimd on 14 Apr 2009 at 7:04

GoogleCodeExporter commented 9 years ago
For the content disposition key, try changing "attachment" to "inline".

Original comment by c.justin...@gmail.com on 16 Apr 2009 at 4:58

GoogleCodeExporter commented 9 years ago
Thanks Justin. Making it inline was not sufficient. I had achieved it in this 
way:

// part1 - text part, MIME type text/html, HTML <img> tag uses 
src="cid:my_image",
this cid value 'my_image' is being referred in SMTP Content-ID header
NSDictionary *plainPart = [NSDictionary
dictionaryWithObjectsAndKeys:@"text/html",kSKPSMTPPartContentTypeKey,@"This is 
a tést
messåge.<img src=\"cid:my_image\">"
,kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil];

// part2 - image part, MIME type image/jpeg, Content-Disposition is 'inline'.
Content-Transfer-Encoding is 'base64'
NSDictionary *imagePart = [NSDictionary
dictionaryWithObjectsAndKeys:@"image/jpeg",kSKPSMTPPartContentTypeKey,@"inline",
kSKPSMTPPartContentDispositionKey,[imageData
encodeBase64ForData],kSKPSMTPPartMessageKey,@"base64",kSKPSMTPPartContentTransfe
rEncodingKey,@"<my_image>",kSKPSMTPPartContentIdKey,nil];

In sendParts method of SKPSMTPMessage.m, this was done:
[message appendString:@"Content-Type: multipart/related;
boundary=SKPSMTPMessage--Separator--Delimiter\r\n"];

for (NSDictionary *part in parts)
{
    ...
    if ([part objectForKey:kSKPSMTPPartContentIdKey])
        {
            [message appendFormat:@"Content-ID: %@\r\n", [part
objectForKey:kSKPSMTPPartContentIdKey]];
        }
    ...
}

Original comment by raktimd on 17 Apr 2009 at 6:00

GoogleCodeExporter commented 9 years ago
how to I take photo and send via mail ??

Original comment by ahmeda...@gmail.com on 21 Nov 2012 at 10:23