如上所述,您需要一台服务器。
我有一个 python 服务器和一个发送电子邮件的小客户端。你需要一点网络背景才能理解这一点,如果你还没有读过一点关于 HTTP、SMTP、AJAX 等的知识。
服务器:
import socket
import select
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
HTTP_FORMATS = ["GET", "POST"]
def valid_http(data):
"""
:param data:
:return: if the http is valid and relevant to us.
"""
temp = data.split(" ")
if temp[0] in HTTP_FORMATS and "HTTP/1.1" in temp[2]:
return True
return False
def focus(data):
"""
:param data:
:return: the http format
"""
temp = data.split(" ")
if temp[0] == "GET":
return temp[1]
elif temp[0] == "POST":
data = data.split("\r\n\r\n")
return data[1]
def clean(data):
"""
:param data:
:return: "cleaned" data- now python can understand the data
"""
data = data.replace("%22", '"', 100) # we want " instead %22
data = data.replace("%20", ' ', 100) # we want space instead %20
data = data.replace("%3Cbr/%3E","\n",100)
data = data.replace("<br/>", "\n", 100)
return data
def send_email(message, email, password,send_to_email, subject):
try:
msg = MIMEMultipart()
msg['From'] = email
msg['To'] = send_to_email
msg['Subject'] = subject
msg.attach(MIMEText(message, 'plain'))
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(email, password)
text = msg.as_string()
server.sendmail(email, send_to_email, text)
server.quit()
print "sent email"
except:
print "probably not valid email address or not blocked email"
# that was part of a big program so thats why its in try thing.
open_client_sockets = []
server_socket = socket.socket()
server_socket.bind(('0.0.0.0', 80))
server_socket.listen(100)
email = "" # email to send from
password = '' # password to the email above
reciever_mail = ""
subject = ''
content = ""
email_const_from_client = "arnav123"
while True:
rlist, wlist, xlist = select.select(open_client_sockets+[server_socket], open_client_sockets, [])
for current_socket in rlist:
if current_socket is server_socket:
(new_socket1, address1) = server_socket.accept()
open_client_sockets.append(new_socket1)
else:
data = current_socket.recv(4096)
if data != "":
print "here"
original_data = data
if valid_http(data):
data = focus(data)
if data == "/":
f = open("website_file.html", "r")
txt = f.read()
current_socket.send(txt)
f.close()
if data == "/favicon.ico":
try:
with open("favicon.png", "rb") as image_file:
a=image_file.read()
current_socket.send(a)
image_file.close()
except:
print "no favicon image in this server"
else:
data = clean(data)
print data
if email_const_from_client in data: # the user wnts to send an email
data = data.split(email_const_from_client) # [0] is mail. [1] is content
subject = data[0]
content = data[1]
reciever_mail = data[2]
print subject
print content
print reciever_mail
send_email(content, email, password, reciever_mail, subject)
open_client_sockets.remove(current_socket)
current_socket.close()
对于客户端:
<html>
<script>
url="127.0.0.1";
function sending (var_to_send, callback){
/*
this is the function that every other sending/receiving function rely on
it creates a XMLHttpRequest- sends it with the relevent data and receives data from the server
*/
var request = new XMLHttpRequest();
request.open('POST', url, true);
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
request.setRequestHeader('Accept', 'application/json');
request.onreadystatechange = function () {
if (request.readyState == 4 && this.status == 200) {
var serverResponse = this.responseText;
serverResponse=serverResponse.replace('HTTP/1.1 200 Ok\r\n\r\n','');
callback(serverResponse);
}
};
request.send(var_to_send);
}
function send() {
var subject = document.getElementById("subject").value;
var message = document.getElementById("message").value;
var email = document.getElementById("email_to_send_to").value;
var separation = "arnav123"; // for the server string seperation and email request acknowledge.
subject = subject.concat(separation);
var to_send = subject.concat(message);
to_send=to_send.concat(separation);
to_send=to_send.concat(email)
var c = function(response){
document.getElementById('server_respone').value = response;
};
var v = sending(to_send,c);
}
</script>
<body>
<textarea rows="5" cols="60" id="subject" placeholder="email subject"></textarea>
<textarea rows="5" cols="60" id="message" placeholder="email content"></textarea>
<textarea rows="5" cols="60" id="email_to_send_to" placeholder="email to send to"> </textarea>
<button type="submit" onclick="send()">send to server</button>
<br />
<textarea rows="3" cols="30" id="server_respone" placeholder="server response"></textarea>
</body>
</html>
当您尝试此操作时,您会在您的 gmail 中收到安全警告。要取消此操作,您需要在此处确认:allowing less secured apps in your google acount.
我建议您仅为该项目创建另一个 gmail 帐户,因为它会降低您的 gmail 安全性。
希望能帮到你:)