用了三个文件实现,部署在linux的apache下。
照理说用html+php即可。但是我初学php,用php发送邮件死活成功不了。
而手上正好有一个现成的python发送邮件的文件。
所以干了这蠢事。
webmail.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>My Web Mail</title> <link rel="stylesheet" type="text/css" href="style.css" /> </head> <body> <h2>My Web Mail</h2> <form method="post" action="webmail.php"> <label for="username">Username:</label> <input type="text" id="username" name="username" /><br /> <label for="password">Password:</label> <input type="password" id="password" name="password" /><br /> <label for="receiver">Receiver:</label> <input type="text" id="receiver" name="receiver" /><br /> <label for="subject">Subject:</label> <input type="text" id="subject" name="subject" /><br /> <label for="content">Content:</label> <textarea id="content" name="content"></textarea><br /> <input type="submit" value="Send" name="submit" /> </form> </body> </html>
webmail.php
<html> <head> </head> <body> <h2>Hello Webmail.php</h2> <?php $username = $_POST['username']; $password = $_POST['password']; $receiver = $_POST['receiver']; $subject = $_POST['subject']; $content = $_POST['content']; passthru('python ./webmail.py' . ' ' . $username . ' ' . $password . ' ' . $receiver . ' ' . $subject . ' ' . $content); ?> </body> </html>
webmail.py
#!/usr/bin/env python #-*-encoding: utf-8 -*- #编码方式utf-8 import sys,smtplib from email.mime.text import MIMEText from email.header import Header from email.mime.multipart import MIMEMultipart smtpserver = 'smtp.163.com' #邮件服务器地址 username=sys.argv[1] #print username + '\n' password=sys.argv[2] #print password + '\n' receiver=sys.argv[3] #print receiver subject =sys.argv[4] #print subject text =sys.argv[5] +'\n' sender=username #print sender msg=MIMEMultipart('alternative') #发送邮件的库 msg['Subject']=subject #text='你好' #邮件内容 content = MIMEText(text, 'plain','utf-8') #邮件内容是平文本,utf-8编码形式 msg.attach(content) #create the attachment #attfile='buptsnow.jpg' #att=MIMEText(open(attfile,'rb').read(),'base64', 'utf-8') #添加附件,为北邮下雪的图 #att["Content-Type"] = 'application/octet-stream' #att["Content-Disposition"] = 'attachment; filename="buptsnow.jpg"' #msg.attach(att) smtp = smtplib.SMTP() smtp.connect(smtpserver)#连接smtp服务器 smtp.login(username, password)#登录授权 smtp.sendmail(sender, receiver, msg.as_string()) smtp.quit() #发送结束 print 'Congratulations!Send WebMail Successfully!'