Using mutt to send a few hundred of emails using python script

This post was first posted on http://ranvir.xyz/blog/using-mutt-to-send-a-few-hundred-of-emails-using-python-script/. Read over there for the better reading experience.

GitHub repository:

https://github.com/singh1114/csiMuttScript

So this was my first encounter with the powerful email client MUTT. So, according to the official website of mutt, “All web clients sucks but this one sucks less”. This is the best way explaining something.

So, let’s go through the build up and share the story of WHY.

What was the reason to use MUTT

So I went to my friend’s room in the hostel and he was asking his roommate to send emails to a list of people. He had the database of the people with him which he was going to use to send the emails.

He told me that he have the database of the registered student and in which all the data is present. He gave the database to his roommate and asked him to send the email to each one of them. I asked him about the procedure that he was going to follow. Very sincerely he told me that he will pick all of the emails one by one and send them the email.

I laughed to this as I knew there were more than a hundred people in the list. I knew that if he went forward by following this procedure he might not be able to complete it till morning. So I asked him to allow me to do the work for him. I assured him that I will write a program that will complete the work faster.

So he gave me the database and I started working on it. The first and very obvious thing that can come to someone’s mind is that we can convert the database into CSV(comma separated values) file and import it using google mail which allows direct importing of contacts from CSV file.

Note: Now that the work is done I am thinking that it could had been a good solution.

I asked my friend and he said that they don’t want everyone to know about each other’s email ID.

He told me that what he want to do is that he can send the email in a loop. So that the privacy of each member is preserved.

I remembered that Rai sir used to talk about MUTT, using which we can send emails through the terminal and if we can do something using the terminal, we can configure it according to our needs.

So, I went forward to install MUTT and started reading about it. Finally, I found a great article which I followed during the build process. The following link will take you to the article.

http://nickdesaulniers.github.io/blog/2016/06/18/mutt-gmail-ubuntu/

After that, I wrote a small program in python that can read each line of CSV file and differentiate between all the values in the file and stores the email in a variable. First of all searched about how to execute bash commands in a python program. “subprocess” was the solution to my problem so I imported it.

I tested it by sending a few emails to my friends. It worked fine. Then I wrote another file and asked my friend about the content that he wanted to send to the list. He told me a few things and I used my open source capabilities to write the content (Collaborating in a big project is initiated by a good conversation).

I tested the script and found that for every mail we have to tell the system that whatever we were doing was correct and we want to go forward. So there was a lot of pressing “Enter” key. I wanted to remove that too but wasn’t able to in the time. I explain my friend on how to carry out the procedure as I was in no mood of doing all that clicks.

When he ended up sending all the emails. I found the solution and corrected the single character in my file. This commit shows the way in which I solved the problem.

https://github.com/singh1114/csiMuttScript/commit/75f2eccb6928fdb3994390283ebe205cf17d4bbb

 

Changing the commit messages that are already pushed

So in this post, we will be talking about the process of changing the commit messages that are already pushed into the remote. But, before talking about changing those messages we must know about the basics commands of GitHub. Please refer to this post, if you have less knowledge about the Git.

Also, it is good to know about the git reset command which is used to reset any changes made by a user in a repo. Use the command git reset –help for more information.

Now let’s dive right into the topic. First thing is to check the changes that you have done in the recent past. For that use the following command.

$ git log 

# This command will show one change in each line
# -5 represent the number of messages that you want to read.
$ git log --oneline -5

Now for changing the commit messages use the amend command. The command is written below.

But before that think that you don’t want to change the last commit message but you want to change a commit message that is somewhat old. Say it is shown at number 5 when you used git log. Use this command to change your head to that commit message.

$ git rebase -i HEAD~5

Next step was hard to figure out but I found it by some proper reading. Now this will open a panel for you where you can make changes for you commit messages. This panel will look something like this.

pick 2f8e279 another message
pick a04482b One more another message 2
pick 97d5145 no new changes available

# Rebase 3bb9b41..a1c796c onto 3bb9b41 (3 command(s))
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
...few other lines

Now if you look at the comments properly you will come to know about how to do this. So as you can see that in the first three lines commit messages are written. In the beginning of each line “pick” is written and in the comments, we can see the description. So,  remove the word pick and change it to the word that you want to use for yourself. I removed pick in all the three lines and changes it with r.

I am not using “e” as it was not required in my case. So, if anyone out there uses it, share it in the comments.

After that, you will be asked to change the commit message one by one in the end.

