使用PHP發(fā)送包含附件的郵件需要使用PHP內(nèi)置的mail函數(shù)或者使用第三方庫(kù),如PHPMailer。以下是使用PHPMailer發(fā)送帶附件的郵件的示例代碼:
<?php
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(); // 設(shè)置使用SMTP發(fā)送郵件
$mail->Host = 'smtp.example.com'; // SMTP服務(wù)器地址
$mail->Port = 587; // SMTP端口號(hào)
$mail->SMTPAuth = true; // 啟用SMTP身份驗(yàn)證
$mail->Username = 'your_email@example.com'; // SMTP用戶名
$mail->Password = 'your_email_password'; // SMTP密碼
$mail->SMTPSecure = 'tls'; // 啟用TLS加密
$mail->setFrom('from@example.com', 'Sender Name'); // 發(fā)件人郵箱及姓名
$mail->addAddress('recipient@example.com', 'Recipient Name'); // 收件人郵箱及姓名
$mail->Subject = 'Test Email with Attachment'; // 郵件主題
$mail->Body = 'This is a test email with attachment.'; // 郵件內(nèi)容
$mail->addAttachment('path/to/attachment.pdf'); // 添加附件
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
在上面的代碼中,首先需要引入PHPMailer庫(kù),然后設(shè)置SMTP相關(guān)信息,包括SMTP服務(wù)器地址、端口號(hào)、SMTP用戶名、SMTP密碼和SMTP加密方式。接著設(shè)置發(fā)件人和收件人信息、郵件主題和內(nèi)容,最后使用addAttachment方法添加附件。發(fā)送郵件時(shí),調(diào)用send方法即可。
請(qǐng)注意,以上示例僅供參考,實(shí)際使用時(shí)請(qǐng)根據(jù)自己的需求和郵箱配置進(jìn)行相應(yīng)調(diào)整。