如何获取我收到的电子邮件中显示的电话号码和姓名 - 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>

然后给我的提交按钮这个属性


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

最后一件事:我想表明该消息已发送到联系表单上方,而不是页面顶部,这不是首要任务,但我仍然想知道。


哔哔one
浏览 125回答 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

相关分类

Html5