Send email in python using subprocess module
Question: I wrote a script to retrieve weather report from a website and send it to my
girfriend in the morning.

Using Gmail. Of course I can send it using my Postfix server. Here is the
script.

What **I'm not sure is how to use Popen() function in the situation with so
many arguments.**

I can send the mail using the command.

`$ mail -s "おお様からの天気予報" abc@gmail.com < foo`

    
    
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    from bs4 import BeautifulSoup
    import urllib2
    import subprocess
    
    weather_url = "http://www.weather.com.cn/weather/101020100.shtml"
    f=urllib2.urlopen(weather_url)
    html = f.read()
    soup = BeautifulSoup(html)
    
    content = soup.title.string
    
    with open("foo","w") as mail:
        mail.write(content.encode('utf-8'))
    
    command_line = 'mail -s "おお様からの天気予報" abc@gmail.com < foo'
    
    li = command_line.split()
    
    process = subprocess.Popen(li, shell=True)
    
    returncode = process.wait()
    

The content of the weather report is in the `foo` file. Can somebody tell me
how to use `Popen()` with so many arguments?

I tried a lot.

This script just doesn't seem to work.


Answer: You don't need to pass an argument list when you use `shell=True` you can just
pass an argument string...

    
    
    command_line = 'mail -s "おお様からの天気予報" abc@gmail.com < foo'
    process = subprocess.Popen(command_line, shell=True)
    

Or.. you can not use the shell to interpret your arguments and pass a list...

    
    
    command_line = 'mail -s "おお様からの天気予報" abc@gmail.com < foo'
    li = command_line.split()
    process = subprocess.Popen(li)
    

But you can't pass an argument list _and_ use the shell to interpret it.

Based on the nature of your command, I would recommend passing a string to the
shell to interpret. (the first option)

