郵件發送的幾種方式


進行以下方式發送郵件之前,需要對接收郵件的郵箱進行設置如下:

1、開啟IMAP/SMTP服務:

 2、設置白名單,這一步我在測試時沒有設置也能收到郵件,所以根據具體情況而定:

 

一、使用telnet命令(依賴postfix等服務)

1、確保postfix已經運行

systemctl status postfix

2、確保telnet已經安裝,沒有安裝執行

yum install -y telnet

3、執行telnet連接25號端口

telnet 127.0.0.1 25

 依次輸入以下命令:

HELO actmyself.com
MAIL FROM: <zcz@actmyself.com>
RCPT TO: <要發送的郵箱地址@qq.com>
DATA
Subject: test message
From: <zcz@actmyself.com>
To: <要發送的郵箱地址@qq.com>
Hello,
this is a test
good bye
.
QUIT

 執行完以上操作,即可在郵箱中收到郵件

 

二、使用mail命令(依賴postfix等服務)

1、設置/etc/mail.rc,這里的發件人郵箱以163為例,qq郵箱可能不同

vim /etc/mail.rc

set from=發件人郵箱@163.com
set smtp=smtp.163.com
set smtp-auth-user=發件人郵箱@163.com
set smtp-auth-password=郵箱密碼
set smtp-auth=login

2、使用mail進行郵件發送:

echo "Content" | mail -s "Title" 收件人郵箱@qq.com

3、獲取到郵件:

 

三、使用代碼實現SMTP協議(依賴postfix等服務):send-mail-use-smtp.php

<?php

$remote_address = 'tcp://127.0.0.1:25';
$client = stream_socket_client($remote_address);
$response = fread($client, 1024);
echo $response, PHP_EOL;

$request = "HELO actmyself.com\r\n";
fwrite($client, $request);
$response = fread($client, 1024);
echo $response, PHP_EOL;

$request = "MAIL FROM: <zcz@actmyself.com>\r\n";
fwrite($client, $request);
$response = fread($client, 1024);
echo $response, PHP_EOL;

$request = "RCPT TO: <1178218582@qq.com>\r\n";
fwrite($client, $request);
$response = fread($client, 1024);
echo $response, PHP_EOL;

$request = "DATA\r\n";
fwrite($client, $request);
$response = fread($client, 1024);
echo $response, PHP_EOL;

$request = "Subject: test message\r\n";
fwrite($client, $request);
$request = "From: <zcz@actmyself.com>\r\n";
fwrite($client, $request);
$request = "To: <1178218582@qq.com>\r\n";
fwrite($client, $request);
$request = "\nHello,\r\nthis is a test\r\ngood bye\r\n.\r\n";
fwrite($client, $request);
$response = fread($client, 1024);
echo $response, PHP_EOL;

$request = "QUIT\r\n";
fwrite($client, $request);
$response = fread($client, 1024);
echo $response, PHP_EOL;

fclose($client);
echo 'done', PHP_EOL;

執行即可收到郵件:

四、使用php提供的mail函數(功能簡單、依賴postfix等服務):send-mail.php

<?php
$to = '收件人的郵箱@qq.com';
$subject = '測試使用php mail函數發送郵件';
$message = '測試,勿回';

$success = mail($to, $subject, $message);

echo ($success ? '投遞成功' : '投遞失敗'), PHP_EOL;

執行即可收到郵件:

