php mail() 发送邮件

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 "发送失败!";
}

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值