I want to send mail into selected mailbox(mail folder, Gmail's label) on a gmail account.
Actually, I made a function which is getting emails on selected mailbox through IMAP Protocol. But, now I have to make a function which sends emails into my selected mailbox.
How can i selectively send mail to this mailbox?
Related
I am trying to export an Email message obtained with aiosmtpd to an *.eml file that I can later open with any *.eml viewer or email client like Thunderbird or Mail.
async def handle_DATA(self, server, session, envelope) -> str:
msg: EmailMessage = message_from_bytes(envelope.content, policy=policy.SMTPUTF8)
with open("test.eml", 'wb') as outfile:
outfile.write(msg.as_bytes())
I've also tried to save the file with Generator with both unixfrom=False and unixfrom=False and same thing.
with open("test.eml", 'w') as file:
emlGenerator = generator.Generator(file)
emlGenerator.flatten(msg, unixfrom=False)
The file gets created correctly but not all the eml files can be read correctly by Mail or Thunderbird.
Emails received from gmail.com are created correctly but emails received from protonmail.com are not. Eml files created from Protonmail emails can be opened but only from and to parameters of the email can be seen. I can't see the content neither the attachments in it.
I guess it does not have the format that eml parsers are expecting to see.
I've tried with different policies (like policy.SMTP, policy.default, ...).
What is the proper way to create eml files with python?
NOTE: The content of the EML file for Protonmail contains the following This is an OpenPGP/MIME signed message (RFC 4880 and 3156). Could this be related with the lack of correct parsing for Protonmail emails?
I had the same issue with the Email API. Although it can not always be opened directly as an .eml file, it is correctly opened if the message is sent to an SMTP server. I understand the Email API generates a string that represents the data sent to the SMTP server. That data is to be interpreted by an SMTP server, not a client. The SMTP server will interpret this data and store it as if finds more convenient (i.e. storing it in a db).
A client like Thunderbird will retrieve the message using POP3 or IMAP, but that doesn't mean that it is retrieving the same message that was sent to the SMTP server, since the server might store it in a different format.
Just like many big companies using Office365, my company is using google (gsuite) to host their email domain. I need to send automated emails to multiple people within organisation using a python script. How can that be done?
You can use a 3rd party service like Mailgun, it provides a REST API which if you hit you can trigger emails that it will send from a custom domain you configure on the service.
Its super easy to use for python, I use it for Raspberry Pi projects.
def send_simple_message():
return requests.post(
"https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages",
auth=("api", "YOUR_API_KEY"),
data={"from": "Excited User <mailgun#YOUR_DOMAIN_NAME>",
"to": ["bar#example.com", "YOU#YOUR_DOMAIN_NAME"],
"subject": "Hello",
"text": "Testing some Mailgun awesomness!"})
It is a nice alternative to using a corporate SMTP server.
Got it fixed.
In order to send an email from Python, we first need to switch ON "Less secure app access" https://myaccount.google.com/lesssecureapps?utm_source=google-account&utm_medium=web.
This we need to do if we don't have 2 Factor Authentication.
If you use 2 Factor Authentication, then you need to create an App Password and use that particular password while sending an email and not your regular password.
To create an App Password use this link: https://support.google.com/mail/answer/185833?hl=en
Now using sample script like below, we can send an email.
import smtplib
# creates SMTP session
s = smtplib.SMTP('smtp.gmail.com', 587)
# start TLS for security
s.starttls()
# Authentication
s.login("username#domain.com", "app_password")
# message to be sent
message = "Message_you_need_to_send"
# sending the mail
s.sendmail("username#domain.com", "recipient#domain.com", message)
# terminating the session
s.quit()
Google provides Gmail api suite for python and it is the preferred way to access versus smtp login/password
You should refer to their developer console for examples and tutorials
I have a instrument at work that emails me a file containing raw data, I can go into my email and download them easily enough but when I have multiple files (which it sends as multiple emails) it gets a bit tedious.
I'm looking at using python and imaplib to login to my email account, search for emails from a known email address within the past day or so and then download any attachments to a directory. So I thought a script might help here.
I've setup a gmail account and altered the settings so that I can connect using imap from a shell, however I'm lost as to where to go from here.
Could someone point me in the right direction as to what I need to do to make this happen.
Here is a repository that is forked off imaplib (made compatible with Python3.6, did not test other versions)
https://github.com/christianwengert/mail
The following snippet checks all unseen messages, then returns their attachments:
server = IMAPClient(imap, use_uid=True, ssl=993)
server.login(username, password)
server.select_folder('INBOX')
message_ids = server.search([b'NOT', b'SEEN']) # UNSEEN
messages = server.fetch(message_ids, data=['ENVELOPE', 'BODYSTRUCTURE', 'RFC822.SIZE'])
for mid, content in messages.items():
bodystructure = content[b'BODYSTRUCTURE']
text, attachments = walk_parts(bodystructure, msgid=mid, server=server)
HTH
I initially tried using the smptlib library but in order to send an e-mail I need to use SSL which costs me with Google App Engine. Other than that, smtplib was a perfect solution.
I found a link here which explains how to send an email using the GAE API.
However, I can't seem to figure out how one would log in to a gmail account say, in order to send the email.
The purpose of this is to send a verification email to addresses of those who are registering. But in the link it shows how to get the current_user and send the email using their credentials. How would I explicitly enter an email, password, and smtp server like I would do with smtplib in order to send the email from my desired address.
Thanks for any answers!
You can't send from an arbitrary email address. As the overview docs state, you can only send from:
The Gmail or Google Apps Account of the user who is currently signed in
Any email address of the form anything#appname.appspotmail.com or anything#appalias.appspotmail.com
Any email address listed in Email API Authorized Senders found in the App Engine Settings page of the Developers Console
If you are the owner of the email account you want to send from, and it is a GMail account, you can add it to the Email API Authorized Senders list from the App Engine console.
Once that's done, you can just use it as the sender address - you don't need to log into anything.
I'm using GAE send mail- but I dont want the sender of the mail to get a coppy of the mail.
as for now, when a user is sending mail he gets a mail saying that he sent a mail to someone and the body of the sent mail, how do I disable that?
You can't. Sending email from someone without their knowledge isn't permitted by App Engine.
You can send email from any administrator address; you could add a "donotreply#yourapp.com" type address as an administrator and send email from that address.