我是新手,我想问一些比我更了解的人。
我的网站全部为工作项目完成,并且已经可以发送带附件等的电子邮件。
现在我想将我的索引的标题放入php页面。有人得到了解决方案吗? 到目前为止我的PHP:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/Exception.php';
require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/SMTP.php';
$mail = new PHPMailer(true);
try {
//Server settings
$mail->CharSet = 'UTF-8';
#$mail->SMTPDebug = 2;
$mail->isSMTP();
$mail->Host = 'localhost';
$mail->SMTPAuth = false;
$mail->Port = 25;
//Recipients
$mail->setFrom('peter@localhost.org', 'Mailer');
$mail->addAddress($_POST["email"], 'New User');
$mail->addAddress('peter@localhost.org');
//Content
$mail->AltBody = 'test non-HTML';
$mail->send();
echo 'E-Mail has been sent';
}
catch (Exception $e) {
echo 'E-Mail wurde gesendet.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
?>
答案 0 :(得分:1)
如果我了解您需要将网站的HTML标题集成到电子邮件标题中?
所以试试这个:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/Exception.php';
require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/SMTP.php';
$mail = new PHPMailer(true);
try {
//Server and e-mail settings
$mail->CharSet = 'UTF-8';
#$mail->SMTPDebug = 2;
$mail->isSMTP();
$mail->Host = 'localhost';
$mail->SMTPAuth = false;
$mail->Port = 25;
$mail->IsHTML(true);
//Recipients
$mail->setFrom('peter@localhost.org', 'Mailer');
$mail->addAddress($_POST["email"], 'New User');
$mail->addAddress('peter@localhost.org');
//HTML header
ob_start();
?>Your html content here with body, divs, ...<?php
$header = ob_get_flush();
//Content
$content = "<div>test HTML</div>";
//Prepare & send
$mail->Body = $header . $content;
$mail->AltBody = 'test non-HTML';
$mail->send();
echo 'E-Mail has been sent';
}
catch (Exception $e) {
echo 'E-Mail wurde gesendet.<br>Mailer Error: '. $mail->ErrorInfo;
}
答案 1 :(得分:1)
不确定是否完全理解你......
是否要在邮件正文中包含索引标题,如果是这样,您可以将其添加到代码中:
$html = file_get_contents('your_header.html');
$mail->IsHTML(true);
$mail->MsgHTML($html);
或者你想在你的php页面中包含索引标题?你可以通过简单地将include('your_header.html')
放在php文件的开头来做到这一点。
PHPMailer文档:here
答案 2 :(得分:-2)
邮件标题示例:
<?php
$to = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>