Checking mail subject from Bash or Python - python

I have many cron jobs running on various servers, I want to check the status of the jobs via mail using command line. The only way seems to me is to extract the subject or body of the mail the jobs sent. cron will send mail regardless of the success or failure.
For example, upon success cron will send a mail with subject:
Done..succeeded
If failed:
Not done..Failed
The cron jobs run at specific times e.g. lets take 10:00, 16:00 and 22:30 everyday.
I have tried curl and urllib2 but could not get desired result. Also note that i can not make any modification to cron itself, the only option is to check the gmail.
So how can i check my gmail from bash or python to extarct the subject so that i get get an idea of the cron job?

You can use imaplib to talk to Gmail with the IMAP protocol. This is the same protocol that you would use to have Gmail work with e.g. Thunderbird, so it’s fairly easy to retrieve messages rather than dealing with scraping often-changing HTML.

I had some python code that used to check gmail for me. I was going to post it but I tried it out and it no longer works for me. I haven't used it for about a year and Gmail has updated their auth to use oauth2 which is more secure. You may be able to get imaplib to work as well but you will need to enable less secure apps on your gmail account. If I have time to tweak it and get it working I will post it. In the meantime this link looks like a pretty good walk-through of how to get it working.
https://github.com/google/gmail-oauth2-tools/wiki/OAuth2DotPyRunThrough

Related

How to send emails from CPanel email using python script

Hope you are doing well.
I am a developer that has not enough experiences in CPanel.
There are some problems to send email from CPanel and have tried a lot times but couldn't resolved, so I asked help of experienced veterans.
Recently days, I made simple project to send email in python and checked it on my local pc.
It worked well, so I posted it on CPanel, but some problems were occurred.
When I ran python code, it works without error but there is a problem for receiving.
I tested it with 2 kinds of email, one is outlook email, one is gmail.
First problem is a big latency. Outlook email was arrived after 2hours from I sent it in python. Also sometimes they was not arrived.
Second problem is occurred in gmail, gmail never arrives.
I searched many blog and post, but couldn't find solution.
Best similar contents is Sending emails from CPanel email using python script, but has not correct answer.
Additional
I use godaddy cpanel and can not use WHM. Also asked questions to support team before 4days, but they did not give me correct solution. Following image is a information that I used to send email.
I want to get the solution if I can send email directly to smtp.gmail.com on cpanel using python or if this is impossible, how to send email from ***#<domain.com> of cpanel to gmail accounts.
Thank you.

Is there a method for automating emails with SMTP or otherwise?

I've been writing some python scripts in order to do some automation for my work. One of the scripts is intended to gather some test results, compile the string of results with a "message" string, and send it as an email every 12-24 hours (if there are results) to each individual who needs this information. Additionally, we're running this script on Linux; either in a Jenkins pipeline, or in a crontab (this script will most likely be run via crontab).
I was initially using gmail's SMTP server (smtp.gmail.com, port 587) to send these since we're working off of our own personal gmail server anyways, and it worked for a bit once I gave the script an "App Password" since it was a "less secure app" to Google. However, after about an hour of testing with it, Google disabled the account for spam. Any subsequent accounts I try to set up for the same purpose are disabled on the spot, as well (the moment I try to send an email with it, it's halted and disabled). It's been a few days since I requested reviews on both of the accounts; but I don't think they'll get back to me any time soon, nor will it be a result in my favor.
Since Google was no longer viable, I looked online and saw that there are plenty of SMTP hosting options available, but we're not looking for a paid service just to send an email once every few days or so. In terms of free options, I was able to find one other post related to PHP/Ruby sending emails without SMTP (Send email without external SMTP service), but if possible I'd like to keep this within Linux/Python only unless there is a simpler way, or a way that links well with Linux/Python. Even then, I'm still concerned that using SMTP is necessary for our gmail accounts to receive these emails. If I'm wrong, please correct me; because it certainly seems that way to me.
Based off of the situation, how could I adjust my strategy in order to automate email updates of this nature?

How to send emails according to user request in python?

How do i send an email whenever a user requests it?. I thought on a script that sends mails every hour with some content relevant for the recipients, but I dont like the idea of spamming mail to other people. So i would like to send this mail whenever the recipients wants it. For example if this user sends me a mail with a keyword, i would like the sript to run and send the mail back with the content already made up. So, there is no spam and information is sent in a more efficient way.
I appreciate your help in advance.
This is a pretty complex and broad question.
First off, you would need a server or your PC to have the script running all the time or periodically fire off via cron.
Secondly, you would need to keep some kind of job queue where you would register that a user has sent you an email requesting that you send them one in response. There are well developed and complex job queues like Celery, but you could potentially do with something less complex - maybe a Google Sheets table.
Thirdly, you would need to parse emails you receive in order to get the keywords and email addresses. If you go the gmail + google sheets route, you could do it with some custom code probably, but otherwise you would need to implement email checking on your PC or server. Once you parse them, you add them to the jobs queue.
Finally, you need to provide email access to your Py script - meaning SMTP login. The docs have more on this.
As you can see, there are a lot of things to consider and implement, so you maybe better off trying to narrow down your question a bit. :)

