linux郵件發送
A. 如何在linux命令行中通過SMTP伺服器發送電子郵件
假定你想配置一個 Linux 應用,用於從你的伺服器或桌面客戶端發送郵件信息。郵件信息可能是郵件簡報、狀態更新(如 Cachet)、監控警報(如 Monit)、磁碟時間(如 RAID mdadm)等等。當你要建立自己的 郵件發送伺服器 傳遞信息時 ,你可以替代使用一個免費的公共 SMTP 伺服器,從而避免遭受維護之苦。
谷歌的 Gmail 服務就是最可靠的 免費 SMTP 伺服器 之一。想要從應用中發送郵件通知,你僅需在應用中添加 Gmail 的 SMTP 伺服器地址和你的身份憑證即可。
使用 Gmail 的 SMTP
伺服器會遇到一些限制,這些限制主要用於阻止那些經常濫用伺服器來發送垃圾郵件和使用郵件營銷的傢伙。舉個例子,你一次只能給至多 100
個地址發送信息,並且一天不能超過 500
個收件人。同樣,如果你不想被標為垃圾郵件發送者,你就不能發送過多的不可投遞的郵件。當你達到任何一個限制,你的 Gmail
賬戶將被暫時的鎖定一天。簡而言之,Gmail 的 SMTP 伺服器對於你個人的使用是非常棒的,但不適合商業的批量郵件。
說了這么多,是時候向你們展示 如何在 Linux 環境下使用 Gmail 的 SMTP 伺服器 了。
Google Gmail SMTP 伺服器設置
如果你想要通過你的應用使用 Gmail 的 SMTP 伺服器發送郵件,請牢記接下來的詳細說明。
郵件發送伺服器 (SMTP 伺服器): smtp.gmail.com
使用認證: 是
使用安全連接: 是
用戶名: 你的 Gmail 賬戶 ID (比如 "alice" ,如果你的郵箱為 [email protected])
密碼: 你的 Gmail 密碼
埠: 587
確切的配置根據應用會有所不同。在本教程的剩餘部分,我將向你展示一些在 Linux 上使用 Gmail SMTP 伺服器的應用示例。
從命令行發送郵件
作為第一個例子,讓我們嘗試最基本的郵件功能:使用 Gmail SMTP 伺服器從命令行發送一封郵件。為此,我將使用一個稱為 mutt 的命令行郵件客戶端。
先安裝 mutt:
對於 Debian-based 系統:
$ sudo apt-get install mutt
對於 Red Hat based 系統:
$ sudo yum install mutt
創建一個 mutt 配置文件(~/.muttrc),並和下面一樣,在文件中指定 Gmail SMTP 伺服器信息。將 替換成自己的 Gmail ID。注意該配置只是為了發送郵件而已(而非接收郵件)。
$ vi ~/.muttrc
set from = "@gmail.com"set realname = "Dan Nanni"set smtp_url = "smtp://@smtp.gmail.com:587/"set smtp_pass = ""
一切就緒,使用 mutt 發送一封郵件:
$ echo "This is an email body." | mutt -s "This is an email subject" [email protected]
想在一封郵件中添加附件,使用 "-a" 選項
$ echo "This is an email body." | mutt -s "This is an email subject" [email protected] -a ~/test_attachment.jpg
使用 Gmail SMTP 伺服器意味著郵件將顯示是從你 Gmail 賬戶發出的。換句話說,收件人將視你的 Gmail 地址為發件人地址。如果你想要使用自己的域名作為郵件發送方,你需要使用 Gmail SMTP 轉發服務。
當伺服器重啟時發送郵件通知
如果你在 虛擬專用伺服器(VPS)
上跑了些重要的網站,建議監控 VPS 的重啟行為。作為一個更為實用的例子,讓我們研究如何在你的 VPS
上為每一次重啟事件建立郵件通知。這里假設你的 VPS 上使用的是 systemd,並向你展示如何為自動郵件通知創建一個自定義的 systemd
啟動服務。
首先創建下面的腳本 reboot_notify.sh,用於負責郵件通知。
$ sudo vi /usr/local/bin/reboot_notify.sh
#!/bin/sh
echo "`hostname` was rebooted on `date`" | mutt -F /etc/muttrc -s "Notification on `hostname`" [email protected]
$ sudo chmod +x /usr/local/bin/reboot_notify.sh
在這個腳本中,我使用 "-F" 選項,用於指定系統級的 mutt 配置文件位置。因此不要忘了創建 /etc/muttrc 文件,並如前面描述的那樣填入 Gmail SMTP 信息。
現在讓我們創建如下一個自定義的 systemd 服務。
$ sudo mkdir -p /usr/local/lib/systemd/system$ sudo vi /usr/local/lib/systemd/system/reboot-task.service
[Unit]
Description=Send a notification email when the server gets rebooted
DefaultDependencies=no
Before=reboot.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/reboot_notify.sh
[Install]
WantedBy=reboot.target
在創建服務後,添加並啟動該服務。
$ sudo systemctl enable reboot-task$ sudo systemctl start reboot-task
從現在起,在每次 VPS 重啟時,你將會收到一封通知郵件。
通過伺服器使用監控發送郵件通知
作為最後一個例子,讓我展示一個現實生活中的應用程序,Monit,這是一款極其有用的伺服器監控應用程序。它帶有全面的 VPS 監控能力(比如 CPU、內存、進程、文件系統)和郵件通知功能。
如果你想要接收 VPS 上由 Monit 產生的任何事件的郵件通知,你可以在 Monit 配置文件中添加以下 SMTP 信息。
set mailserver smtp.gmail.com port 587
username "" password ""
using tlsv12
set mail-format {
from: @gmail.com
subject: $SERVICE $EVENT at $DATE on $HOST
message: Monit $ACTION $SERVICE $EVENT at $DATE on $HOST : $DESCRIPTION.
Yours sincerely,
Monit
}
# the person who will receive notification emails
set alert [email protected]
這是一個因為 CPU 負載超載而由 Monit 發送的郵件通知的例子。
B. linux下如何發送mail,難道一定要開smtp服務嗎
是的
Linux下的SMTP服務的配置,比較復雜一些,且各發行版本有些差異,以下是一個示例,供參考(建議使用第三個方法)
linux下smtp配置方法有幾種,具體如下:
方法一,使用mail函數發送郵件;使用時時需要在本地系統上正確設置SMTP,否則將不能發送郵件。由於對系統的依賴性比較大,很多時候很不穩定,在一些提供虛擬主機服務的代理商中使用mail函數發送郵件往往很不好用,所以不推薦使用這種方法。
方法二,使用管道的形式發送郵件,主要是使用php中的popen函數。使用管道的方法發送郵件屬於比較底層的操作,它取決於用戶調用程序的穩定性。所以相比mail函數,這是一種可選的發送郵件的方式,但是這些本地的郵件系統都太復雜了,用戶可能不會配置。
方法三(推薦),使用phpmailer。phpmailer類是一個開源的發送郵件類,可以從http://phpmailer.sourceforge.net官網下載,它含兩個文件class.smtp.php和class.phpmailer.php。代碼如下
include_once("class.phpmailer.php");
/**
* 定義郵件模塊配製信息
*/
define("SMTP_HOST","smtp.mail.yahoo.com"); // SMTP 主機
define("SMTP_MAIL"," [email protected]"); // SMTP 用戶email
define("SMTP_PASS"," XXXX"); // SMTP 用的密碼
define("SERVICE_MAIL"," [email protected]"); // SMTP 用戶email
define("SERVICE_NAME","PHPBOOK郵件測試"); // SMTP 用的名字
/**
* 使用phpmailer發郵件模塊
*
* @param string $email
* @param string $user
* @param string $subject
* @param string $body
* @return bool
*/
function sendMail($email,$user,$subject,$body)
{
$mail = new PHPMailer();
//$this;
$mail->IsSMTP(); // 設置使用SMTP
$mail->Host = SMTP_HOST; // 設置SMTP伺服器地址
$mail->SMTPAuth = true; // 打開SMTP許可權驗證
$mail->Username = SMTP_MAIL; // SMTP 用戶名
$mail->Password = SMTP_PASS; // SMTP 伺服器密碼
$mail->From = SERVICE_MAIL; // 設置發送者地址
$mail->FromName = SERVICE_NAME; // 設置發送者名字
$mail->AddAddress($email, $user); // 添加接收者地址
$mail->AddReplyTo(SERVICE_MAIL, SERVICE_NAME); // 設置回復地址
$mail->WordWrap = 50; // 設置顯示格式
$mail->IsHTML(true); // 設置郵件支持html
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AltBody = ""; // 文本類型的郵件
if(!$mail->Send())
{
return $mail->ErrorInfo;
}
return true;
}
//開始發送測試郵件ng: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /var/www/xiehui/admin/mail/class.smtp.php on line 89
$tomail = " [email protected]";
$user = " XXXXlinux";
$_mailSubject = "郵件測試示例!"; // 發給用戶的郵件標題小組
$_mailBody = "新浪網"; // 郵件內容小組
sendMail($tomail,$user,$_mailSubject,$_mailBody);
?>
實驗證明yahoo的smtp很好用,號稱sina的其實並不好用,我卡在著好長時間。
方法四,給予socket編寫的程序,源代碼如下
使用socket發送郵件的封裝類:
class sendmail{
var $lastmessage; //記錄最後返回的響應信息
var $lastact; //最後的動作,字元串形式
var $welcome; //用在HELO後面,歡迎用戶
var $debug; //是否顯示調試信息
var $smtp; //smtp伺服器
var $port; //smtp埠號
var $fp; //socket句柄
//發送郵件函數
function send_mail($smtp, $welcome="", $debug=false) {
if(empty($smtp)) die("SMTP不能為空!");
$this->smtp=$smtp;
if(empty($welcome)) {
$this->welcome=gethostbyaddr("localhost");
}else
$this->welcome=$welcome;
$this->debug=$debug;
$this->lastmessage="";
$this->lastact="";
$this->port="25";
}
//顯示調試信息
function show_debug($message, $inout) {
if ($this->debug) {
if($inout=="in"){ //響應信息
$m='<< ';
}else
$m='>> ';
if(!ereg("\n$", $message))
$message .= "
";
$message=nl2br($message);
echo "${m}${message}";
}
}
//執行傳遞的命令
function do_command($command, $code) {
$this->lastact=$command;
$this->show_debug($this->lastact, "out");
fputs ( $this->fp, $this->lastact );
$this->lastmessage = fgets ( $this->fp, 512 );
$this->show_debug($this->lastmessage, "in");
if(!ereg("^$code", $this->lastmessage))
return false;
else
return true;
}
//郵件發送處理
function send( $to,$from,$subject,$message) {
//連接伺服器
$this->lastact="connect";
$this->show_debug("連接到SMTP 伺服器: ".$this->smtp, "out");
$this->fp = fsockopen ( $this->smtp, $this->port );
if ( $this->fp ) {
$this->set_socket_blocking( $this->fp, true );
$this->lastmessage=fgets($this->fp,512);
$this->show_debug($this->lastmessage, "in");
if (! ereg ( "^220", $this->lastmessage ) ) {
return false;
}else{
$this->lastact="HELO " . $this->welcome . "\n";
if(!$this->do_command($this->lastact, "250")){
fclose($this->fp);
return false;
}
$this->lastact="MAIL FROM: $from" . "\n";
if(!$this->do_command($this->lastact, "250")){
fclose($this->fp);
return false;
}
$this->lastact="RCPT TO: $to" . "\n";
if(!$this->do_command($this->lastact, "250")){
fclose($this->fp);
return false;
}
//開始發送郵件正文
$this->lastact="DATA\n";
if(!$this->do_command($this->lastact, "354")){
fclose($this->fp);
return false;
}
//開始處理郵件主題頭
$head="Subject: $subject\n";
if(!empty($subject) && !ereg($head, $message)){
$message = $head.$message;
}
//開始處理郵件From頭
$head="From: $from\n";
if(!empty($from) && !ereg($head, $message)) {
$message = $head.$message;
}
//開始處理郵件To頭
$head="To: $to\n";
if(!empty($to) && !ereg($head, $message)) {
$message = $head.$message;
}
//處理結束串
if(!ereg("\n\.\n", $message))
$message .= "\n.\n";
$this->show_debug($message, "out");
fputs($this->fp, $message);
$this->lastact="QUIT\n";
if(!$this->do_command($this->lastact, "250")){
fclose($this->fp);
return false;
}
}
return true;
}else{
$this->show_debug("連接失敗!!", "in");
return false;
}
}
}
?>
使用socket發送郵件示例:
include ("./sendmail.class.php");
$mail = new sendmail();
$email = "您好,這是一個測試郵件!";
$sendmail = new send_mail("smtp.mail.126.com","PHPBOOK",true); //顯示調示信息
if($mail->send("[email protected]", "[email protected]", "測試SOCKET郵件", $email)) {
echo "發送成功!
";
}else{
echo "發送失敗!
";
}
?>
C. linux 怎麼發送郵件
可以從網頁上進行發送郵件,或者用郵件客戶端都可以,一般linux系統都自帶郵件客戶端,如Evolution等。
D. linux下發送郵件過程
發郵件:
mail -s 「標題」 對方@地址.com
然後輸入內容
完成時 輸入 . 然後回車
E. Linux sendmail到底怎麼往外發郵件
我用的是
sendemail
還是比較簡單的
部分參數如下:
-f 表示from,發件人地址
-t 表示to,收件人地址
-s mail伺服器域名
-u 主題
-xu 用戶名(@之前的)
-xp 用戶密碼
-m 純文本信息
-o message-file=/root/.. 發送文件中的內容
-a 發送附件 (-m,-o,-a可以同時使用)
需要注意的是填寫伺服器地址時最好添加上埠號
qq郵箱的埠號就不是默認的埠號
sudo apt-get install sendemail
建議在安裝前先安裝另外兩個包:
libio-socket-ssl-perl libnet-ssleay-perl
F. Linux認證系統管理:linuxmail命令發送郵件失敗
Linux認證系統管理:linuxmail命令發送郵件失敗
使用linux自帶的郵件功能測試郵件發送功能如下:
$ mail -s test [email protected]
sldkfjlskdjf[CTRL+D]
cc:
有時會出再提示:
You have a new mail in /var/spool/mail/root
並且在郵箱中看不到郵件;
解決辦法:
1.查看出錯的日誌
$ tail -n 50 /var/spool/mail/root
From MAILER-DAEMON@ltv_73 Thu Dec 12 11:35:28 2013
Return-Path:
Received: from localhost (localhost)
by ltv_73 (8.13.8/8.13.8) id rBC3ZSPe006446;
Thu, 12 Dec 2013 11:35:28 +0800
Date: Thu, 12 Dec 2013 11:35:28 +0800
From: Mail Delivery Subsystem
Message-Id: <201312120335.rBC3ZSPe006446@ltv_73>
To:
MIME-Version: 1.0
Content-Type: multipart/report; report-type=delivery-status;
boundary="rBC3ZSPe006446.1386819328/ltv_73"
Subject: Returned mail: see transcript for details
Auto-Submitted: auto-generated (failure)
This is a MIME-encapsulated message
--rBC3ZSPe006446.1386819328/ltv_73
The original message was received at Thu, 12 Dec 2013 11:35:18 +0800
from 41_154 [127.0.0.1]
----- The following addresses had permanent fatal errors -----
(reason: 530 5.7.1 Client was not authenticated)
----- Transcript of session follows -----
...while talking to mail.163.com.:
>>> MAIL From: SIZE=519
<<< 530 5.7.1 Client was not authenticated
554 5.0.0 Service unavailable
--rBC3ZSPe006446.1386819328/ltv_73
Content-Type: message/delivery-status
Reporting-MTA: dns; ltv_73
Received-From-MTA: DNS; sohu_41_154
Arrival-Date: Thu, 12 Dec 2013 11:35:18 +0800
Final-Recipient: RFC822; [email protected]
Action: failed
Status: 5.7.1
Diagnostic-Code: SMTP; 530 5.7.1 Client was not authenticated
Last-Attempt-Date: Thu, 12 Dec 2013 11:35:28 +0800
2.修改DNS
$ vim /etc/resolv.conf
修改成可用的DNS,這個地方的修改保存後即生效;
3.修改主機名【也可不做這一步】
$ sysctl kernel.hostname=newhostname
4.重啟郵件服務
$ service sendmail restart
;