Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
須在php.ini文件中正確配置PHP ,并詳細說明系統如何發送電子郵件。打開/ etc /目錄中的php.ini文件,并找到標題為[郵件功能]的部分。
Windows用戶應確保提供了兩個指令。第一個稱為SMTP,它定義您的電子郵件服務器地址。第二個稱為sendmail_from,它定義您自己的電子郵件地址。
Windows的配置應如下所示:
[mail function]
; For Win32 only.
SMTP=smtp.secureserver.net
; For win32 only
sendmail_from=webmaster@tutorialspoint.com
Linux用戶只需要讓PHP知道他們sendmail應用程序的位置即可。路徑和任何所需的開關應指定給sendmail_path指令。
Linux的配置應該看起來像這樣-
[mail function]
; For Win32 only.
SMTP=; For win32 only
sendmail_from=; For Unix only
sendmail_path=/usr/sbin/sendmail -t -i
現在您可以開始了-
PHP利用mail()函數發送電子郵件。此功能需要三個必填參數,用于指定收件人的電子郵件地址,郵件主題和實際郵件,此外還有其他兩個可選參數。
mail( to, subject, message, headers, parameters );
這是每個參數的說明。
一旦調用了郵件功能,PHP將嘗試發送電子郵件,如果成功,它將返回true;如果失敗,則將返回false。
可以將多個收件人指定為逗號分隔列表中mail()函數的第一個參數。
當您使用PHP發送文本消息時,所有內容將被視為簡單文本。即使您將HTML標簽包含在文本消息中,它也將顯示為簡單文本,并且不會根據HTML語法設置HTML標簽的格式。但是PHP提供了將HTML消息作為實際HTML消息發送的選項。
發送電子郵件時,您可以指定Mime版本,內容類型和字符集來發送HTML電子郵件。
以下示例將HTML電子郵件發送到xyz@somedomain.com,并將其復制到afgh@somedomain.com。您可以使用以下方式對程序進行編碼:它應接收用戶的所有內容,然后發送電子郵件。
<html>
<head>
<title>Sending HTML email using PHP</title>
</head>
<body>
<?php
$to="xyz@somedomain.com";
$subject="This is subject";
$message="<b>This is HTML message.</b>";
$message .="<h1>This is headline.</h1>";
$header="From:abc@somedomain.com \r\n";
$header .="Cc:afgh@somedomain.com \r\n";
$header .="MIME-Version: 1.0\r\n";
$header .="Content-type: text/html\r\n";
$retval=mail ($to,$subject,$message,$header);
if( $retval==true ) {
echo "Message sent successfully...";
}else {
echo "Message could not be sent...";
}
?>
</body>
</html>
要發送包含混合內容的電子郵件,需要將Content-type標頭設置為multipart / mixed。然后可以在邊界內指定文本和附件節。
邊界以兩個連字符開頭,后跟一個唯一的數字,該數字不能出現在電子郵件的消息部分。PHP函數md5()用于創建32位十六進制數字以創建唯一數字。表示電子郵件最后部分的最后邊界也必須以兩個連字符結尾。
者 | Mateusz Iwaniuk
譯者 | 明明如月,責編 | 夕顏
出品 | CSDN(ID:CSDNnews)
文章配套代碼: https://github.com/iwaniukooo11/email-sender
現在,即使是創建最基本的網站,程序員也必須使用現代的功能和技術。甚至像為你的朋友創建簡單的投資組合這樣的基本項目也可能涉及到一些問題,比如從聯系人表單接收數據。有很多方法可以讀取這些數據。你可以將表單與數據庫連接起來,然后從數據庫中讀取傳入的消息來實現功能,但這樣做會給不懂技術的客戶造成困難。
你為什么不通過發送電子郵件傳輸信息?
不使用數據庫就能接收到傳入的消息,絕對是最佳選擇,也是最方便用戶的選擇。但問題來了—如何實現呢?你可能認為需要使用某種后端語言。
實際上,你不必使用任何如 php 或 python 這種后端語言,你甚至不需要用到 node.js!你需要的就是一個簡單的EmailJS 庫。
本文將介紹下面兩個重要功能:
配置 emailjs 帳戶
使用 JS 發送電子郵件
請注意,在我的項目中,我使用了 gulp 和 webpack,我在 src 文件夾存放源碼,dist 存放最終發布版本的代碼。
我將分 5 個步驟向你展示如何從頭開始構建電子郵件發送器。
步驟1-用 HTML 創建表單
首先需要創建一個 HTML 表單。你不必放置像 required 或 max 這種驗證屬性,因為稍后,preventDefault 函數將在你的提交事件上運行,它會讓這些屬性的處理失效。
表單中最重要的是為每個輸入放置 name 屬性,后面會用到。
我的非常簡單的表單是這樣的:
src/html/index.html
<form class="form">
<input name='name' type="text" placeholder="Your name..." class="form__input" />
<input name='topic' type="text" placeholder="Topic..." class="form__input" />
<textarea name='message' type="text" placeholder="Your Message..." class="form__input" ></textarea>
<input type="submit" value="send" class="form__input form__input--button">
</form>
步驟2-注冊成為 email 用戶
要配置你的電子郵件,你必須注冊電子郵件服務。別擔心—使用這個網站非常方便和省時。
登入后,系統會詢問你的電子郵件服務,它位于個人電子郵件服務區(personal email service)。在我的例子中,我選擇了 gmail。
然后,你需要連接你的 gmail 帳戶。這將用來發送電子郵件給你客戶。例如,如果你關聯了 xyz@gmail.com 賬戶,你后續發送的郵件都將從這個郵箱發出。所以不要擔心“ Send email on your behalf” 這個授權信息—這正是你需要的!
連接完 gmail 賬戶后,點擊添加服務(add service)按鈕。
步驟3-創建郵件模板
如果你已經成功連接了你的 gmail 賬戶,你現在應該在信息中心中。現在需要創建電子郵件模板了。
切換到電子郵件模板卡,并單擊創建一個新的模板(create a new template)。界面非常友好,所以創建模板不會有任何問題。
你可以選擇模板的名稱和 ID。我稱之為“我的神奇模板(my_amazing_template)”。
接下來,你必須指定郵件的內容。
模板的變量值來自 input 中的 `name` 屬性。你已將變量插入`{{{}}}`符號中。
不要忘記在“收件人”部分 (右側) 添加電子郵件地址。你的電子郵件將被發送到該電子郵件地址上。截圖中的收件人郵箱是我自己的公司郵箱。
這是我的簡單模板,它使用來自 HTML 表單里的 3 個變量。我還指定了接收電子郵件的主題。
步驟4-保存 API 密鑰
這部分沒什么特別的。Emailjs 共享授權 API 密鑰,將在發送電子郵件時使用。當然,放這些鑰匙最好的地方是`.env` 配置。但是因為我使用的是簡單的靜態文件,我不想使用服務器配置,所以我將它們保存在 apikeys 文件中,然后再將它們導入。
你的 USER_ID 位于 Account > API Keys 菜單下。
TEMPLATE_ID 位于模板的標題下面。
這是我基于不存在的 keyssrc / js / apikeys. js 的示例配置.
src/js/apikeys.js
export default {
USER_ID :'user_DPUd-rest-of-my-id',
TEMPLATE_ID:'my_amazing_template'
}
如果需要將源碼發布到 GITHUB,不要忘記將 APIKEYS 文件添加到 .GITIGNORE文件中
現在是該項目最后也是最重要的部分的了。現在我們必須使用 javascript 發送電子郵件。
首先,你必須下載 emailjs 包。
npm i emails-com
然后,轉到 js 文件,導入庫和 apikeys。
src/js/main.js
import emailjs from 'emailjs-com'
import apiKeys from './apikeys'
現在是編寫發送電子郵件功能的時候了
src/js/main.js
const sendEmail=e=> {
e.preventDefault
emailjs
.sendForm('gmail', apiKeys.TEMPLATE_ID, e.target, apiKeys.USER_ID)
.then(
result=> {
console.log(result.text)
},
error=> {
console.log(error.text)
}
)
}
sendForm 函數有4個參數:
你的電子郵件的 ID,在這里:
TEMPLATE_ID 來自 apikey 文件,
事件對象來自你的表單提交
USER_ID 來自 apikey 文件,
最后,查找表單并添加提交事件監聽器:
src/js/main.js
const form=document.querySelector('.form')
form.addEventListener('submit',sendEmail)
正如我前面提到的,由于 `preventDefault` 函數,屬性驗證將無法工作。你必須使用 JS 自己進行驗證和清除輸入。
以上就是全部內容,接下來讓我們測試一下。
填寫頁面上的表單并發送。
我收到電子郵件,內容正是根據我們的模板和表單數據渲染出來的。
通過上圖可以看出,所有的變量的值都填充到了正確的位置上。
通過本文的介紹你會發現用 JS 發送郵件并非難事。
使用 emailjs,你可以簡單的方式發送電子郵件。
我相信你未來的用戶會很高興收到來自他們網頁上表單填寫數據的t郵件,相信本文對你有幫助。
這篇文章的配套代碼在這里: https://github.com/iwaniukooo11/email-sender
原文鏈接:
https://dev.to/iwaniukooo11/send-e-mails-directly-from-front-end-with-js-5d7d
本文為CSDN翻譯文章,轉載請注明出處。
?我們想研發一個機器學習框架,6 個月后失敗了
?生產型機器學習已經沒那么困難了?
?視頻 | 你不知道的"開源"60年秘史
?GitHub標星10,000+,Apache項目ShardingSphere的開源之路
?阿里技術專家告訴你,如何畫出優秀的架構圖?
?加拿大API平臺如何做到30%為中國明星項目?創業老兵這樣說……
wish I could remember the very first email message I ever sent. Unfortunately, the truth is that I’ve come to take email for granted; for instance, I easily send more email messages than I make phone calls. Because the novelty has long since worn off, the memory of that first email is as lost to me as my first phone call; but I doubt my sense of wonder was any less complete at the time. To someone who has never seen email in action before, that first message can be magical.
我希望我能記得我發送的第一封電子郵件。不幸的是,事實是我已經把電子郵件視為理所當然;例如,我很容易發送比打電話更多的電子郵件。因為新奇感早已消失,我對第一封電子郵件的記憶就像第一次打電話一樣消失了;但我懷疑當時我的驚奇感是否不夠完整。對于一個以前從未見過電子郵件的人來說,第一條消息可能很神奇。
In this article, I’ll try to recapture some of that magic for those of you who have never created a Web site that sends email messages. We’ll see how the PHP server-side scripting language may be set up to send email, and explore how to send complex message types such as HTML email or emails with file attachments.
在這篇文章中,我將嘗試為那些從未創建過發送電子郵件的網站的人重溫其中的一些魔力。我們將了解如何設置PHP服務器端腳本語言來發送電子郵件,并探索如何發送復雜的消息類型,如HTML電子郵件或帶有文件附件的電子郵件。
PHP Email Setup
PHP電子郵件步驟
Before we can send email with PHP, we need to set it up to do so, just as you need to set up your email program before it can send messages. Configuration for sending email in PHP is done with the php.ini file, so open up your Web server’s php.ini in whichever editor you normally use.
在我們用PHP發送電子郵件之前,我們需要對它進行設置,就像您需要在它發送消息之前設置電子郵件程序一樣。用PHP發送電子郵件的配置是通過PHP.ini文件完成的,所以在您通常使用的編輯器中打開Web服務器的PHP.ini。
If you don’t run your own server, but instead have a PHP-equipped Web host, you can safely assume that everything in this section has been done for you, and skip ahead.
如果您沒有運行自己的服務器,而是有一個配備了PHP的Web主機,那么您可以放心地假設本節中的所有內容都已為您完成,然后跳過。
In the section entitled [mail function] in the php.ini file, you’ll find three settings: SMTP, sendmail_from, and sendmail_path. If your server runs on a Windows machine, you’ll want to set the SMTP option to point to your SMTP server (or your ISP’s SMTP server, if you’re setting up PHP on your home machine). If instead you’re setting up PHP on a Linux (or other Unix-based OS) server, you’ll want to set the sendmail_path option to point to the sendmail program on your server, passing it the -t option. You can use the SMTP option in Linux instead if you don’t have sendmail set up.
在php.ini文件中標題為[mail function]的部分中,您將找到三個設置:SMTP、sendmail_from和sendmail_path。如果您的服務器在Windows計算機上運行,則需要將SMTP選項設置為指向您的SMTP服務器(或ISP的SMTP服務器,如果您在家庭計算機上設置PHP)。如果要在Linux(或其他基于Unix的操作系統)服務器上設置PHP,則需要將sendmail_path選項設置為指向服務器上的sendmail程序,并將-t選項傳遞給它。如果沒有設置sendmail,則可以在Linux中使用SMTP選項。
In either case, you’ll want to set the sendmail_from option to your email address, or whichever address you’d like to appear as the default ‘from’ address for emails sent from PHP scripts.
在任何一種情況下,您都需要將sendmail_from選項設置為您的電子郵件地址,或者您希望顯示為從PHP腳本發送的電子郵件的默認“from”地址的任何地址。
Here’s how the section might look on a typical Windows server, or on a Linux server without sendmail:
以下是該部分在典型Windows服務器上的外觀,或在沒有sendmail的Linux服務器上的展示:
[mail function]
; Setup for Windows systems
SMTP=smtp.my.isp.net
sendmail_from=me@myserver.com
And here’s how it might look on a Linux server with sendmail:
[mail function]
; Setup for Linux systems
sendmail_path=/usr/sbin/sendmail -t
sendmail_from=me@myserver.com
With those settings in place, restart your Web server and you’re ready to go!
Sending Plain Email
發送普通電子郵件
It doesn’t come much easier than the procedure to send plain text email in PHP. In fact, you can do it in just one line in a PHP script:
用PHP發送純文本電子郵件的過程并不容易。事實上,您可以在PHP腳本的一行中完成:
<?php
mail('recipient@some.net', 'Subject', 'Your message here.');
?>
The above line would send an email message to recipient@some.net with ‘Subject‘ as the subject line and ‘Your message here.‘ as the message body. As you can see, PHP’s mail function makes sending email exceedingly simple, but there are a few advanced tricks we can use to get more out of this simple function.
上述行將向發送電子郵件recipient@some.net以“Subject”作為標題,以“Your message here.”作為郵件正文。正如您所看到的,PHP的郵件函數使發送電子郵件變得非常簡單,但是我們可以使用一些高級技巧來從這個簡單的函數中獲得更多信息。
First of all, if the mail system you configured in your php.ini file rejects a message you try to send (for example, if the ‘to’ address is not a valid email address), this function will display an error message in the user’s browser. Like most PHP functions, however, error messages may be suppressed by preceding the function name with an @ symbol. Combine this with the fact that the mail function returns either true or false depending on whether the email was accepted by the mail sending system, and you have a formula to send email with appropriate error checking:
首先,如果您在php.ini文件中配置的郵件系統拒絕您嘗試發送的郵件(例如,如果“to”地址不是有效的電子郵件地址),此函數將在用戶瀏覽器中顯示錯誤消息。但是,與大多數PHP函數一樣,可以通過在函數名前面加上@符號來抑制錯誤消息。再加上郵件函數根據郵件發送系統是否接受郵件而返回true或false,并且您有一個發送電子郵件并進行適當錯誤檢查的公式:
<?php
if (@mail($to, $subject, $message)) {
echo('<p>Mail sent successfully.</p>');
} else {
echo('<p>Mail could not be sent.</p>');
}
?>
Note that just because an email message could be sent doesn’t guarantee it will arrive at its destination. An email address can be valid (i.e. correctly formed) but not actually exist. For instance, you can successfully send a message to nonexistant.user@hotmail.com — that is, the mail function will return true — but the message will bounce because no such user exists. PHP provides no built-in means of detecting when this happens.
請注意,僅僅因為可以發送電子郵件并不保證它會到達目的地。電子郵件地址可能有效(即格式正確),但實際上不存在。例如,您可以成功地向不存在的用戶發送消息。user@hotmail.com-也就是說,郵件函數將返回true-但郵件將反彈,因為不存在這樣的用戶。PHP沒有提供檢測何時發生這種情況的內置方法。
When you want to send a message to multiple recipients, you can just list their email addresses one after another, separated by commas, in the first parameter. For example:
當您想向多個收件人發送郵件時,您可以在第一個參數中逐個列出他們的電子郵件地址,并用逗號分隔。例如:
*請認真填寫需求信息,我們會在24小時內與您取得聯系。