checking local email messages with Python

My problem is what's the best strategy for periodically checking a local email account to find if there is any new message(if any, then send these messages to some function to process)?
While during development, we use a Gmail account, so we use a periodic celery task to check the gmail account (through IMAP), and process the emails if there is any.
Now if we implement the mail server by ourselves, shall I still IMAP to the server, or I can just read the files under Maildir? Which is the preferred way?
Actually my problem may not be language-specific, but since I'm using Django/Python, so I just put it in the title. But a general answer about the pros and cons would be enough. Thanks!
Check out the mailbox module:
http://docs.python.org/2/library/mailbox.html
http://docs.python.org/3.3/library/mailbox.html
It would be far faster to just read the messages directly than to interact via IMAP.

Integrate postfix mail into my (python)webapp

I have a postfix server listening and receiving all emails received at mywebsite.com Now I want to show these postfix emails in a customized interface and that too for each user
To be clear, all the users of mywebsite.com will be given mail addresses like someguy#mywebsite.com who receives email on my production machine but he sees them in his own console built into his dashboard at mywebsite.com.
So to make the user see the mail he received, I need to create an email replica of the postfix mail so that mywebsite(which runs on django-python) will be reflecting them readily. How do I achieve this. To be precise this is my question, how do I convert a postfix mail to a python mail object(so that my system/website)understands it?
Just to be clear I have written psuedo code to achieve what I want:
email_as_python_object = postfix_email_convertor(postfix_email)
attachments_list = email_as_python_object.attachments
body = email_as_python_object.body # be it html or whatever
And by the way I have tried default email module which comes with python but thats not handy for all the cases. And even I need to deal with mail attachments manually(which I hate). I just need a simple way to deal with cases like these(I was wondering how postfix understands a email received. ie.. how it automatically figures out different headers,attachments etc..). Please help me.
You want to have postfix deliver to a local mailbox, and then use a webmail system for people to access that stored mail.
Don't get hung up on postfix - it just a transfer agent - it takes messages from one place, and puts them somewhere else, it doesn't store messages.
So postfix will take the messages over SMTP, and put them in local mail files.
Then IMAP or some webmail system will display those messages to your users.
If you want the mail integrated in your webapp, then you should probably run an IMAP server, and use python IMAP libraries to get the messages.
First of all, Postfix mail routing rules can be very complex and your presumably preferred solution involves a lot of trickery in the wrong places. You do not want to accidentally show some user anothers mails, do you? Second, although Postfix can do almost anything, it shouldn't as it only is a MDA (mail delivery agent).
Your solution is best solved by using a POP3 or IMAP server (Cyrus IMAPd, Courier, etc). IMAP servers can have "superuser accounts" who can read mails of all users. Your web application can then connect to the users mailbox and retreive the headers and bodys.
If you only want to show the subject-line you can fetch those with a special IMAP command and very low overhead. The Python IMAP library has not the easiest to understand API though. I'll give it a shot (not checked!) with an example taken from the standard library:
import imaplib
sess = imaplib.IMAP4()
sess.login('superuser', 'password')
# Honor the mailbox syntax of your server!
sess.select('INBOX/Luke') # Or something similar.
typ, data = sess.search(None, 'ALL') # All Messages.
subjectlines = []
for num in data[0].split():
typ, msgdata = sess.fetch(num, '(RFC822.SIZE BODY[HEADER.FIELDS (SUBJECT)])')
subject = msgdata[0][1].lstrip('Subject: ').strip()
subjectlines.append(subject)
This logs into the IMAP server, selects the users mailbox, fetches all the message-ids then fetches (hopefully) only the subjectlines and appends the resulting data onto the subjectlines list.
To fetch other parts of the mail vary the line with sess.fetch. For the specific syntax of fetch have a look at RFC 2060 (Section 6.4.5).
Good luck!
I'm not sure that I understand the question.
If you want your remote web application to be able to view users' mailbox, you could install a pop or imap server and use a mail client (you should be able to find one off the shelf) to read the emails. Alternatively, you could write something to interrogate the pop/imap server using the relevant libraries that come with Python itself.
If you want to replicate the mail to another machine, you could use procmail and set up actions to do this. Postfix can be set up to invoke procmail in this wayy.

Categories