五、使用第三方庫(多功能,如群發、添加附件,依賴postfix等服務):swiftmailer(官方文檔:https://swiftmailer.symfony.com/docs/index.html)

1、Postfix郵件服務器搭建:

1.1、安裝:

yum install -y postfix

1.2、設置配置:

需要設置以下參數:

postconf -n

配置文件為:/etc/postfix/main.cf,執行以下操作,然后配置上述選項

vim /etc/postfix/main.cf

重啟postfix:

systemctl restart postfix

 設置hostname:

hostnamectl set-hostname zhengchuzhou.xyz

 

設置郵件域名,可以在dnspod上注冊:https://www.dnspod.cn/,ip可以通過curl http://ip.cn獲取

 查看設置是否生效:

dig -t mx zhengchuzhou.xyz

2、swiftmailer發送普通文字郵件

2.1、創建新目錄send-mail

2.2、composer安裝swiftmailer,在send-mail中執行:

composer require "swiftmailer/swiftmailer:^6.0"

2.3、創建demo.php,代碼如下:

<?php
require_once 'vendor/autoload.php';

$transport = (new Swift_SmtpTransport('127.0.0.1', 25));
$mailer = new Swift_Mailer($transport);

$message = (new Swift_Message('Wonderful Subject1'))
    ->setFrom(['zcz@zhengchuzhou.xyz' => 'zcz'])
    ->setTo(['1178218582@qq.com'])
    ->setBody('Hello, This is a test');

$mailer->send($message);

2.4、執行該文件,即可收到郵件

3、swiftmailer發送包含html代碼的郵件

3.1、代碼如下:demo2.php

<?php
require_once 'vendor/autoload.php';

$transport = (new Swift_SmtpTransport('127.0.0.1', 25));
$mailer = new Swift_Mailer($transport);

$html_body = <<<'EOT'
<p style="color:red">Hello</p>
My <em>amazing</em> body
EOT;

$message = (new Swift_Message('Wonderful Subject1'))
    ->setFrom(['zcz@zhengchuzhou.xyz' => 'zcz'])
    ->setTo(['1178218582@qq.com'])
    ->setBody($html_body, 'text/html');

$mailer->send($message);

3.2、其他步驟同2,執行后收到郵件如下:

4、swiftmailer發送包含圖片的郵件:

4.1、代碼如下,demo3.php:

<?php
require_once 'vendor/autoload.php';

$transport = (new Swift_SmtpTransport('127.0.0.1', 25));
$mailer = new Swift_Mailer($transport);

$html_body = <<<'EOT'
<p style="color:red">Hello</p>
My <em>amazing</em> body
EOT;

$message = (new Swift_Message('Wonderful Subject1'))
    ->setFrom(['zcz@zhengchuzhou.xyz' => 'zcz'])
    ->setTo(['1178218582@qq.com'])
    ->setBody($html_body, 'text/html');
$attachment = Swift_Attachment::fromPath('1.jpg', 'image/jpeg');
$message->attach($attachment);

$mailer->send($message);

4.2、其他步驟同2,執行后收到的郵件如下:

5、swiftmailer群發郵件:

5.1、代碼如下,demo4.php:

<?php
require_once 'vendor/autoload.php';

$transport = (new Swift_SmtpTransport('127.0.0.1', 25));
$mailer = new Swift_Mailer($transport);

$html_body = <<<'EOT'
<p style="color:red">Hello</p>
My <em>amazing</em> body
EOT;

$message = (new Swift_Message('Wonderful Subject1'))
    ->setFrom(['zcz@zhengchuzhou.xyz' => 'zcz'])
    ->setTo(['1178218582@qq.com', '15521218742@163.com'])
    ->setBody($html_body, 'text/html');
$attachment = Swift_Attachment::fromPath('1.jpg', 'image/jpeg');
$message->attach($attachment);

$mailer->send($message);

5.2、其他同上

六、使用第三方服務(安全、高效,不依賴postfix等服務):Mailgun、AWS SES、QQ企業郵箱

七、異常及日志:

1、安裝monolog:github地址:https://github.com/Seldaek/monolog

composer require monolog/monolog

2、使用logrotate對日志進行切割:

2.1、logrotate依賴crond,需要確保crond已經開啟:

systemctl status crond

2.2、查看logrotate的配置文件:

rpm -ql logrotate

2.3、在/etc/logrotate.d下編寫分隔配置文件:send-mail

/var/www/mail/send-mail/app.log
{
copytruncate
daily
su zcz zcz
}

2.4、驗證:

強制執行logrotate:

sudo logrotate -f /etc/logrotate.d/send-mail

即可看到日志文件被分割:

3、使用supervisor管理進程:

3.1、查看配置:

rpm -ql supervisor | grep etc

3.2、在/etc/supervisord.d下配置send-mail.ini文件:

[program: send-mail]
command=/usr/bin/php /var/www/mail/send-mail/consume_with_logger.php
autorestart=true
user=zcz

 3.3、啟動supervisord及查看supervisord

sudo systemctl start supervisord
sudo systemctl status supervisord

 

 3.4、驗證

 殺死進程然后查看進程是否還存在,發現依然存在,但pid發生變化,可見supervisord已經起到作用

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM