Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

php163邮件发送

作者:至尊巅峰   发布日期:2025-11-29   浏览:400

要使用PHP发送邮件,你可以使用PHP内置的mail()函数或者使用第三方库如PHPMailer。

使用mail()函数发送邮件的基本语法如下:

mail($to, $subject, $message, $headers);

其中,参数说明如下:

  • $to:收件人的邮箱地址。
  • $subject:邮件的主题。
  • $message:邮件的内容。
  • $headers:可选参数,用于设置邮件的附加信息,如发件人、抄送等。

示例代码如下:

$to = "recipient@example.com";
$subject = "Test Email";
$message = "This is a test email.";

$headers = "From: sender@example.com\r\n";
$headers .= "Reply-To: sender@example.com\r\n";
$headers .= "CC: cc@example.com\r\n";

mail($to, $subject, $message, $headers);

如果你想发送HTML格式的邮件,可以在$headers中添加"Content-Type"头部信息:

$headers .= "Content-Type: text/html; charset=UTF-8\r\n";

使用第三方库PHPMailer发送邮件的示例代码如下:

require 'PHPMailer/PHPMailerAutoload.php';

$mail = new PHPMailer;

$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'your_username';
$mail->Password = 'your_password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

$mail->setFrom('sender@example.com', 'Sender Name');
$mail->addAddress('recipient@example.com', 'Recipient Name');
$mail->addReplyTo('sender@example.com', 'Sender Name');

$mail->isHTML(true);
$mail->Subject = 'Test Email';
$mail->Body = '<h1>This is a test email.</h1>';

if(!$mail->send()) {
    echo 'Email could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Email has been sent.';
}

以上是使用PHP发送邮件的基本方法,你可以根据自己的需求进行相应的配置和调整。

上一篇:php的倒序

下一篇:php将数据写到edb

大家都在看

php session用法

php 定义常量

phpisset函数

php html转图片

php后端

php爬虫框架

php读取csv文件

php+mysql动态网站开发

phpmyadmin docker

php session id

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站