在PHP邮件中添加消息体可以通过使用邮件库或者PHP内置的邮件函数来实现。以下是一种常见的方法:
Body
或msgHTML
方法来设置消息体内容,可以是纯文本或HTML格式。send
方法发送邮件。示例代码:
require 'path/to/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->setFrom('sender@example.com', 'Sender Name');
$mail->addAddress('recipient@example.com', 'Recipient Name');
$mail->Subject = 'Email Subject';
$mail->Body = 'This is the message body in plain text.';
// 或者使用以下代码设置HTML格式的消息体
// $mail->msgHTML('<h1>This is the message body in HTML format.</h1>');
if ($mail->send()) {
echo 'Email sent successfully.';
} else {
echo 'Error sending email: ' . $mail->ErrorInfo;
}
mail
函数):mail
函数来发送简单的文本邮件。mail
函数来发送邮件,其中包括收件人、主题、消息体等参数。示例代码:
$to = 'recipient@example.com';
$subject = 'Email Subject';
$message = 'This is the message body in plain text.';
$headers = 'From: sender@example.com' . "\r\n" .
'Reply-To: sender@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if (mail($to, $subject, $message, $headers)) {
echo 'Email sent successfully.';
} else {
echo 'Error sending email.';
}
无论使用哪种方法,你都可以在消息体中添加任何你想要的内容,包括文本、HTML、图片等。
领取专属 10元无门槛券
手把手带您无忧上云