Saturday, April 7, 2018

Write Python Code to Send Email from Gmail

sendmail

In this tutorial, we will write python code to send to multiple emails. Followings are the scenario:

  • Emails will be listed in a text file
  • The Python code will read the emails and send email to each address
  • The email used is gmail

Allow Less Secure App

You must allow low secure app in your google account setting before you can run the code. In order to do that, go to https://myaccount.google.com/?pli=1, navigate to sign in and security, scroll down and enable the allow less secure apps

Write the Python Code

Now we go to the most interesting part i.e. writing the code.

Simplest Code

We will start with a very simple code to send to one account

#!/usr/bin/python

import smtplib

fromaddr = "mymail@gmail.com"
frompass = "mypass"
text = "this is the body"

email = "targetmail@gmail.com"

# login and send the email
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, frompass)
server.sendmail(fromaddr, email, text)
server.quit()

print "sent to " + email

Say you save the file to be sendmail.py. Now you can run it in terminal

$ chmod +x sendmail.py
$ ./sendmail.py
sent to targetmail@gmail.com

When you open your email, you will find a new email with (no subject) with the body as this is the body

Complete Email Content

As you notice, your program will send email without subject. To put complete email content, you should modify your program as follows

#!/usr/bin/python

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
from email import encoders

fromaddr = "mymail@gmail.com"
frompass = "mypass"
email = "targetmail@gmail.com"

msg = MIMEMultipart()
msg['from'] = fromaddr
msg['to'] = email
msg['Subject'] = "test mail"
body = "this is the body"
msg.attach(MIMEText(body, 'plain'))

text = msg.as_string()

# login and send the email
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, frompass)
server.sendmail(fromaddr, email, text)
server.quit()

print "sent to" + email

You can run it again

$ ./sendmail.py
sent to targetmail@gmail.com

In the target email, you will find that now you have the subject. If you want to add attachment with your email, you can add some lines as follows. But, before, create a file named emails. We will use the file as the attachment. If you use *nix, you can create the file using the following command

$ touch emails

Now add some lines in your Python code as follows

#!/usr/bin/python

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

fromaddr = "mymail@gmail.com"
frompass = "mypass"
email = "targetmail@gmail.com"

msg = MIMEMultipart()
msg['from'] = fromaddr
msg['to'] = email
msg['Subject'] = "test mail"
body = "this is the body"
msg.attach(MIMEText(body, 'plain'))

# prepare the attachment
part = MIMEBase('application', "octet-stream")
part.set_payload(open(os.getcwd() + "/emails", "rb").read())
Encoders.encode_base64(part)

part.add_header('Content-Disposition', 'attachment; filename="emails.txt"')

# attach into email
msg.attach(part)

# convert msg into string
text = msg.as_string()

# login and send the email
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, frompass)
server.sendmail(fromaddr, email, text)
server.quit()

print "sent to " + email

Now run it again

$ ./sendmail.py
sent to targetmail@gmail.com

You will find that the email now has the attachment. The name of the attachment is email.txt. As we put nothing in the emails file, the file content sent through the email will be just an empty file.

Read Target Email from File

Now we will make it more interesting by reading the target email from the text file. Create a text file using your favourite editor. In my case, I prefer to use nano. In my case, I did the followings

$ nano emails

My list of emails is something like this

firstmail@something.com
secondmail@something.com
thirdmail@something.com

Your Python code will be like this

#!/usr/bin/python

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

fromaddr = "mymail@gmail.com"
frompass = "mypass"
email = "targetmail@gmail.com"

msg = MIMEMultipart()
msg['from'] = fromaddr
msg['Subject'] = "test mail"
body = "this is the body"
msg.attach(MIMEText(body, 'plain'))


# prepare the attachment
part = MIMEBase('application', "octet-stream")
part.set_payload(open(os.getcwd() + "/emails", "rb").read())
Encoders.encode_base64(part)

part.add_header('Content-Disposition', 'attachment; filename="emails"')

# attach into email
msg.attach(part)

f = open(os.getcwd() + "/emails")

for email in f:
    msg['To'] = email

    text = msg.as_string()

    # login and send the email
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login(fromaddr, frompass)
    server.sendmail(fromaddr, email, text)
    server.quit()
    print('sent to ' + email)

print "Sending email finished"

When you run the Python code, it will be

$ ./coba.py 
sent to firstmail@something.com

sent to secondmail@something.com

sent to thirdmail@something.com

Sending email finished

Thank you for reading this tutorial. Hope it is useful for you :D

Monday, July 3, 2017

Procedure for applying dependent pass in Malaysia

When you study or work in Malaysia and want to bring along your families, you will have to apply dependent pass for your family. Dependent pass can be applied for your spouse, children, parents, or adopted children.

Document Checklist

I got a checklist from immigration office. So, rather than you have to go there only to get the checklist, it will be much better if I write the checklist for you. You can also download each form needed. Followings are the checklist for long term application of social visit pass for dependent/student's companion:
  1. IMM 12 for new application or IMM 55 for extension
  2. IMM 38
  3. Proof of relationship, verified by Embassy/Representative/High Commission Office (original and copy)
    • Marriage certificate (husband and wife)
    • Birth certificate (children)
    • Custody of children letter/adopted child certificate (for step children and adopted children only)
    • Birth certificate of student pass holder (when applying biological father or mother). Citizen of Republic of China have to get proof of relationship from Malaysian representative office in China
  4. Letterhead from institution including information regarding the position in institution (if sent by institution representative)
  5. Confirmation letter from institution
  6. Copy of passport page that contain information and valid pass
  7. Passport copy of student pass holder (page that contain information & latest pass)
  8. Health insurance policy of applicant within the valid year
  9. Prove of the ability to self-sustain in Malaysia
  10. Personal bond (duty stamp RM 10) for each applicant
  11. Bank statement
Some notes regarding the checklist:
  • For renewal process, ignore document 3 and 10
  • Amendment of personal bond is not allowed
  • Passport copy verified by the institution is needed if the applications resides outside the country
  • Applicant/institution should be willing to provide additional documents if being prompted by the immigration officers
  • Original documents should be provided during submission of application
  • Only completed application will be processed

Some more details based on my experience

  • You will need to have photographs of your dependents. The photograph is visa size with white background. Print at least 3 or 5 for spare.
  • Ensure that your documents are correct, including the photograph. If you have more than one children, sometime you may misplace their photograph.
  • If your kid was born outside Malaysia, you will need to verify the birth certificate in the respective embassy. If your kid(s) was/were born in Indonesia, you have to verify the birth certificate in KBRI and it took two days to finish. If you want to be faster, you can go to Konjen in Johor or Penang, and it will take only one day to finish. However, if your kid was born in Malaysia, you just need to make a copy of his/her birth certificate. You can refer on how to get birth certificate for kid born in Malaysia in this post (sorry it is in Bahasa, you may use Google Translate if you need to).
  • You will also need to fill security bond form. You then need to put 10 ringgit duty stamp on the form
  • You can get the duty stamp in POS LAJU (maybe you can get it elsewhere, but I went to poslaju to get it)
  • You need to go to Lembaga Hasil for the security bond form to be stamped
  • To prove the ability to self-sustain in Malaysia, you can use your offer letter showing the scholarship during your study
  • The bank statement needs to be verified by the respected bank. For example, if you have CIMB account, you have to go to CIMB to verify the bank statement. However, if you are Indonesian like me, you can use your bank book
  • If your supervisor is Malaysian, like me, ask him to be your guarantor. Otherwise, if you supervisor is not Malaysia, you may ask your Malaysian friend to be the guarantor
  • In the security bond form, it is said that you have to provide some amount of money as security bond. However, you don't need to provide the money as long as you don't make any trouble in Malaysia. That amount of money is just to say that it will be available if anything goes wrong.
Please note that the policy or the procedure may change in the future. So, if you experience it differently, appreciate if you share your experience in the comment section.

Wednesday, September 28, 2016

Modify and build your own SikuliX

In this tutorial, I show steps to modify and edit sikulix under OSX environment. This should not be different with other *nix environment. However, for windows environment, you may need to adjust yourself.

Modify your code

  • Clone from the repository
  • Open the repository as maven project in netbeans. You can easily use this button to open the project open
  • You will see that the cloned project appeared in open windows like the following
  • Open the project. It will be like this
  • Modify any part you want to modify
  • Clean and build the maven project. If you open other sub project of sikulix, make sure that you clean and build only the main sikulix maven project

Create Installer

Go to File Finder in the repository folder. You will see one file named runSetup. Using your terminal and cd to the folder and run the setup.

$ runSetup

It will bring you to SikuliX installation. Please remember that if you build yourself, ALWAYS do it in offline mode, means that you are not supposed to download anything during the installation. In other words, because SikuliX will need some packages/libraries, before installing SikuliX using your own build, you MUST install SikuliX in your computer by running normal build first. You can download the normal build here

Back to runSetup. You will see SikuliX installation window as follows

Before you continue, please pay attention to in this folder part. That part shows you where you can find your new build. To make myself clearer, I mean this part

It shows me that I can find the build in /Users/arwankhoiruddin/Library/Application Support/Sikulix/SikulixSetup folder

Then follow the instructions carefully. I repeat once more, please do not download anything during this installation.

Now go to SikulixSetup folder. You will find your new and fresh build there

Just move or copy SikuliX to /Applications folder and voila! Your newly build sikulix is ready to use