[英]How do I send email with JavaScript without opening the mail client?
我正在編寫一個帶有注冊按鈕的 HTML 頁面,該頁面應該在不打開本地郵件客戶端的情況下靜默發送電子郵件。 這是我的 HTML:
<form method="post" action="">
<input type="text" id="email_address" name="name" placeholder="Enter your email address..." required>
<button onclick="sendMail(); return false">Send Email</button>
</form>
...這是我的 JavaScript 代碼:
<script type="text/javascript">
function sendMail() {
var link = 'mailto:hello@domain.com?subject=Message from '
+document.getElementById('email_address').value
+'&body='+document.getElementById('email_address').value;
window.location.href = link;
}
</script>
上面的代碼有效......但它打開了本地電子郵件客戶端。 如果我刪除onclick
屬性中的return
語句,如下所示:
<form method="post" action="">
<input type="text" id="email_address" name="name" placeholder="Enter your email address..." required>
<button onclick="sendMail()">Send Email</button>
</form>
...然后根本沒有發送電子郵件。 我錯過了什么嗎?
任何幫助將不勝感激:-)
您不能讓用戶的瀏覽器靜默發送電子郵件。 這將是一個可怕的安全問題,因為任何網站都可以將其系統用作垃圾郵件中繼和/或獲取其電子郵件地址。
您需要向從您的服務器發送郵件的服務器端進程(以您選擇的語言編寫)發出 HTTP 請求。
您需要服務器端支持來實現這一點。 基本上,您的表單應該發布(AJAX 也可以)到服務器,並且該服務器應該通過 SMTP 連接到某個郵件提供商並發送該電子郵件。
即使可以直接使用 JavaScript(即來自用戶計算機)發送電子郵件,用戶仍然必須連接到某個 SMTP 服務器(如 gmail.com),提供 SMTP 憑據等。這通常在服務器端(在您的應用程序中),它知道這些憑據。
直接來自客戶
in short:
1. register for Mandrill to get an API key
2. load jQuery
3. use $.ajax to send an email
像這樣 -
function sendMail() {
$.ajax({
type: 'POST',
url: 'https://mandrillapp.com/api/1.0/messages/send.json',
data: {
'key': 'YOUR API KEY HERE',
'message': {
'from_email': 'YOUR@EMAIL.HERE',
'to': [
{
'email': 'RECIPIENT@EMAIL.HERE',
'name': 'RECIPIENT NAME (OPTIONAL)',
'type': 'to'
}
],
'autotext': 'true',
'subject': 'YOUR SUBJECT HERE!',
'html': 'YOUR EMAIL CONTENT HERE! YOU CAN USE HTML!'
}
}
}).done(function(response) {
console.log(response); // if you're into that sorta thing
});
}
https://medium.com/design-startups/b53319616782
注意:請記住,任何人都可以看到您的 API 密鑰,因此任何惡意用戶都可能使用您的密鑰發送電子郵件,這可能會耗盡您的配額。
通過您的服務器間接- 安全
為了克服上述漏洞,您可以修改自己的服務器以在基於會話的身份驗證后發送電子郵件。
node.js - https://www.npmjs.org/package/node-mandrill
var mandrill = require('node-mandrill')('<your API Key>');
function sendEmail ( _name, _email, _subject, _message) {
mandrill('/messages/send', {
message: {
to: [{email: _email , name: _name}],
from_email: 'noreply@yourdomain.com',
subject: _subject,
text: _message
}
}, function(error, response){
if (error) console.log( error );
else console.log(response);
});
}
// define your own email api which points to your server.
app.post( '/api/sendemail/', function(req, res){
var _name = req.body.name;
var _email = req.body.email;
var _subject = req.body.subject;
var _messsage = req.body.message;
//implement your spam protection or checks.
sendEmail ( _name, _email, _subject, _message );
});
然后在客戶端使用 $.ajax 調用您的電子郵件 API。
您不能僅使用客戶端腳本來執行此操作...您可以對某些將發送電子郵件的服務器端代碼進行 AJAX 調用...
我想你可以使用smtpjs.com
需要某種類型的后端框架來發送電子郵件。 這可以通過 PHP/ASP.NET 或本地郵件客戶端來完成。 如果您不希望用戶看到任何內容,最好的方法是通過 AJAX 調用對單獨的 send_email 文件進行訪問。
您需要直接在服務器上執行此操作。 但更好的方法是使用 PHP。 聽說PHP有一個特殊的代碼,可以不用打開郵件客戶端直接發送郵件。
嗯,PHP 可以輕松做到這一點。
它可以通過 PHP mail()
函數來完成。 下面是一個簡單的函數的樣子:
<?php
$to_email = 'name@company.com';
$subject = 'Testing PHP Mail';
$message = 'This mail is sent using the PHP mail function';
$headers = 'From: noreply@company.com';
mail($to_email,$subject,$message,$headers);
?>
這將向$to_email
指定的收件人發送后台電子郵件。
為簡單起見,上面的示例在源代碼中使用硬編碼值作為電子郵件地址和其他詳細信息。
假設您必須創建一個與我們聯系的表單,供用戶填寫詳細信息然后提交。
讓我們創建一個使用filter_var()
內置函數驗證和清理電子郵件地址的自定義函數。
這是一個示例代碼:
<?php
function sanitize_my_email($field) {
$field = filter_var($field, FILTER_SANITIZE_EMAIL);
if (filter_var($field, FILTER_VALIDATE_EMAIL)) {
return true;
} else {
return false;
}
}
$to_email = 'name@company.com';
$subject = 'Testing PHP Mail';
$message = 'This mail is sent using the PHP mail ';
$headers = 'From: noreply@company.com';
//check if the email address is invalid $secure_check
$secure_check = sanitize_my_email($to_email);
if ($secure_check == false) {
echo "Invalid input";
} else { //send email
mail($to_email, $subject, $message, $headers);
echo "This email is sent using PHP Mail";
}
?>
我們現在將其設為一個單獨的 PHP 文件,例如sendmail.php
。
然后,將在表單提交時使用此文件,使用表單的action
屬性,例如:
<form action="sendmail.php" method="post">
<input type="text" value="Your Name: ">
<input type="password" value="Set Up A Passworrd">
<input type="submit" value="Signup">
<input type="reset" value="Reset Form">
</form>
希望我能幫上忙
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.