【问题标题】:PHPMailer with HTML Contact Form带有 HTML 联系表的 PHPMailer
【发布时间】:2020-05-01 23:01:12
【问题描述】:

对不起我的英语不好:( 我在这里遇到了一个大问题,我在那里有带有联系表格的 html 主页。我想用 PHPMailer 发送这个表单,当我发送一条消息时,我得到了这个但没有文本:/ 我只看到像“这是主题”这样的示例文本。有人可以帮帮我吗?

这里是 phpmailer.php 代码:

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'src/Exception.php';
require 'src/PHPMailer.php';
require 'src/SMTP.php';

$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
    //Server settings
    $mail->SMTPDebug = 0;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp.test.de';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'MY EMAIL';                 // SMTP username
    $mail->Password = 'MY PASSWORT!';                           // SMTP password
    $mail->SMTPSecure = 'ssl';                           // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 465;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('MY EMAIL');
    $mail->addAddress('MY EMAIL');     // Add a recipient



    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
}
?>

我的电子邮件只是为了安全

这里是来自 html 联系表单的代码:

<!-- Contact Form -->
<div class="row">
    <div class="col-md-8 col-md-offset-2">

        <form action="phpmailer_new.php" method="POST">
            <div class="form-group">
                <input type="text" name="mail" class="form-control" placeholder="Email Address">
            </div>
            <div class="form-group">
                <input type="text" name="subject" class="form-control" placeholder="Subject">
            </div>
            <div class="form-group">
                <textarea class="form-control" name="text" rows="3" placeholder="Your Message"></textarea>
                <button class="btn btn-default" type="submit">Send Message</button>
            </div>
        </form>
    </div>
</div>
<!-- End Contact Form -->

干杯伊夫

【问题讨论】:

  • 而不是phpmailer 为什么不试试phpmail 功能? php.net/manual/en/function.mail.php
  • 我认为我没有足够的知识来做到这一点。 xD
  • 不,不要使用mail();使用它来生成和发送有效的电子邮件要困难得多,it's prone to vulnerabilities。 PHPMailer 默认充当mail() 的前端并处理它导致的问题,但通过 SMTP 发送更快更安全。

标签: php html phpmailer contact-form


【解决方案1】:

您需要在 PHPmailer 脚本中添加变量 $_POST :

  <?php
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;

    require 'src/Exception.php';
    require 'src/PHPMailer.php';
    require 'src/SMTP.php';

    $mail = new PHPMailer(true);                              // Passing `true` enables exceptions
    try {
        //Server settings
        $mail->SMTPDebug = 0;                                 // Enable verbose debug output
        $mail->isSMTP();                                      // Set mailer to use SMTP
        $mail->Host = 'smtp.test.de';  // Specify main and backup SMTP servers
        $mail->SMTPAuth = true;                               // Enable SMTP authentication
        $mail->Username = 'MY EMAIL';                 // SMTP username
        $mail->Password = 'MY PASSWORT!';                           // SMTP password
        $mail->SMTPSecure = 'ssl';                           // Enable TLS encryption, `ssl` also accepted
        $mail->Port = 465;                                    // TCP port to connect to

        //Recipients
        $mail->setFrom('MY EMAIL');
        $mail->addAddress($_POST['mail']);     // Add a recipient



        //Content
        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject = $_POST['subject'];
        $mail->Body    = $_POST['text'];

        $mail->send();
        //echo 'Message has been sent';
        header('Location: http://www.example.com/contact.php');
        exit();
    } catch (Exception $e) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    }
    ?>

【讨论】:

  • 也许你可以帮助我解决我的最后一个问题......现在消息发送,当我点击发送按钮时,它会将我带到一个新站点,其中显示“消息已发送”是否可以这样做是为了让你留在一边,而不是来一个新的?
  • 我更改了代码,最后我添加了一个重定向到example.com/contact.php,所以将此网址更改为您希望在发送消息后看到的页面
  • 很高兴它对您有所帮助,祝您有美好的一天
  • 好吧...不是最后一个问题 xD 很抱歉在这方面是个菜鸟 :-D 但你帮了我这么快:) 我想在表格中添加 2 个复选框是否有特殊复选框的代码将其放入 html 和 php 文档中,或者我将模板中的复选框代码放入我网站的 html 中并在 php 中编写复选框代码?
  • 检查此链接以添加复选框:stackoverflow.com/questions/14781270/post-checkbox-value
猜你喜欢
  • 1970-01-01
  • 2019-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-15
  • 1970-01-01
  • 2018-03-06
相关资源
最近更新 更多