Archive

Posts Tagged ‘email’

email notification from a script

June 15, 2016 1 comment

Problem
You want to send an email to yourself from a script.

Solution
You can find here how to do it from a Bash script. That solution uses the mailx command.

Here is a simple Python wrapper for the mailx command:

#!/usr/bin/env python3
# coding: utf8

import os

DEBUG = True
# DEBUG = False

class NoSubjectError(Exception):
    pass

class NoRecipientError(Exception):
    pass

def send_email(to='', subject='', body=''):
    if not subject:
        raise NoSubjectError
    if not to:
        raise NoRecipientError
    #
    if not body:
        cmd = """mailx -s "{s}" < /dev/null "{to}" 2>/dev/null""".format(
            s=subject, to=to
        )
    else:
        cmd = """echo "{b}" | mailx -s "{s}" "{to}" 2>/dev/null""".format(
            b=body, s=subject, to=to
        )
    if DEBUG:
        print("#", cmd)
    #
    os.system(cmd)

def main():
    send_email(to="to@email.com",
               subject="subject")
    #
    send_email(to="to@email.com",
               subject="subject",
               body='this is the body of the email')

#############################################################################

if __name__ == "__main__":
    main()

You can also find this code as a gist.

Advertisements
Categories: python Tags: , ,

find out a github user’s (hidden) email address

January 11, 2016 Leave a comment

Problem
You want to contact someone on GitHub but (s)he doesn’t indicate his/her email on the profile page. What to do?

Solution
Figure it out :) The manual steps are indicated here. I made a Python script of it that can be found here: https://github.com/jabbalaci/Bash-Utils/blob/master/github_user_email.py .

Usage example:

$ ./github_user_email.py 
Github info (username or repo. link): https://github.com/jabbalaci/Bash-Utils                                 
jabba.laci@gmail.com

If you want to hide your email address, then here are some tips.

Categories: python Tags: ,

Send e-mails via Gmail

February 19, 2011 1 comment

The following entry is based on the excellent post of Kutuma. Credits go to him.

I made some minor modifications: (1) conversion to module, (2) “From:” address shows the name too.

send_email_via_gmail.py module:

#!/usr/bin/env python

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders
import os

gmail_user = "username@gmail.com"
gmail_name = "User Name <username@gmail.com>"
gmail_pwd = "userpassword"

def mail(to, subject, text, attach):
   msg = MIMEMultipart()

   msg['From'] = gmail_name
   msg['To'] = to
   msg['Subject'] = subject

   msg.attach(MIMEText(text))

   part = MIMEBase('application', 'octet-stream')
   part.set_payload(open(attach, 'rb').read())
   Encoders.encode_base64(part)
   part.add_header('Content-Disposition',
           'attachment; filename="%s"' % os.path.basename(attach))
   msg.attach(part)

   mailServer = smtplib.SMTP("smtp.gmail.com", 587)
   #mailServer = smtplib.SMTP_SSL("smtp.gmail.com", 465)   # didn't work for me
   mailServer.ehlo()
   mailServer.starttls()
   mailServer.ehlo()
   mailServer.login(gmail_user, gmail_pwd)
   #mailServer.sendmail(gmail_user, to, msg.as_string())   # just e-mail address in the From: field
   mailServer.sendmail(gmail_name, to, msg.as_string())   # name + e-mail address in the From: field
   # Should be mailServer.quit(), but that crashes...
   mailServer.close()

if __name__ == "__main__":
    mail("send-mail-to-this-person@address.com",
       "Hello from Python!",
       "This is an e-mail sent with Python.",
       "/tmp/some-image.jpg")

How to use it in a script:

#!/usr/bin/env python

import send_email_via_gmail as gmail

gmail.mail( "send-mail-to-this-person@address.com",
                    "Subject of the mail",
                    "Body of the mail.",
                    "/tmp/report.pdf" )

Update (20130113): pushed on github.

Alternatives (20130810)
Recently I came across some alternatives that are easier to use:

  • envelopes
  • gmail (I tried it and it works well. I used it for reading Gmail messages.)
Categories: python Tags: , ,