How can you send mail using IMAP?
Asked Answered
N

5

29

I am developing a lightweight Gmail client for mobile phones, accessing Gmail by IMAP. I want to send a draft from the Drafts folder, but it has some attachments and I cannot download all of them to send it by SMTP.

Moving/copying it to "Sent Mail" does not send it, just moves it to that folder.

How can I send a Draft directly without fetching all the content and attachments from the client? Is there any IMAP command to do it?

Northampton answered 26/10, 2009 at 22:4 Comment(1)
There is an draft from one of the Trojita authors providing this functionality: tools.ietf.org/html/draft-kundrat-imap-submit-02Cressi
S
52

IMAP is a mailbox protocol. It does not (natively) support sending mail, only accessing it. In order to send mail you must use SMTP. Its possible that there is an IMAP extension for sending mail, and its possible that Google Mail supports that extension, but I doubt it. Hence, if you want to send an email with attachments, you must actually have the full content of the message available to you to send.

Single answered 26/10, 2009 at 22:46 Comment(1)
In fact Google Mail doesn't support that "extension". That's why I tried to move the mail to "Sent Mail" box directly.Northampton
I
17

IMAP was designed to receive email messages, not to send it. There is no IMAP command for sending email AFAIK. There is, however, at least one IMAP server which supports a special 'Outbox' folder. When you place the message into this folder it will be sent automatically.

Check Courier-IMAP documentation on Sending mail via an IMAP connection. Note, that this is a non standard method and I'm not aware of any other server which supports this.

There RFC 4468 which extends SMTP so it can fetch the mail content from the IMAP server, but I don't know about any working and widely used implementation.

Talking about gmail: sticking with SMTP is probably the safest way to go.

Induct answered 27/10, 2009 at 18:13 Comment(3)
I'd love to know why this is; is it purely historical that a separate connection is used for receiving and sending? Is it because POP used to be the standard?Macrobiotics
It's mainly just because SMTP was already well established so no need to duplicate its purpose by building email submission support into another protocol designed for a different purpose. There's no harm in using different protocols for submission and reading - they're two very different tasks when it comes to email.Odericus
Are you not sending the message twice: once to SMPT for delivery and once to IMAP for storage. This would be inefficient.Renal
F
6

By the way, now that any modern mail client (including the webbased ones) supports a Sent folder, you typicaly have to use both SMTP and IMAP to send a single mail. And there's a race condition between sending the e-mail over SMTP and successfully saving the e-mail to the IMAP Sent folder. Using IMAP for sending e-mail is a way to avoid this race condition.

Fish answered 26/12, 2013 at 19:1 Comment(0)
A
1

Sending email is a special feature of some imap servers. Its nothing in the imap protocol. You just copy your email into a special imap directory on the server and it sends them. I doubt that gmail supports this.

Acotyledon answered 26/10, 2009 at 23:31 Comment(2)
What server(s) support this? I've never heard of this, and would like to investigate it some more. Thanks!Debbydebee
Only a few years late ... Apparently Courier Mail Server supports this.Inman
G
0

I sent an email to my own email address using IMAP using Python 3 to a gmail account. What is does is append a message to a mailbox. You need to utilize a handful of Python's native libraries. Also study this documentation for imaplib, this code is featured in the section Uploading Messages: To add a new message to a mailbox, construct a Message instance and pass it to the append() method, along with the timestamp for the message.

Then check your gmail inbox and you'll see the new message.

import imaplib
import time
import email.message
import imaplib_connect

new_message = email.message.Message()
new_message.set_unixfrom('name')
new_message['Subject'] = 'Test'
new_message['From'] = '[email protected]'
new_message['To'] = '[email protected]'
new_message.set_payload('This is an example message body.\n')

print(new_message)

with imaplib_connect.open_connection() as c:
    c.append('INBOX', '',
             imaplib.Time2Internaldate(time.time()),
             str(new_message).encode('utf-8'))

# Show the headers for all messages in the mailbox
c.select('INBOX')
typ, [msg_ids] = c.search(None, 'ALL')
for num in msg_ids.split():
    typ, msg_data = c.fetch(num, '(BODY.PEEK[HEADER])')
    for response_part in msg_data:
        if isinstance(response_part, tuple):
            print('\n{}:'.format(num))
            print(response_part[1])
Gammon answered 17/6, 2016 at 20:32 Comment(2)
You do not "send" an email - you actually append an email in your 'Inbox' mailbox, to appear as you have received it. That's not what OP asked for.Lithomarge
The encode('utf-8') part finally got this to work for me.Maulstick

© 2022 - 2024 — McMap. All rights reserved.