Это коды к статье
Емайл формa, кэпча и антиспам.
Download:
mail2.tar.gz
captcha.css
body {
margin: 0;
padding: 0;
display:inline-block;
}
body:after{
display:none;
content: url(/mail/refresh1.png) url(/mail/refresh2.png);
}
table {
margin: 0;
padding: 0;
height: 40px;
border: 0;
}
td {
margin: 0;
padding: 0;
height: 40px;
border: 0;
outline: 0;
}
img {
display:inline-block;
vertical-align:top;
}
input {
background:url(/mail/refresh1.png) no-repeat;
cursor:pointer;
width: 40px;
height: 40px;
border:0 none !important;
}
input:hover {
background:url(/mail/refresh2.png) no-repeat;
}
input:focus {
border:none !important;
outline: 0 !important;
}
input::-moz-focus-inner {border:0;}
compose.css
body {
background-color: none;
color: white;
font-family: Verdana;
font-size: 14px;
margin: 0;
padding: 0;
}
h1 {
font-family: Tahoma;
font-size: 20px;
}
input {
display:inline-block;
vertical-align:top;
}
table {
margin: 0;
padding: 0;
border: 0;
}
td {
padding-top: 0;
padding-bottom: 0;
display:table-cell;
}
iframe {
margin: 0;
padding: 0;
border: 0;
}
compose.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Compose</title>
</head>
<body>
<!-- === Копируй отсюда === -->
<iframe src="/cgi-bin/compose.py" width="500" height="300" frameBorder="1"></iframe>
<!-- === и досюда ========= -->
</body>
</html>
index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="refresh" content="0;url=/mail/compose.html">
<link rel="canonical" href="/mail/compose.html"/>
<title>Home</title>
</head>
<body>
<a href="/mail/compose.html">Home</a>
</body>
</html>
response.css
body {
background-color: darkmagenta ;
color: white;
font-family: Verdana;
font-size: 14px;
}
h1 {
font-family: Tahoma;
font-size: 20px;
}
response.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<base target="_parent" />
<link rel="stylesheet" type="text/css" href="response.css">
<title>Спасибо</title>
</head>
<body>
<h1>Thank you</h1>Name: aaa<br>Email: a@gmail.com<br>Subject: done<br><br>done
</body>
</html>
settings.txt
path_blocks = /var/www/html/mail/blocks.txt
path_font = /var/www/html/mail/alfphabet.ttf
path_mail_dir = /var/www/html/mail/
path_response = /var/www/html/mail/response.html
path_template = /var/www/html/mail/template.txt
path_tokens = /var/www/html/mail/tokens.txt
N_tokens = 50
url_captcha_css = /mail/captcha.css
url_compose_css =/mail/compose.css
url_mail_dir = /mail/
url_open = /wordpress/index.php/response
code_bg_color = (253, 0, 211, 255)
code_dots_color = (0, 0, 0, 255)
code_dots_n = 3000
code_text_color = (255, 255, 255, 255)
email_to = vasqpupkin@mail.ru
sender_name = Мой секси сайт
smtp_login = moisexisite@gmail.com
smtp_password = T&ldkwtE4P
smtp_port = 587
smtp_security_type = TLS
smtp_server = smtp.gmail.com
mail_form_width = 400
template.txt
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<base target="_parent" />
<link rel="stylesheet" type="text/css" href="response.css">
<title>Спасибо</title>
</head>
<body>
[msg]
</body>
</html>
captcha.py
#!/usr/bin/python
#-*-coding:utf-8-*-
path_settings='/var/www/html/mail/settings.txt'
import cgitb
cgitb.enable()
import cgi, os, base64, ast
import mail_f
path_mail_dir, url_mail_dir, path_tokens, url_captcha_css, code_text_color, code_bg_color, code_dots_color, code_dots_n, path_font = '','','','','','','','',''
if os.path.exists(path_settings):
f=open(path_settings)
s=f.read()
f.close()
L=s.split('\n')
for item in L:
if '=' in item:
l=item.split('=')
var=l[0].strip()
val=l[1].strip()
if var=='path_mail_dir':
path_mail_dir = val
elif var=='url_mail_dir':
url_mail_dir = val
elif var=='path_tokens':
path_tokens = val
elif var=='url_captcha_css':
url_captcha_css = val
elif var=='code_text_color':
code_text_color = ast.literal_eval(val)
elif var=='code_bg_color':
code_bg_color = ast.literal_eval(val)
elif var=='code_dots_color':
code_dots_color = ast.literal_eval(val)
elif var=='code_dots_n':
code_dots_n = int(val)
elif var=='path_font':
path_font = val
else:
print 'Content-type: text/html\n\n'
print '<!doctype html>'
print 'Not found: settings.txt'
sys.exit()
T = mail_f.Code(path_mail_dir, url_mail_dir, code_text_color, code_bg_color, code_dots_color, code_dots_n, path_font)
path_out=T[0]
code_txt=T[1]
with open(path_out, "rb") as image_file:
s = base64.b64encode(image_file.read())
with open(path_tokens, "a") as f:
f.write(code_txt+'\n')
if os.path.exists(path_out):
os.remove(path_out)
img='<img src="data:image/png;base64,'+s+'" height="40" width="120" alt="" title="Enter code">'
print 'Content-type: text/html\n\n'
print '<!doctype html>'
print '<html lang="en">'
print '<head>'
print '<meta charset="utf-8">'
print '<link rel="stylesheet" type="text/css" href="'+url_captcha_css+'">'
print '<title>Captcha</title>'
print '</head>'
print '<body>'
print '<form action="/cgi-bin/captcha.py" method="post">'
print '<table>'
print '<tr><td>'
print img
print '</td><td>'
print '<input type="submit" name="" value="">'
print '</td></tr></table>'
print '</form>'
print '</body>'
print '</html>'
compose.py
#!/usr/bin/python
#-*-coding:utf-8-*-
path_settings='/var/www/html/mail/settings.txt'
import cgitb
cgitb.enable()
import cgi, os, sys
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import mail_f
url_compose_css, mail_form_width = '',''
if os.path.exists(path_settings):
f=open(path_settings)
s=f.read()
f.close()
L=s.split('\n')
for item in L:
if '=' in item:
l=item.split('=')
var=l[0].strip()
val=l[1].strip()
if var=='url_compose_css':
url_compose_css = val
elif var=='mail_form_width':
mail_form_width = int(val)
else:
print 'Content-type: text/html\n\n'
print '<!doctype html>'
print 'Not found: settings.txt'
sys.exit()
################################
print 'Content-type: text/html\n\n'
print '<!doctype html>'
print '<html lang="en">'
print '<head>'
print '<meta charset="utf-8">'
print '<link rel="stylesheet" type="text/css" href="'+url_compose_css+'">'
print "<base target='_parent' />"
print '<title>Compose</title>'
print '</head>'
print '<body>'
T=mail_f.mail_form_fields()
print '<form style="width: '+str(mail_form_width)+'px;" '+T[6]+'>'
print 'Name:<br>'
print '<input '+T[0]+' style="width: 100%;" type="text"><br><br>'
print 'Email:<br>'
print '<input '+T[1]+' style="width: 100%;" type="text" pattern="[^@]+@[^@]+\.[^@]+"><br><br>'
print 'Subject:<br>'
print '<input '+T[2]+' style="width: 100%;" type="text"><br><br>'
print 'Message:<br>'
print '<textarea '+T[3]+' style="width: 100%; height: '+str(mail_form_width/2.0)+'px;"></textarea><br>'
print '<table width="100%">'
print '<tr valign="middle">'
print '<td align="right">'
print 'Enter code: '
print '</td>'
print '<td>'
print '<input '+T[4]+' maxlength="6" size="7" type="text" pattern="[A-Z]{6}">'
print ' </td>'
print '<td align="right">'
print ' <table>'
print ' <tr>'
print ' <td>'
print ' <iframe '+T[5]+' marginwidth="0" marginheight="0" hspace="0" vspace="0" frameborder="0" scrolling="no" height="45" width="160"></iframe>'
print ' </td>'
print ' </tr>'
print ' </table>'
print '</td>'
print '</tr>'
print '<tr>'
print '<td></td>'
print '<td></td>'
print '<td align="right"><input value="Submit" type="submit"></td>'
print '</tr>'
print '</table>'
print '</form>'
print '</body>'
print '</html>'
mail_f.py
#!/usr/bin/python
#-*-coding:utf-8-*-
""" Функции для Мэил"""
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import random, string
def mail_form_fields():
return ('name="name"', 'name="from"', 'name="subject"', 'name="message"', 'name="code"', 'src="/cgi-bin/captcha.py"', 'action="/cgi-bin/send.py" method="post"')
def Code(path_mail_dir, url_mail_dir, code_text_color, code_bg_color, code_dots_color, code_dots_n, path_font):
"""Random Code Generator"""
size=6
chars=string.ascii_uppercase
code_txt=''.join(random.choice(chars) for _ in range(size))
# Random string -----------------------
img_width=120
img_height=40
img_font_size = 20
img_padding=20
path_out=path_mail_dir+code_txt+'.png'
# Code Text -------------------------
img = Image.new("RGBA", (img_width, img_height), code_bg_color)
draw = ImageDraw.Draw(img)
i=0
while i< code_dots_n:
draw.point((random.randrange(5, img_width-5) , random.randrange(5, img_height-5) ), code_dots_color)
i=i+1
font = ImageFont.truetype(path_font, img_font_size)
w, h = draw.textsize(code_txt, font)
img_padding_hor=(img_width-w)/2.0
img_padding_vert=(img_height-h)/2.0
draw.text((img_padding_hor, img_padding_vert), code_txt, fill=code_text_color, font=font)
img.save(path_out)
return(path_out, code_txt)
send.py
#!/usr/bin/python
#-*-coding:utf-8-*-
path_settings='/var/www/html/mail/settings.txt'
import cgitb
cgitb.enable()
import cgi
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
import os, time, re, sys
sender_name, email_to , smtp_security_type, smtp_server , smtp_port, smtp_login , smtp_password, path_template, path_response, path_blocks, url_compose_css, url_open , path_tokens, N_tokens = '','','','','','','','','','','','','',''
form = cgi.FieldStorage()
Errors=[]
Name, From, Subject, Message, Code = '','','','', ''
a, b, c, d = '','','',''
http_user_agent, remote_addr, http_referer = '', '', ''
if os.path.exists(path_settings):
f=open(path_settings)
s=f.read()
f.close()
L=s.split('\n')
for item in L:
if '=' in item:
l=item.split('=')
var=l[0].strip()
val=l[1].strip()
if var=='sender_name':
sender_name = val
elif var=='email_to':
email_to = val
elif var=='smtp_security_type':
smtp_security_type = val
elif var=='smtp_server':
smtp_server = val
elif var=='smtp_port':
smtp_port = int(val)
elif var=='smtp_login':
smtp_login = val
elif var=='smtp_password':
smtp_password = val
elif var=='path_template':
path_template = val
elif var=='path_response':
path_response = val
elif var=='path_blocks':
path_blocks = val
elif var=='url_compose_css':
url_compose_css = val
elif var=='url_open':
url_open = val
elif var=='path_tokens':
path_tokens = val
elif var=='N_tokens':
N_tokens = int(val)
else:
print 'Content-type: text/html\n\n'
print '<!doctype html>'
print 'Not found: settings.txt'
sys.exit()
try:
http_user_agent=cgi.escape(os.environ["HTTP_USER_AGENT"])
except:
http_user_agent='none'
try:
remote_addr=cgi.escape(os.environ["REMOTE_ADDR"])
except:
remote_addr='none'
try:
http_referer=cgi.escape(os.environ["HTTP_REFERER"])
except:
http_referer='none'
try:
Name=form['name'].value.strip()
Name=re.sub(r'<[^>]*?>', '', Name)
a='Name: '+ Name
if len(Name)==0:
Errors.append('Name: none')
except:
Errors.append('Name: none')
try:
From = form['from'].value.strip()
From = re.sub(r'<[^>]*?>', '', From)
b='Email: '+ From
except:
Errors.append('Email: none')
try:
Subject=form['subject'].value.strip()
Subject=re.sub(r'<[^>]*?>', '', Subject)
c='Subject: '+ Subject
except:
Errors.append('Subject: none')
try:
Message = form['message'].value.strip()
#Message = re.sub(r'<[^>]*?>', '', Message)
d= Message.strip()
except:
Errors.append('Message: none')
try:
Code = form['code'].value.strip()
except:
Errors.append('Error: code mismatch')
if len(Errors) == 0:
EMAIL_REGEX = re.compile(r"[^@]+@[^@]+\.[^@]+")
if not EMAIL_REGEX.match(From):
Errors.append('Email is invalid: '+From)
if len(Errors) == 0:
if os.path.exists(path_tokens)==False:
Errors.append('Error: code mismatch')
else:
f=open(path_tokens, 'r')
s=f.read()
f.close()
L=s.split()
if Code not in L:
Errors.append('Error: code mismatch')
L.reverse()
i=0
l=[]
OUT=[]
for line in L:
if i < N_tokens:
if len(line)>0:
OUT.append(line)
i=i+1
OUT.reverse()
if len(OUT) >0:
f=open(path_tokens, 'w')
for line in OUT:
f.write(line+'\n')
f.close()
html='<h1>Thank you</h1>'+a+'<br>'+b+'<br>'+c+'<br>'*2+d.replace('\n', '<br>')
if len(Errors) == 0:
try:
msg = MIMEMultipart()
msg['From'] = sender_name+' <'+smtp_login+'>'
msg['Reply-To'] = Name+' <'+From+'>'
msg['To'] = email_to
msg['Subject'] = Subject
msg['Date'] = time.strftime("%c")
Message= \
Message+'\n\n'\
+'HTTP_USER_AGENT = '+http_user_agent+'\n'\
+'REMOTE_ADDR = '+remote_addr+'\n'\
+'HTTP_REFERER = '+http_referer+'\n\n'\
msg.attach(MIMEText(Message, 'plain'))
# Опции для SMTP сервера ---------
if smtp_security_type =='TLS':
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls()
if smtp_security_type =='SSL':
server = smtplib.SMTP_SSL(smtp_server, smtp_port)
server.ehlo()
server.login(smtp_login, smtp_password)
text = msg.as_string()
# Checking blocks.txt ----------
block='no'
if os.path.exists(path_blocks):
f=open(path_blocks, 'r')
s=f.read()
f.close()
H=[http_user_agent, remote_addr, http_referer] # Headers
L=s.split('\n')
for item in L:
h=''
v=''
if '=' in item:
l=item.split('=')
h=l[0].strip()
v=l[1].strip()
if v in H:
block='yes'
break
if block=='no':
server.sendmail(From, email_to, text)
server.close()
#----------------------------------
except Exception as e:
i=0
for item in e:
item=str(item)
if i==0:
item='Error: '+item
item=item.replace('<', '<')
item=item.replace('>', '>')
Errors.append(item)
i=1+1
if len(Errors)>0:
html='<h1>Please correct</h1>'
for item in Errors:
html=html+item+'<br>'
html=html+'<br>Email delivery failed.'
f=open(path_template, 'r')
s=f.read()
f.close()
s=s.replace('[msg]', html)
f=open(path_response, 'w')
f.write(s)
f.close()
print 'Content-type: text/html\n\n'
print '<meta http-equiv="refresh" content="0;url='+url_open+'">'
print '<link rel="canonical" href="'+url_open+'"/>'
iframe.html
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<title>Response</title>
</head>
<body>
<!-- === Копируй отсюда === -->
<iframe src="/mail/response.html" width="500" height="300" frameBorder="0"></iframe>
<!-- === и досюда ========= -->
</body>
</html>