电子邮件 是最早的互联网应用之一。
一个 电子邮件系统 至少由三部分组成
一封 电子邮件 由两个部分组成
一封 电子邮件地址 由三个部分组成
总结一下就是 三种协议,一种格式
echo -n "bootstrap@example.com" | base64
echo -n "SendEmails" | base64
AUTH LOGIN
Ym9vdHN0cmFwQGV4YW1wbGUuY29t
U2VuZEVtYWlscw==
\0
printf "\0%s\0%s" "bootstrap@example.com" "SendEmails" | base64
AUTH PLAIN
AGJvb3RzdHJhcEBleGFtcGxlLmNvbQBTZW5kRW1haWxz
\0
,字符串要以 auth 开头 printf "auth\0%s\0%s" "bootstrap@example.com" "SendEmails" | base64
AUTH PLAIN YXV0aABib290c3RyYXBAZXhhbXBsZS5jb20AU2VuZEVtYWlscw==
一个收件人的例子
HELO example.com
AUTH LOGIN
MAIL FROM:<alice@example.com>
RCPT TO:<bob@example.com>
DATA
Date: Mon, 4 April 2022
From: Alice <alice@example.com>
Subject: Eggs benedict casserole
To: Bob <bob@example.com>
Hi Bob,
I will bring the eggs benedict casserole recipe on Friday.
-Alice
.
QUIT
多个个收件人的例子
HELO example.com
AUTH LOGIN
MAIL FROM:<alice@example.com>
RCPT TO:<bob1@example.com>
RCPT TO:<bob2@example.com>
DATA
Date: Mon, 4 April 2022
From: Alice <alice@example.com>
Subject: Eggs benedict casserole
To: Bob <bob@example.com>
Hi Bob,
I will bring the eggs benedict casserole recipe on Friday.
-Alice
.
QUIT
sendmail
busybox 中的 sendmail
-S
表示SMTP服务器的地址和端口号-au
表示发送邮箱名-ap
表示发送邮箱授权码 sendmail -f from@xx.com -t to@xx.com -S smtp.qq.com:465 -auxxx -apxxxx -s mail.txt
cat mail.txt | sendmail -f from@xx.com -t to@xx.com -S smtp.qq.com:465 -auxxx -apxxxx
使用标准输入的例子
cat <<- EOF | sendmail -f from@xx.com -t to@xx.com -S smtp.qq.com:465 -auxxx -apxxxx
Date: Fri, 5 Apr 2024 06:27:52 +0000
To: bob1@example.com, bob2@example.com, bob3@example.com
From: Mailer <alice@example.com>
Subject: Here is the subject
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
This is the test message
EOF
curl
curl -v --ssl --url 'smtps://smtp.qq.com:465/smtp.qq.com' \
--user 'alice@example.com:NvbQBTZW5kRW' \
--mail-from 'alice@example.com' \
--mail-rcpt 'bob@example.com' \
--login-options AUTH=LOGIN \
--upload-file mail-curl.txt
cat mail-curl.txt | curl -v --ssl --url 'smtps://smtp.qq.com:465/smtp.qq.com' \
--user 'alice@example.com:NvbQBTZW5kRW' \
--mail-from 'alice@example.com' \
--mail-rcpt 'bob@example.com' \
--login-options AUTH=LOGIN \
--upload-file -
从标准输入中读取邮件
cat <<- EOF | curl -v --no-progress-meter --url 'smtp://127.0.0.1:25/NB4045' \
--user 'alice@example.com:NvbQBTZW5kRW' \
--mail-from 'alice@example.com' \
--mail-rcpt 'bob1@example.com' \
--mail-rcpt 'bob2@example.com' \
--mail-rcpt 'bob3@example.com' \
--upload-file - \
Date: Fri, 5 Apr 2024 06:27:52 +0000
To: bob1@example.com, bob2@example.com, bob3@example.com
From: Mailer <alice@example.com>
Subject: Here is the subject
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
This is the test message
EOF
[mail function]
; For Win32 only.
; https://php.net/smtp
SMTP = localhost
; https://php.net/smtp-port
smtp_port = 25
;sendmail_from = me@example.com
// 设置收件人地址
$to = 'asd <asd@123.com>, asd2 <asd2@123.com>';
// 设置邮件主题
$subject = 'subject';
// 设置邮件正文
$message = '<p>message</p>';
// 设置邮件头部信息,包括发件人地址和回复地址
$additional_headers = [
'From' => 'Mailer <1643253513@qq.com>',
'Content-Type' => 'text/html; charset=utf-8',
'Reply-To' => 'bing@example.com',
];
$additional_params = '';
// 调用 mail 函数发送邮件
if (mail($to, $subject, $message, $headers, $additional_params)) {
echo "邮件发送成功";
} else {
echo "邮件发送失败";
}
// 理论上是可以在 message 里塞附件的,
// 一些版本的 sendmail 可以在命令行里设置 smtp 地址和账号密码,这时就可以通过 additional_params 变量设置
// mail 函数里的 additional_params 参数可以设置 mat 的更多的命令行参数,
// 其实就类似于这样
// echo "邮件正文" | sendmail -t -i additional_params
https://github.com/PHPMailer/PHPMailer
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
require 'vendor/autoload.php';
//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_LOWLEVEL; //Enable verbose debug output
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'smtp.qq.com'; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = 'joe@example.net'; //SMTP username
$mail->Password = 'matthew'; //SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
$mail->Port = 465; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
//Recipients
$mail->setFrom('joe@example.net', 'Mailer');
// $mail->addAddress('joe@example.net', 'Joe User'); //Add a recipient
$mail->addAddress('info@example.com'); //Name is optional
// $mail->addReplyTo('info@example.com', 'Information');
// $mail->addCC('cc@example.com');
// $mail->addBCC('bcc@example.com');
//Attachments
// $mail->addAttachment('/var/tmp/file.tar.gz'); //Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); //Optional name
//Content
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
require './vendor/autoload.php';
use Laminas\Mail\Message;
$message = new Message();
$message->addFrom('matthew@example.org', 'Matthew Somelli');
$message->addTo('foobar@example.com', 'foobar');
$message->addTo('foobar2@example.com', 'foobar2');
$message->addCc('ralph@example.org', 'ralph');
$message->addCc('ralph2@example.org', 'ralph2');
$message->addBcc('enrico@example.org', 'enrico');
$message->addBcc('enrico2@example.org', 'enrico2');
$message->addReplyTo('matthew@example.com', 'Matthew');
$message->setSender('matthew@example.org', 'Matthew Sommeli');
$message->setSubject('Sending an email from Laminas\Mail!');
// $message->setEncoding('UTF-8');
$message->setBody('This is the message body.');
// echo $message->toString(); // 这样可以生成一个 IMF 字符串,
$transport = new \Laminas\Mail\Transport\Smtp();
$options = new \Laminas\Mail\Transport\SmtpOptions([
'name' => 'localhost',
'host' => '127.0.0.1',
// 'connection_class' => 'login',
'connection_class' => 'plain',
'connection_config' => [
'username' => 'user',
'password' => 'pass',
],
]);
$transport->setOptions($options);
$transport->send($message);