Now say you only want to change a single commit message (last commit message) it is a bad choice to use something like rebase. You can use this command rather.

$ git commit --amend

This command will open the editor in the terminal. Make the changes and save it. Now is the time to push the changes to the remote. Use the following command. Keep in mind to include that + sign, otherwise, your changes will be ignored.

 $ git push origin +branch_name

Thanks for reading this post. Keep reading and Happy coding.

Automating the action of creating the presentations

GitHub repository : https://github.com/singh1114/automatingPresentations/

8th February 2017

I came back from my so-called holidays and sir were ready to excite me with a new task on the table. As we know that the progress in the chatting app is really slow, that is why I decided to work on this side project. So the basic aim of the project is to create the presentations that are good enough to get the work done. We didn’t want to create something extraordinary. The basic aim of the project is to create a markdown presentation from a Powerpoint presentation. Sir were able to get whole of the text out from the presentation and now the job is to create something is good enough with the formatting.

The file that contains the whole of the text, differentiate each slide with some references in the end. So differentiating two different slides will be easy. Next task will be to figure out what tags to be used for all the content. I saw that all the heading were written in the capitals. But we cannot differentiate between different levels of headings. So this might be done manually.

9th February 2017

Now was the time to think something about the procedure that we were going to follow. I wrote some of the pseudo code to start with.

Read the file:

  whenever the line starts with *:

    read each character:

      if the letters are capital:
        give heading h1

      if the * is first in the group:
        raise the height by 50%

      if there is nothing after *
        do nothing

  if the line starts with References:
    write the code for new slides

We decided to use python for completing the job. So let the work begin.

10th February 2017

I wrote the code for the above-written pseudo code. In the end, the code written was somewhat different from the pseudo code. While writing the code I learned a few things about the file handling in python which I want to share in a quick fashion. First of all, the file handling in python does not require any type of library to be imported.

For opening a file, we can use the function open(). The parameter that we pass are:

  1. The file name
  2. The mode in which we want to open the file

If you know about the file handling in C, then you must know about these modes like “r”, “w”, “a” which stands for read, write and append respectively. The first two words are self-explanatory on the other hand append is used to append the new text to the end of the file.

read() function is used to read the content of the file and write() function is used to write some content to the file. seek() function is used to send position of the file to some other position or check the position of the pointer of reading and writing the text. Do keep in mind to close() the opened file in the end.

Now talking about the script, I used a temporary variable to check for all types of cases but still two cases are not under the hood (There might be more but for now we only know about two of them). They are as follows:

  1. Even if the first word is in upper case it takes it a header text
  2. Paragraphs are not handled properly.

The commit for the code is here.

In the end I wrote some documentation about the way in which this code will be used.

The commit for the documentation is here.

11th February 2017

We were able to resolve the earlier discussed issues. Further information will be shared in some other follow ups.

Please comment on the post if there is some confusion.

Sorting the messages in the _postMessage function

I am trying to sort the messages that I am receiving when I used the getHistory function in the Message Archiving. But till now I haven’t found a success.

They say failures are the greatest teachers. I hope they are true because yet again, I have failed.

A lot of tough words up there. Let’s simplify the situation a little. So from last few posts, I have been talking about XMPP- The protocol for chatting( or message sharing). I did posted them because I was working on a chatting web-client called JSXC. I tried it and found that the client never retrieved messages from the server. So my work was to implement this feature.

With the help of the main developer, I was able to make some progress and made the messages appear into the chat box. As the developer told me that the messages are not going to be sorted so I have to make some changes in the function such that the sorted messages can appear in the chat window.

In this post I want to discuss the way in which I wrote the function without showing the real code so that we can keep everything simple. First of all, I wrote the getHistory function such that if there is message in the server for a particular person( we call it jid in terms of XMPP), make a object having everything about the message and send it into the function named postMessage which in return is responsible of printing these messages into the chat box.

Now, I did checked personally that the messages are stored properly on the server and they are retrieved properly one at a time. Whenever a message is retrieved from the server the message is given to the function for printing. But to my surprise the messages order does not remain intact and messages printed in the chat box are in no particular order.

So I decided to make some changes to my function. I changed the function and now the function saves all the messages for a partcular person in an array and when all the messages are retrieved they are sent to the function for printing. I used a loop to make it work but as far as I think it is not working. I don’t know why.

For more details stay tuned. Also this week was great in terms for my typing speed. I wrote whole of this post without giving a single look to the keyboard. Also the speed have reached a new level of 30 wps. Which is a great progress.