【问题标题】:Send a mail directly without opening either mail App or mail website直接发送邮件,无需打开邮件应用程序或邮件网站
【发布时间】:2020-10-10 06:15:47
【问题描述】:

我想在没有打开网站或应用程序的情况下向我的公司地址发送邮件。另外,如果客户没有填写所有字段,我想显示一个所有字段都需要的错误,并出现一个框,告诉客户邮件已成功发送。

这是我目前的代码:

<form method="POST" action="mailto:jorge.soto.rodriguez@outlook.com" enctype="text/plain">
  <label for="fname">*¿Cuál es tu nombre?</label><br>
  <input type="text" id="fname" name="firstname">
  <br><br>
  <label for="lname">*¿Cuál es tu correo electrónico?</label><br>
  <input type="text" id="lname" name="lastname">
  <br><br>
  <label for="country">*¿Qué artista te interesa?</label><br>
  <select id="country" name="country" placeholder="Selecciona un artista...">
    <option value="faruz">FARUZ</option>
    <option value="potencia">POTENCIA</option>
    <option value="rapder">RAPDER</option>
    <option value="rc">RC</option>
    <option value="zticma">ZTICMA</option>
  </select>
  <br><br>
  <label for="subject">Detalla aquí tu propuesta:</label><br>
  <textarea id="subject" name="subject" style="height:150px"></textarea>
  <br>
  <input type="submit" value="Enviar">
</form>

【问题讨论】:

  • 没有网站/应用程序,您打算将这段代码放在哪里?
  • 单靠 HTML 和 CSS 是不行的,你需要像 PHP 这样的服务器端语言来发送电子邮件。最好安装一个框架并从那里开始(如 Laravel),否则查找 mail() 函数或使用 PhpMailer、SwiftMailer 等。
  • 我的意思是,我不希望网站每次点击“发送”时都打开邮件服务器网站或APP。我只想让用户点击它来完成表单。
  • HTML 和 CSS 不会单独完成,因为到目前为止还没有设计用于执行您真正想要的功能,唯一的建议是使用服务器端语言,如 PHP、Node .js 、Python 或任何其他你可能觉得很好用的文件
  • 好吧,action="mailto:jorge.soto.rodriguez@outlook.com" 由浏览器在本地处理。 POST 到服务器端 url 并让服务器发送电子邮件。

标签: html css forms


【解决方案1】:

如上所述,您需要一台服务器。 我有一个 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 安全性。

希望能帮到你:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-03
    • 2013-06-20
    • 1970-01-01
    • 2016-05-20
    • 2014-01-09
    • 1970-01-01
    • 1970-01-01
    • 2011-11-03
    相关资源
    最近更新 更多