微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我很困惑:如何使用 PHP Mailer 发送 HTML 表单

如何解决我很困惑:如何使用 PHP Mailer 发送 HTML 表单

好的,所以我在过去几天里做了大量的研究并且被难住了。我有 XAMPP 和 PHP Mailer,我知道它们都在本地正常工作。

我还有一个 HTML 表单,我知道使用 Hostgator 虚拟主机可以正常工作。此 HTML 表单调用名为 send_email.PHP 的 .PHP 文件,该文件将表单内容发送到我的电子邮件

我的问题是:

  1. 使用 PHP Mailer 在我的本地主机 (XAMPP) 上发送此 HTML 表单的新 .PHP 代码(粗略地说)是什么?
  2. 如何在我的 HTML 文件调用新的 .PHP 文件
  3. 对于这个 HTML 表单和 PHP Mailer 将用来发送它的新 .PHP 文件,我需要什么文件树结构?

请注意:我更改了代码中的一些详细信息,因此不包括我的实际电子邮件地址和个人信息。

这是我的表格:

<!-- THE SUBMISSION FORM -->
<div class="container">  

          <form id="contact" action="send_email.PHP" method="post">

              <h3>Apply Today!</h3>
              <h4></h4>

              <fieldset>
                  <input placeholder="Full Name" type="text" name="name" tabindex="1" required autofocus>
              </fieldset>

              <fieldset>
                  <input placeholder="Telephone Number" type="tel" name="telephone" tabindex="2" required>
              </fieldset>

              <fieldset>
                  <input placeholder="Email Address" type="email" name="email" tabindex="3" required>
              </fieldset>

              <fieldset>
                  <input placeholder="Subject" type="text" name="subject" tabindex="4" required>
              </fieldset>
           
              <fieldset>
                  <textarea placeholder="Type your Message Here...." name="message" tabindex="5" required></textarea>
              </fieldset>
            
              <fieldset>
                  <button name="submit" type="submit" id="contact-submit" data-submit="...Sending" tabindex="6">Submit</button>
              </fieldset>

          </form> 

</div>

这是我正在使用的 send_email.PHP 文件(我知道它有效)

<?PHP session_start();    

if(isset($_POST['submit'])) {

    $from = "sender@gmail.com";
    $to = "receiver1@gmail.com";
    $to2 = "receiver2@gmail.com";
    $to3 = "receiver3@gmail.com";

    $subject = "NEW LEAD!";
    $message =
    "|---------BEGIN TRANSMISSION----------|" . PHP_EOL . 
    PHP_EOL . "The person that contacted you is: ". $_POST['name'] .
    PHP_EOL . "E-mail: " . $_POST['email'] .
    PHP_EOL . "Telephone: " . $_POST['telephone'] .
    PHP_EOL . "Subject: " . $_POST['subject'] .
    PHP_EOL . "Message: " . $_POST['message'] .  PHP_EOL .
    PHP_EOL . "|---------END TRANSMISSION----------|"; 

    $headers = "From:" . $from;

echo "Thank you for contacting us. We will will be in touch.<br/>Go to <a href='/index.PHP'>Home Page</a>"; 
    mail($to,$subject,$message,$headers);
    mail($to2,$headers);
    mail($to3,$headers);
 } else { 
echo "You must write a message. </br> Please go to <a href='/index.html'>Home Page</a>"; 
}
?> 

这是我的 PHPMAILER 文件夹中的 index.PHP 文件(我知道它有效)。

    <?PHP
//Import PHPMailer classes into the global namespace
//These must be at the top of your script,not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require 'vendor/autoload.PHP';

//Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      //Enable verbose debug output
    $mail->isSMTP();                                            //Send using SMTP
    $mail->Host       = 'smtp.gmail.com';                     //Set the SMTP server to send through
    //$mail->Host = gethostbyname('smtp.gmail.com');
    $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
    $mail->Username   = 'sender@gmail.com';                     //SMTP username
    $mail->Password   = 'sender-password';                               //SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         //Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged                                   //TCP port to connect to,use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
    $mail->Port       = 587;                                    //TCP port to connect to,use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

    //Recipients
    $mail->setFrom('receiver1@gmail.com','Mailer');
    $mail->addAddress('receiver2@gmail.com','Dylan');     

    //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->AltBody = 'This is the body in plain text for non-HTML mail clients';

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

这是我在 XAMPP 上安装 PHP Mailer 的位置:

c:/Users/dylan/.bitnami/stackman/machines/xampp/volumes/root/htdocs/PHPmailer

这是我的 HTML 表单和 send_email.PHP 的位置:

c:/Users/dylan/.bitnami/stackman/machines/xampp/volumes/root/htdocs/WEBSITE

解决方法

您根本不必更改代码;无论它从哪里运行,它都应该同样有效。只要您使用的是托管服务提供商允许的发送机制,例如他们不会阻止出站 SMTP,它应该可以正常工作。你试过了吗?

除此之外,您的文件结构看起来有点不寻常。 Composer 为你安装 PHPMailer 并在你请求时自动加载它,所以 composer 也负责它的存储位置;您不需要制作自己的 phpmailer 文件夹。如果您托管单个站点,则可能没有必要将内容放在 Web 根目录的子文件夹中,因此您可以使用更扁平的结构,例如:

c:/Users/dylan/.bitnami/stackman/machines/xampp/volumes/root/htdocs/
    index.php
    send_email.php
    vendor/
        composer/
        phpmailer

如果您托管多个站点,您通常会在 .../htdocs/ 中拥有多个文件夹,每个文件夹都具有相似的内部结构。

,

您对 php 的了解为 0

  1. 首先,当您使用 .php 文件时,您可以在其中使用 html
  2. 你觉得这是个玩笑吗?

try {
    //Server settings
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      //Enable verbose debug output
    $mail->isSMTP();                                            //Send using SMTP
    $mail->Host       = 'smtp.gmail.com';                     //Set the SMTP server to send through
    //$mail->Host = gethostbyname('smtp.gmail.com');
    $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
    $mail->Username   = 'sender@gmail.com';                     //SMTP username
    $mail->Password   = 'sender-password';                               //SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         //Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged                                   //TCP port to connect to,use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
    $mail->Port       = 587;                                    //TCP port to connect to,use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

    //Recipients
    $mail->setFrom('receiver1@gmail.com','Mailer');
    $mail->addAddress('receiver2@gmail.com','Dylan');     

    //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->AltBody = 'This is the body in plain text for non-HTML mail clients';

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

这是复制并粘贴到新的级别!

$mail->setFrom('receiver1@gmail.com','Mailer');
$mail->addAddress('receiver2@gmail.com','Dylan');

你需要设置它

  1. 整个代码都是错误的!

    去首页”; 邮件($to,$subject,$message,$headers); 邮件($to2,$subject,$message,$headers); 邮件($to3,$subject,$message,$headers); } 别的 { echo "您必须留言,请到首页"; } ?>

最后 这是我在 XAMPP 上安装 PHP Mailer 的位置:

c:/Users/dylan/.bitnami/stackman/machines/xampp/volumes/root/htdocs/phpmailer

这是我的 HTML 表单和 send_email.php 的位置:

c:/Users/dylan/.bitnami/stackman/machines/xampp/volumes/root/htdocs/WEBSITE

您将 PHPMAILER 放在您的网页目录中!!!!!!!!!1

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。