我试图制作一个简单的PHP联系表单,但事实证明我需要使用SMTP和PHPMailer,因为它托管在Windows服务器上。 我已经使用PHPMailer网站上的示例文档设置了联系表单。我无法从表单中提取数据。电子邮件会发送,但在正文中显示为:
> Name: $name
>
> Email: $email
>
> Message: $comment
我知道Stackoverflow上有很多关于此的问题,但我似乎无法弄明白。
PHP
<?php
require 'PHPMailer/PHPMailerAutoload.php';
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$comment = $_REQUEST['comment'];
$mail = new PHPMailer;
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mail.example.co.uk'; // Specify main and backup SMTP servers
$mail->SMTPAuth = false; // Enable SMTP authentication
$mail->Username = 'test@example.co.uk'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 25; // TCP port to connect to
$mail->From = 'test@example.co.uk';
$mail->FromName = 'Mailer';
$mail->addAddress('test@example.co.uk', 'Bob'); // Add a recipient
//$mail->addAddress('ellen@example.com'); // Name is optional
//$mail->addReplyTo('info@example.com', 'Information');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = '<p><strong>Name:</strong> $name</p>
<p><strong>Email:</strong> $email</p>
<p><strong>Message:</strong> $comment</p>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
HTML
<form role="form" method="post" action="mail.php">
<p><label for="name">Name</label></p>
<p><input type="text" id="name" name="name" placeholder="First & Last Name" value=""></p>
<p><label for="subject" >Subject</label></p>
<p><input type="text" id="subject" name="subject" placeholder="Subject" value=""></p>
<p><label for="email" >Email</label></p>
<p><input type="email" id="email" name="email" placeholder="example@domain.com" value=""></p>
<p><label for="comment">Message</label></p>
<p><textarea rows="4" id="comment" name="comment"></textarea></p>
<p><input id="submit" name="submit" type="submit" value="Send"></p>
</form>
答案 0 :(得分:1)
$ mail-&gt; body 的代码是:
$mail->Body = '<p><strong>Name:</strong> $name</p>
<p><strong>Email:</strong> $email</p>
<p><strong>Message:</strong> $comment</p>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
只需替换为以下代码:
$mail->Body = '<p><strong>Name: </strong>'.$name.'</p>
<p><strong>Email: </strong>'.$email.'</p>
<p><strong>Message: </strong>'.$comment.'</p>';
$mail->AltBody = ' This is the body in plain text for non-HTML mail clients';