Send an email directly with only one URL! Super practical!

Source: Internet
Author: User
Tags sdo

A small python was deployed on Gae a few days ago to regularly send weather forecasts to mobile phones.ProgramThe principle is very simple: capture the weather website, parse the page to get the weather information, and then use the Gae mail function to send an email to your 139 mailbox, the phone will be able to receive the weather forecast for free.

Later I found that there was a limit on the number of emails sent to Gae. Only 100 emails can be sent in a month. If I sent one email to my dad, mom, and me a day, more than 90 emails will be sent in a month, I have already spent a few tests. If I want to send weather forecasts to other friends and family, or I want to receive some other email information, how can I use them? This is the problem...

I originally wanted to directly call Python's smtplib module on Gae to send an email and found that it was not usable. Using RZ ....

As a result, find various solutions -_-

Solution 1: directly parse the email login page and log on and send an email ----> too troublesome, depending on the service website

Solution 2: Push messages to mobile phones using "Courier" ----> only on smart phones, not for promotion

Solution 3: a website that directly sends emails without logon is similar to a ten-minute mailbox. However, this mailbox can only receive emails but cannot send emails, even if it can be sent, it is easy to identify as spam, unreliable.

Solution 4: the use of Feixin to send ----> the Feixin protocol has changed rapidly. Currently, popular online Feixin modules generally need to enter verification codes. I don't know which fairy can identify the verification codes directly? (WAP fetion homepage)

Solution 5: Use the mail list provided by Gmail, 163, QQ, etc. ----> group sending is acceptable, but if the mail content to each person is different, it will not work.

Solution 6: bid your brains and continue to think ~~~~

After a while, a magical idea came into being: why not build an email sending environment on your server? What I need is only the mail sending function. To make it easier for external users to use the mail sending function, what is the simplest? Is it easy to directly enter a URL in the address bar of a browser to execute the mail sending command? I designed it. Of course, the website requires necessary information, such as the recipient, email subject, and content. A URL like this, "Why? At that time, you only need to let the Gae program access this website, and then the mail function will be realized! You may say that it is too insecure to directly get the website, and the information in it is displayed in plain text. Hey, it doesn't matter. Anyway, I don't need to send this private information. Hackers like weather forecasts can just cut it apart. If you are interested, change it to post!

Now you have an idea. The next step is to implement this idea! I chose the simplest web module in Python: Web. PY to receive external requests, parse the request, get the mailbox, subject, and body, and then call the function of sending the email to send the email ~ This is simple.CodeRight

# ! /Usr/bin/ENV Python
# Coding = UTF-8
Import Web
Import Urllib
Import Logging
From Sendmail Import Send_mail
URLs = (
# '/(. *)', 'Hello ',
' /Mailto /(.*) ' , ' WebMail '
)

APP = web. Application (URLs, globals ())

Logger = logging. getlogger ()
Logger. setlevel (logging. Debug)
FH = logging. filehandler ('Webmail. Log')
FH. setlevel (logging. Debug)
Formatter = logging. formatter ('% (Asctime) S-% (Message) S')
FH. setformatter (formatter)
Logger. addhandler (FH)

Class Webmail:
_ Doc __ ='''
Send mail to anyone!
Usage:
Http://xxx.com/mailto/?mail_address#/#subject#/#body}
Example:
Http://xxx.com/mailto/aaaa@qq.com/subject/body/
Multi-address:
Http://xxx.com/mailto/aaaa@qq.com/bbbb@163.com/subject/body/
'''
Def Get (self, name ):
Logger.info (name)
Info = urllib. Unquote (name). Split (' / ' )
Info = [I For I In Info If I! = '' ]
If Len (Info) <3:
Return Webmail._ Doc __
Try :
Send_mail (info [:-2], info [-2], info [-1])
Except :
Return ' Failed send mail '
Print Info
Render = web. template. Render ( ' ./ ' )
Return Render. pages (info [:-2], info [-2], info [-1])

If _ Name __='_ Main __':
App. Run ()

Application = app. wsgifunc ()

Web. py is simple and small. In the code, get is used to receive external requests. The obtained requests are in the name, encoded, and divided, in this way, the info list contains the information we need. To stop empty elements in the info list, use a built-in loop of the List to delete empty elements. That's cool! Haha! Next, judge whether the information is complete. If it is less than 3, it will be incomplete. Let's take a look at the instructions and return "_ Doc. If the information is correct, send a message. For convenience, I have made a simple definition of the information. The last information is the text, and the last and last information is the topic, the last two steps forward are the recipient address! Haha, how is it, simple? Then, call the mail sending function to send emails. For ease of use, I encapsulated the Sendmail function:

# Coding = UTF-8
Def Send_mail (to_list, sub, content ):
Import Smtplib
From Email. Mime. Text Import Mimetext
Mail_host = " Smtp.163.com " # Set Server
Mail_user = "" # User Name
Mail_pass = "" # Password
Mail_postfix = " 163. com " # Suffix of the sender

Me = " Groupmail " + " < " + Mail_user + " @ " + Mail_postfix + " > "
MSG = mimetext (content, _ subtype = ' Plain ' , _ Charset = ' Gb2312 ' )
MSG [ ' Subject ' ] = Sub
MSG [ ' From ' ] = Me
MSG [ ' To ' ] = " ; " . Join (to_list)
Try :
Server = smtplib. SMTP ()
Server. Connect (mail_host)
Server. login (mail_user, mail_pass)
Server. Sendmail (Me, to_list, MSG. as_string ())
Server. Close ()
Return True
Except Exception, E:
Raise STR (E)

If you want to do the same thing, you may need to fill in the username and password of your mailbox in the sendmail module.

Next, you need to determine whether the email is successfully sent. If it fails, an error message is returned. If it succeeds, a message is returned. I thought about it and output the email sending request submitted by the user, in this way, you can check whether the message is sent correctly. Therefore, the web. the small and powerful template engine in Py comes in handy:

$ Def with (to, sub, body)
< Meta Charset = 'Utf-8' >
< P > Send success! </ P >
< P > To:
$ For C in:
$ C,
</ P >

<P>Subject: $ sub</P>

<P>Body: $ body</P> 

There is no need for too much information, the interface is not too dazzling, simple, just display the display. In the Web. py template, you can write a statement similar to Python. For example, the for loop above outputs each email address in the list, which is cool! It seems better than Django's template engine!

In this way, even if the basic functions are implemented, there will be another major event, hey, deployment! I searched the internet and found that web. py can be deployed together with Apache or nginx. Then I looked for a tutorial and compared it to what I was doing. After half a day, the deployment was not deployed! Tragedy! I don't know why. I remember the deployment was very troublesome. Writing this program is not as long as deploying it! How can this be done ?! I like the simplicity of Python. I hope it is easy to deploy Python programs. I have found a method that is simple and shared with you. I use uwsgi for deployment. This program can be installed as follows: sudo easy_install uwsgi. If python is missing during the installation process. h, you need to install Python-Dev and install it directly with APT-Get. After installation, run the following command: uwsgi -- http: 8080-W test-P 4 -- enable-threads, this command is to deploy the python program "test" to the local port 8080, and send four threads and a master process concurrently. This command supports Python thread debugging, for details, refer to the online materials. The above command is so long that it is not easy to remember. Write it into the start. Sh script!

Later I thought about how to add a logging function and save all the requests using the logging module. After all, this mailbox is public. In case of any exceptions, I can view the logs.

This task is basically completed. Use the powerful markdown to write a description and share it with GitHub! Https://github.com/ma6174/webmail. if you want to download the installation package, check it out.

Then write a blog to promote the http://www.cnblogs.com/ma6174/archive/2012/08/06/2625477.html
Finally, let's write a summary: The above small program only implements a small worker, maybe you can expand the program, such as adding simple authentication, otherwise, anyone can send emails as long as they know the website address. In this case, the spam is full again! Haha, this is not what we hope. What other functions can be implemented, such as remote control? You just need to let the server parse your command and execute it! In other words, if you want to send an email to your friends one day, you are too lazy to log on to your mailbox. Just enter it in the address bar of your browser. Press enter and send it immediately! More highlights created by you!

The following are some references. I found them online and saved them to my MACO notebook. If you are interested, please study them in depth. (By The Way, metako is a good thing to remember !)

Web. py template: https://note.sdo.com/u/ma6174/n/M5cEN ~ K1z0pvnm1xi001ca
Python logging module: https://note.sdo.com/u/ma6174/n/M5cEN ~ K1ZT4FnM1XI001-K

Rapid deployment of Web. py applications with uwsgi: http://note.sdo.com/u/ma6174/n/M5cEN ~ K1zbgpnm1xi001th

Web. py beginner's Guide: http://note.sdo.com/u/ma6174/n/M5cEN ~ K14rcmlx0tq000za

(If you feel that metacenter is not bad, you can register an Olympics. I have an invitation code.634687868481358385)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.