【问题标题】:How to Send Email from custom html form in wordpress?如何从 wordpress 中的自定义 html 表单发送电子邮件?
【发布时间】:2020-04-23 17:09:36
【问题描述】:

我创建了一个自定义 html 表单,在提交表单时,我想使用我的 mail.php 文件向所有者发送电子邮件,但是当我提交表单时,我得到了这个

此页面无法正常工作

Yoursite.com 目前无法处理

这个请求。 HTTP 错误 500

这是我的表格

<form action="mail.php">
    <label for="fname">Full Name</label>
    <input type="text" id="fname" name="name" placeholder="Your name..">

    <label for="email">Email</label>
    <input type="email" id="email" name="email" placeholder="Your email..">
   <label for="contact">Contact number</label>
    <input type="tel" id="contact" name="phone" placeholder="Your contact number..">
     <label for="subject">Description</label>
    <textarea id="subject" name="msg" placeholder="Tell us your requirements.." style="height:100px"> 
    </textarea>
    <input type="submit" value="Submit">
  </form>

这是我的 mail.php 文件

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
    $name    = $_POST["name"];
    $email   = $_POST["email"];
    $phone   = $_POST["phone"];
    $message = $_POST["msg"];
    //Server settings
    $mail->SMTPDebug = 2;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = '';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication

    $mail->Username = '';                 // SMTP username
    //$mail->Password = '';                           // SMTP password
    $mail->Password = '';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('owner@gmail.com', 'Mailer');

    $mail->addAddress('nitin@gmail.com');               // Name is optional
    $mail->addReplyTo('info@example.com', 'Information');

    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Request Enquiry';
    $mail->Body    = "             <html>     <body> <center>         <table border='0' align='center'>     <tr> <td> <b><span style='color:#005BAC;font-family:arial; font-size:20px;'>4 Way Technologies</span> </td> </tr>             </table> <table>     <tr> <td>Name : </td>             <td>$name</td>         </tr>         <tr> <td>Email : </td>         <td>$email</td>                 </tr>     <tr> <td>Phone : </td>             <td>$phone</td>             </tr>     <tr> <td>Message : </td> <td>$message</td>             </tr><tr> <td>URL : </td> <td>$url</td>             </tr>         </table>         </center>     </body>     </html>";
    //$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    // header('Location:thankyou.php');
} catch (Exception $e) {
   // 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}

我已在 Wordpress/htdocs 文件夹中插入 mail.php

【问题讨论】:

  • 您可以使用wp_mail函数使用自定义html表单发送电子邮件。

标签: php html wordpress smtp


【解决方案1】:

嘿,请在 wordpress 中插入您的邮件文件,例如 wordpress/mail.php
然后在表单操作中使用**action="&lt;?php echo home_url(); ?&gt;/mail.php"**

您还可以测试http://yoursite.com/mail.php 应该可以工作

【讨论】:

  • 当我在 wordpress 文件夹上创建文件时,它显示权限被拒绝,所以如果我执行 action="/htdocs/mail.php" 会起作用吗??
  • 这不起作用,因为您的域指向 word press 目录。您需要将邮件文件放在 word-press 目录中。
  • 主题文件夹内多了一个创建页面模板mail.php,然后创建页面send-email并将mail.php模板分配给这个页面。
  • 所以现在我正在尝试将 wordpress 文件夹权限从 755 更改为 777,但它仍然显示访问被拒绝所以有什么办法可以更改目录权限
  • 在主题文件夹内还有一个创建页面模板mail.php,然后创建页面send-email并将mail.php模板分配给该页面。然后在行动中传递 action ="pageurl" 像 yoursite.com/send-email
【解决方案2】:

你可以使用:

<?php bloginfo('stylesheet_directory'); ?>/mail.php

【讨论】:

    猜你喜欢
    • 2012-06-11
    • 2017-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-09
    • 2015-07-30
    相关资源
    最近更新 更多