在PHP中,处理邮件附件需要使用PHPMailer类。以下是使用PHPMailer发送带附件的邮件的步骤:
composer require phpmailer/phpmailer use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; $mail = new PHPMailer(true); $mail->isSMTP(); $mail->Host = 'smtp.example.com'; // 请替换为你的SMTP服务器地址 $mail->SMTPAuth = true; $mail->Username = 'your_email@example.com'; // 请替换为你的发件人邮箱 $mail->Password = 'your_password'; // 请替换为你的邮箱密码 $mail->SMTPSecure = 'tls'; $mail->Port = 587; // 或者使用其他端口,如25、465等 $mail->setFrom('your_email@example.com', 'Your Name'); // 请替换为你的发件人邮箱和姓名 $mail->addAddress('recipient@example.com', 'Recipient Name'); // 请替换为收件人邮箱和姓名 $mail->isHTML(true); $mail->Subject = 'Email with Attachment'; $mail->addAttachment('/path/to/your/attachment.txt', 'Attachment Name'); // 请替换为附件的文件路径和附件名 try { $mail->send(); echo 'Message has been sent'; } catch (Exception $e) { echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"; } 这样,你就可以使用PHPMailer发送带附件的邮件了。请确保替换示例中的占位符为实际的信息。