php mail() 发送邮件
记录 php 函数 mail() 发送邮件、附件
windows 系统
php 8.0.2
# 先修改 php.ini
# 在文件内搜索 mail function
# sendmail.exe 下载地址 https://www.glob.com.au/sendmail/sendmail.zip?54d0e6fa1ca4fa1d3033cb60990c0d13
# SMTP、smtp_port、sendmail_from 、sendmail_path 修改成对应参数,去分号
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost # 发件SMTP服务器 smtp.163.com
; http://php.net/smtp-port
smtp_port = 25 # SMTP 端口
; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = me@example.com # 发件人邮箱 xxx@163.com
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
sendmail_path ="D:/phpstudy_pro/sendmail/sendmail.exe -t" # sendmail.exe 地址
# "D:/phpstudy_pro/sendmail/sendmail.exe -t"
# 修改 sendmail 文件夹下 sendmail.ini 配置文件
[sendmail]
smtp_server=mail.mydomain.com # 发件SMTP服务器 smtp.163.com
smtp_port = 25 # SMTP 端口
smtp_ssl=auto # ssl 25 默认就行
auth_username= # 发件人邮箱
auth_password= # 发件人邮箱授权密码
<?php
/** 邮件 **/
mail("收件人邮箱","邮件主题","邮件正文","From: 发件人邮箱 ");
<?php
/** 邮件加附件 **/
$to = "xxx@qq.com"; // 收件人邮箱
$subject = "邮件主题"; // 邮件主题
$message = "This is the first email."; // 邮件正文
$from = 'xxx@163.com'; // 发件人邮箱
/******************** 附件 ********************/
$fileName = '1.jpg';
$path = ''.$fileName; // 当时文件在同一文件夹下
$content = file_get_contents($path);
$content = chunk_split(base64_encode($content));
$separator = md5(time());
$br = "\r\n";
$headers = "From: $from " . $br;
$headers .= "MIME-Version: 1.0" . $br;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $br;
$headers .= "Content-Transfer-Encoding: 8bit" . $br;
$headers .= "This is a MIME encoded message." . $br;
$msg = "--" . $separator .$br;
$msg .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $br;
$msg .= "Content-Transfer-Encoding: 8bit" . $br. $br;
$msg .= $message . $br;
$msg .= "--" . $separator . $br;
$msg .= "Content-Type: application/octet-stream; name=\"" . $fileName . "\"" . $br;
$msg .= "Content-Transfer-Encoding: base64" . $br;
$msg .= "Content-Disposition: attachment" . $br. $br;
$msg .= $content . $br;
$msg .= $content . $br;
$msg .= "--" . $separator . "--";
/******************** 附件 ********************/
if(mail($to,$subject,$msg,$headers)){
echo "发送成功!";
}else{
echo "发送失败!";
}