命令行可以运行,CGI方式却不能运行的问题
代码如下:
#!/usr/bin/python
######
def send_email (recipients):
import smtplib
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
import email
import time
femail='我的邮箱地址' #发件人地址
temail=recipients #收件人地址
msg=MIMEMultipart()
msg['From'] = femail
msg['To'] = temail
a="test"
msg['Subject'] = a
msg['Reply-To'] = femail
msg['Date'] = time.ctime(time.time())
msg['X-Priority'] = '''3'''
msg['X-MSMail-Priority'] = '''Normal'''
msg['X-Mailer'] = '''Microsoft Outlook Express 6.00.2900.2180'''
msg['X-MimeOLE'] = '''Produced By Microsoft MimeOLE V6.00.2900.2180'''
body=email.MIMEText.MIMEText('''<a>test</a>''',_subtype='html',_charset='gb2312')
msg.attach(body)
s = smtplib.SMTP('SMTP服务器')
s.login('用户名','密码')
s.sendmail(femail,temail,msg.as_string())
s.close()
r='某个邮箱地址'
send_email(r)
这个初衷是想从别的页面得到用户输入的邮箱地址然后给他发邮件,我这里先测试了一下,直接命令行运行是没有问题的,但在网页中用到时error_log提示以下错误:
Fri Nov 03 20:48:30 2006] [error] [client 162.105.75.235] File
"/var/www/html/cyberchair/cgi-bin/mail.py", line 35, in ?
[Fri Nov 03 20:48:30 2006] [error] [client 162.105.75.235] send_email(r)
[Fri Nov 03 20:48:30 2006] [error] [client 162.105.75.235] File
"/var/www/html/cyberchair/cgi-bin/mail.py", line 30, in send_email
[Fri Nov 03 20:48:30 2006] [error] [client 162.105.75.235] s =
smtplib.SMTP('SMTP服务器地址')
[Fri Nov 03 20:48:30 2006] [error] [client 162.105.75.235] File
"/usr/lib/python2.4/smtplib.py", line 241, in __init__
[Fri Nov 03 20:48:30 2006] [error] [client 162.105.75.235] (code,
msg) = self.connect(host, port)
[Fri Nov 03 20:48:30 2006] [error] [client 162.105.75.235] File
"/usr/lib/python2.4/smtplib.py", line 289, in connect
[Fri Nov 03 20:48:30 2006] [error] [client 162.105.75.235] for res
in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
[Fri Nov 03 20:48:30 2006] [error] [client 162.105.75.235]
socket.gaierror: (-3, 'Temporary failure in name resolution')
[Fri Nov 03 20:48:30 2006] [error] [client 162.105.75.235] Premature end
of script headers: mail.py
提示临时域名解析失败,如果我把SMTP服务器换成该服务器的IP地址的话,又会提示:
[Sat Nov 04 20:26:57 2006] [error] [client 162.105.75.235] Traceback
(most recent call last):
[Sat Nov 04 20:26:57 2006] [error] [client 162.105.75.235] File
"/var/www/html/cyberchair/cgi-bin/mail.py", line 35, in ?
[Sat Nov 04 20:26:57 2006] [error] [client 162.105.75.235] send_email(r)
[Sat Nov 04 20:26:57 2006] [error] [client 162.105.75.235] File
"/var/www/html/cyberchair/cgi-bin/mail.py", line 30, in send_email
[Sat Nov 04 20:26:57 2006] [error] [client 162.105.75.235] s =
smtplib.SMTP('162.105.129.22')
[Sat Nov 04 20:26:57 2006] [error] [client 162.105.75.235] File
"/usr/lib/python2.4/smtplib.py", line 241, in __init__
[Sat Nov 04 20:26:57 2006] [error] [client 162.105.75.235]
[Sat Nov 04 20:26:57 2006] [error] [client 162.105.75.235] (code, msg) =
self.connect(host, port)
[Sat Nov 04 20:26:57 2006] [error] [client 162.105.75.235] File
"/usr/lib/python2.4/smtplib.py", line 303, in connect
[Sat Nov 04 20:26:57 2006] [error] [client 162.105.75.235]
[Sat Nov 04 20:26:57 2006] [error] [client 162.105.75.235] raise
socket.error, msg
[Sat Nov 04 20:26:57 2006] [error] [client 162.105.75.235] socket
[Sat Nov 04 20:26:57 2006] [error] [client 162.105.75.235] .
[Sat Nov 04 20:26:57 2006] [error] [client 162.105.75.235] error
[Sat Nov 04 20:26:57 2006] [error] [client 162.105.75.235] :
[Sat Nov 04 20:26:57 2006] [error] [client 162.105.75.235] (13,
'Permission denied')
[Sat Nov 04 20:26:57 2006] [error] [client 162.105.75.235]
[Sat Nov 04 20:26:57 2006] [error] [client 162.105.75.235] Premature end
of script headers: mail.py
不知道为什么提示'Permission denied'
在命令行下是没有这些问题的,邮件可以正常发送
希望有经验的人可以给予一些指点