Tuesday, October 4, 2011

Machine generated mails

We have seen many sites, which send confirmation mails on registration. They are the system generated mails and it will not send any reply if we send mails to that address. I also think about setting a machine generated mail for my git wiki. If a user register into git wiki or he creates an article, the machine will generate a mail and send it to him.

We need to import smtplib for this.

import smtplib

We also need to set some information in our python code basically regarding the smtp server,port, mail id, password etc. I used a gmail id for this purpose. I created a mail id noreplygitwiki@gmail.com. Now I need to set the smtp server of gmail and its port number. Its smtp server address is smtp.gmail.com and port number is 587. Use the following code outside all classes.

web.config.smtp_server = 'smtp.gmail.com'
web.config.smtp_port = 587
web.config.smtp_username = 'noreplygitwiki@gmail.com'
web.config.smtp_password = '12345678'
web.config.smtp_starttls = True

You need to enter your mail id and password instead. Now it is very simple to generate a mail. Just use the following code wherever or whenever you need,

web.sendmail('noreplygitwiki@gmail.com', session.email, subject, message)

Here first parameter is the sender email id. Second parameter is destination mail address. Third one is the email subject and the last one is the message content. You need to set the message content very carefully. Show your creativity here. Because it decides the beauty of your message. I used the following code for mailing the user on registration.

message = ("Dear %s,\n\n\t Thanks for joining our site.\n\t Your Account Details : \n\n\t\t\t username = %s\n\t\t\t password = %s\n\n\t Keep updated with regular articles.\n\n\t Thank you.\n\t Admin.\n\t Git Wiki\n\t\t\t\tBlog : http://ajaysoman.blogspot.com/" % (fi.fname,fi.email,fi.pswd1))

It will be displayed as,

Dear Ajay,

Thanks for joining our site.
Your Account Details :

username = ajaysoman007@gmail.com
password = 12121212

Keep updated with regular articles.

Thank you.
Git Wiki
Blog : http://ajaysoman.blogspot.com/

You can try for better mail formats. Try for it in your projects.



No comments:

Post a Comment