PHP在线发送邮件是指使用PHP脚本通过SMTP(Simple Mail Transfer Protocol)协议将电子邮件发送到指定的收件人邮箱。SMTP是一种用于传输电子邮件的标准协议,大多数邮件服务器都支持该协议。
以下是一个使用PHPMailer库发送HTML邮件的示例:
<?php
require 'vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$mail = new PHPMailer(true);
try {
// 邮件服务器设置
$mail->isSMTP();
$mail->Host = 'smtp.example.com'; // SMTP服务器地址
$mail->SMTPAuth = true;
$mail->Username = 'your_email@example.com'; // SMTP用户名
$mail->Password = 'your_password'; // SMTP密码
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
// 发件人
$mail->setFrom('from@example.com', 'Mailer');
// 收件人
$mail->addAddress('to@example.com', 'Receiver');
// 邮件内容
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = '<h1>Hello World!</h1>';
$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}";
}
?>From、Reply-To等。通过以上步骤,您应该能够成功地在PHP中实现在线发送邮件的功能。如果遇到具体问题,可以根据错误信息进行排查和解决。
没有搜到相关的沙龙