繁体   English   中英

无法在phpmailer中设置发件人姓名,他的电子邮件和主题

[英]Can't set the sender name, his email and subject in phpmailer

我的phpmailer发送的消息没有发件人姓名,发件人电子邮件和消息主题。

我需要这样的消息:

Name: sender name
Email: sender@gmail.com
Subject: Business
Message: text of message .................... ............. ............

请帮助我修复它,以接收发件人姓名,电子邮件和主题。 谢谢。

这是我的php文件:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta http-equiv="Content-Language" content="EN" />
        <title> Contact Form</title>
    </head>
    <body>
    <?php
        //receive and save all informations
        $name=$_POST['Name'];
        $from=$_POST['E-mail'];
        $subject=$_POST['Subject'];
        $message=$_POST['Message'];

        // reading PHPmailer
        require("phpmailer/class.phpmailer.php");
        include("phpmailer/class.smtp.php");

        $mail = new phpmailer(true);
        $mail->IsSMTP();
        try {
            $mail->IsSMTP();
            $mail->CharSet="UTF-8";
            $mail->SMTPSecure = 'tls';
            $mail->Host = 'smtp.gmail.com';
            $mail->Port = 587;
            $mail->Username = 'eric21@gmail.com';
            $mail->Password = 'ter3221';
            $mail->SMTPAuth = true;
            $mail->SetFrom("$from",'website');
            $mail->AddAddress('eric@gmail.com','message has been sent. contact us');
            $mail->Subject='Website message';
            $mail->CharSet='UTF-8';
            $mail->ContentType='text/html';
            $mail->MsgHTML("$message");
            if (!$mail->send()) {
                echo 'your message was not sent. Tap "Try Again" to send the message.';
                echo 'Mailer Error: ' . $mail->ErrorInfo;
            } else {
                echo '<div class="alert alert-success container text-center" style="text-align:center" role="alert">
                        <big><b><br><br><br>Message has been sent!<br></big></b>
                    </div>
                    <div class="alert alert-success container text-center" style="text-align:center" role="alert">
                        <a href="page.html"> <big><b><button>Back </big></b></button> </a>
                    </div>';
            }
        } catch(phpmailerException $e) {
            echo "<p class='message alert alert-danger'>" . $e->errorMessage() . "</p>";
        } catch(Exception $e) {
            echo "<p class='message alert alert-danger'>" . $e->getMessage() . "</p>";
        }
    ?>
    </body>
</html>

这是我的表格

<form class="form" id="form-main" method="Post" action="1.php">
 <p class="name">
        <input name="Name" type="text" class="validate[required,custom[onlyLetter],length[0,100]] feedback-input" placeholder="Name" id="name" />
      </p>
      <p class="e-mail">
        <input name="E-mail" type="text" class="validate[required,custom[email]] feedback-input" id="e-mail" placeholder="E-mail" />
      </p>
      <p class="subject">
        <input name="subject" type="text" class="validate[required,custom[onlyLetter],length[0,100]] feedback-input" placeholder="subject" id="subject" />
      </p>
      <p class="text">
        <textarea name="message" class="validate[required,length[6,300]] feedback-input" id="message" placeholder="Message"></textarea>
      </p>


      <div class="submit">
        <input type="submit" value="SEND" id="button-blue"/>
        <div class="ease"></div>
      </div>

    </form>

您的代码基于过时的示例,并且可能正在使用旧版本- 获取最新版本。 如果希望这些提交的字段出现在消息中,则需要在调用msgHTML()之后将它们附加到$message$mail->Body 这将使您成为具有以下内容的纯文本正文:

$mail->isHTML(false);
$mail->Body = "Name: $name
Email: $email
Subject: $subject
Message: $message";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM