我如何获得我收到的电子邮件中显示的电话号码和姓名 - PHPMailer

所以我收到的电子邮件只显示主题、发件人、发件人,而不是发件人姓名或电话号码,这不是在本地完成的,而是从我的网站托管的地方完成的。我希望它在正文后显示发件人姓名和电话号码。这是我的代码:


<?php


use PHPMailer\PHPMailer\PHPMailer;

use PHPMailer\PHPMailer\Exception;

use PHPMailer\PHPMailer\SMTP;


require 'vendor/autoload.php';


    $mail = new PHPmailer();


    $mail->Host = "";

    $mail->isSMTP();

    $mail->SMTPAuth = true;

    $mail->SMTPDebug = SMTP::DEBUG_OFF;

    $mail->Username = "";

    $mail->Password = "";

    $mail->SMTPSecure = "tls";

    $mail->Port = 587;


    $mail->addAddress('');

    $mail->setFrom($_POST['email']);

    $mail->Subject = $_POST['subject'];

    $mail->isHTML(true);

    $mail->Body = $_POST['message'];

    $mail->Name = $_POST['name'];

    $mail->Phone = $_POST['number'];



    if(!$mail->send()) {

    echo 'Message could not be sent.';

    echo 'Mailer Error: ' . $mail->ErrorInfo;

    } else {

    echo 'Message has been sent';


    }

        //echo $mail->ErrorInfo;

?>

这是我的html:


<form action="contact.php" method="post" class="signin-form mt-lg-5 mt-4">

    <div class="input-grids">

        <input type="text" name="name" placeholder="Full name or Business" 

        class="contact-input" required=""/>


        <input type="email" name="email" placeholder="Your email" class="contact- 

        input" required=""/>


        <input type="text" name="subject" placeholder="Subject" class="contact- 

        input" required=""/>


        <input type="text" name="number" placeholder="Phone number" 

        class="contact-input" />

    </div>

    <div  class="form-input">

        <textarea name="message" placeholder="Type your message here" 

        required=""></textarea>

    </div>

    <div  class="form-input mb-5">

        <label class="container"><p>Send me a copy <a href="#">privacy policy. 

        </a></p>

        <input type="checkbox">

        <span class="checkmark"></span>

        </label>

    </div>

    <button class="btn submit">Submit</button>

</form>


最后一件事:我想表明消息是在联系表单上方而不是在页面顶部发送的,它不是最重要的,但我仍然想知道。


慕勒3428872
浏览 101回答 1
1回答

墨色风雨

您基本上想创建一个$message变量并将其分配给$mail->Body.您显示的第二个示例很接近,但您需要确保以下内容在您的 $_POST['submit'] if 语句中。$body = $_POST['message'];$name = $_POST['name'];$phone = $_POST['number'];像这样:&nbsp; &nbsp; if (isset($_POST['submit'])) {&nbsp; &nbsp; &nbsp; &nbsp; require 'vendor/autoload.php';&nbsp; &nbsp; $body = $_POST['message'];&nbsp; &nbsp; $name = $_POST['name'];&nbsp; &nbsp; $phone = $_POST['number'];&nbsp; &nbsp; &nbsp; &nbsp; $mail = new PHPmailer();&nbsp; &nbsp; &nbsp; &nbsp; $mail->Host = "";&nbsp; &nbsp; &nbsp; &nbsp; $mail->isSMTP();&nbsp; &nbsp; &nbsp; &nbsp; $mail->SMTPAuth = true;&nbsp; &nbsp; &nbsp; &nbsp; $mail->SMTPDebug = SMTP::DEBUG_OFF;&nbsp; &nbsp; &nbsp; &nbsp; $mail->Username = "";&nbsp; &nbsp; &nbsp; &nbsp; $mail->Password = "";&nbsp; &nbsp; &nbsp; &nbsp; $mail->SMTPSecure = "tls";&nbsp; &nbsp; &nbsp; &nbsp; $mail->Port = 587;&nbsp; &nbsp; &nbsp; &nbsp; $mail->addAddress('');&nbsp; &nbsp; &nbsp; &nbsp; $mail->setFrom($_POST['email']);&nbsp; &nbsp; &nbsp; &nbsp; $mail->Subject = $_POST['subject'];&nbsp; &nbsp; &nbsp; &nbsp; $mail->isHTML(true);&nbsp; &nbsp; &nbsp; &nbsp; $mail->Body = $body . "<br>Phone number: " . $number . "<br>Name: " . $name;$response = $mail->send(); //to actually send the email&nbsp; &nbsp; }对于: 最后一件事:我想表明消息是在联系表单上方而不是在页面顶部发送的,这不是最重要的,但我仍然想知道。您可以通过 ajax 提交表单并听取 $response 值。如果它是 1 那么很好,否则就会出现问题。
打开App,查看更多内容
随时随地看视频慕课网APP