【问题标题】:Send html form in php with smtp使用 smtp 在 php 中发送 html 表单
【发布时间】:2026-02-07 03:55:02
【问题描述】:

更新:我的 send.php 现在看起来像这样,当我单击发送时,得到一个空白页面并且什么都不发送。我在正确的位置有 class.phpmailer.php。

<html>
<head>
<title>PHPMailer - SMTP basic test with authentication</title>
</head>
<body>

<?php

error_reporting(E_STRICT);

date_default_timezone_set('America/Toronto');

require_once('class.phpmailer.php');

$mail             = new PHPMailer();



$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "mail.******.hu"; // SMTP server
$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
                                           // 1 = errors and messages
                                           // 2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->Host       = "mail.******.hu"; // sets the SMTP server
$mail->Port       = 465;                    // set the SMTP port for the GMAIL server
$mail->Username   = "support@******.hu"; // SMTP account username
$mail->Password   = "******";        // SMTP account password

$mail->SetFrom('support@******.hu', 'First Last');

$mail->AddReplyTo("support@******.hu","First Last");

$mail->Subject    = "PHPMailer Test Subject via smtp, basic with authentication";

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$address = "support@******.hu";
$mail->AddAddress($address, "support@******.hu");

  $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $call = $_POST['call'];
    $website = $_POST['website'];
    $priority = $_POST['priority'];
    $type = $_POST['type'];
    $message = $_POST['message'];
    $address = "support@******.hu";

    $formcontent=" From: $name \n Phone: $phone \n Call Back: $call \n Website: $website \n Priority: $priority \n Type: $type \n Message: $message";
    $formcontent= eregi_replace("[\]",'',$formcontent);
    $mail->MsgHTML($formcontent);


if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}

?>

</body>
</html>

喂! 我想用这个 php 代码在电子邮件中发送一个 html 表单,但不工作...... 我不知道为什么...如果您知道解决方案,请帮助我:) 谢谢!

HTML 表单:

<div id="stylized" class="myform">

<form id="form1" action="send.php" method="POST">

    <label>Név
        <span class="small">Kérem adja meg nevét</span>
    </label>
<input type="text" name="name">
    <label>Email
        <span class="small">Kérem valós címet adjon meg</span>
    </label>
<input type="text" name="email">


<br />
<br />

    <label>Telefon
        <span class="small">Visszahíváshoz adja meg telefonszámát</span>
    </label>
<input type="text" name="phone">

<br />
<br />

    <label>Elérhetőség
        <span class="small">Kérem adja meg mikor érhetjük el telefonon</span>
    </label>

<select name="priority" size="1">
<option value="Low">Délelőtt</option>
<option value="Normal">Délután</option>
<option value="High">Este</option>
<option value="Emergency">Egész nap</option>
</select>
<br />
<br />
<br />
    <label>Szolgáltatás
        <span class="small">Mivel kapcsolatban keres minket?</span>
    </label>
<select name="type" size="1">
<option value="update">Szolgáltatás Megrendelése</option>
<option value="change">Szolgáltatás Lemondás</option>
<option value="addition">Információ</option>
<option value="new">Hiba Bejelentése</option>
</select>
<br />
<br />
<br />

  <label>Tárgy
        <span class="small">Írja le az üzenet tárgyát</span>
    </label>
 <input type="text" name="website">

<br />
<br />
<br /> 

    <label>Üzenet
        <span class="small">Írja le üzenetét</span>
    </label>
<textarea name="message" rows="15" cols="29"></textarea><br />

    <button type="submit" value="Send" style="margin-top:15px;">Küldés</button>
<div class="spacer"></div>

</form>

</div>

还有我的 php:

<?php

    //error_reporting(E_ALL);
    error_reporting(E_STRICT);



    $mail             = new PHPMailer();


    $formcontent             = eregi_replace("[\]",'',$formcontent);

    $mail->IsSMTP(); // telling the class to use SMTP
    $mail->Host       = "mail.myserver.hu"; // SMTP server
    $mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
                                               // 1 = errors and messages
                                               // 2 = messages only
    $mail->SMTPAuth   = true;                  // enable SMTP authentication
    $mail->Host       = "mail.myserver.hu"; // sets the SMTP server
    $mail->Port       = 465;                    // set the SMTP port for the GMAIL server
    $mail->Username   = "support@myserver.hu"; // SMTP account username
    $mail->Password   = "myserverpass";        // SMTP account password

    $mail->SetFrom('email@myserver.hu', 'First Last');

    $mail->AddReplyTo("email@myserver.hu","First Last");

    $mail->Subject    = "test";

    $mail->AltBody    = "test"; // optional, comment out and test

    $mail->MsgHTML($formcontent);

    $formcontent=" From: $name \n Phone: $phone \n Call Back: $call \n Website: $website \n Priority: $priority \n Type: $type \n Message: $message";
    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $call = $_POST['call'];
    $website = $_POST['website'];
    $priority = $_POST['priority'];
    $type = $_POST['type'];
    $message = $_POST['message'];
    $address = "email@myserver.hu";



    if(!$mail->Send()) {
      echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
      echo "Message sent!";
    }

    ?>

    </body>

</html>

如果可以,请帮助我! :) 谢谢!

【问题讨论】:

  • 检查 PHP 代码的结尾。它有&lt;/body&gt;&lt;/html&gt;。还将 PHP 代码放在 send.php 文件中,并将其保存在与 HTML 文件相同的文件夹中。如果您遇到任何错误,请发布。
  • 我收到服务器错误 500 但我的 .htaccess 文件没问题。我用基本的和它的工作测试了 php 邮件程序。但我的代码不起作用

标签: php html forms email smtp


【解决方案1】:

您可能想添加$mail-&gt;SMTPSecure = "ssl";

【讨论】:

    【解决方案2】:

    您可以通过这个短代码使用邮件基本功能 PHP...:

    Class Mailer{
    
            public static function sendMailNewUser($username,$firstname,$lastname,$password){
    
                $to = $username;
                $from_name = "INFO MAIL";
                $from_mail = "noreply@mail.it";
                $subject = "New User";
    
                $uid = md5(uniqid(time()));
    
                // header
                $header = "From: ".$from_name." <".$from_mail.">\r\n";
                $header .= "MIME-Version: 1.0\r\n"; 
                $header .= "Content-Type: text/html; charset=utf8\r\n";
                $message = "Welcome $firstname $lastname, ";
                $message .= "<br><br>";
                $message .= "This is your account:<br>
                             <b>username: $to<br>
                             password: $password</b><br>";
    
    
                mail($to, $subject, $message, $header);
    
    
            }
    
    }
    

    【讨论】:

    • 无法使用,因为我的主机没有开启邮件功能。
    • 啊抱歉...我的主机是我的解决方案
    【解决方案3】:

    您只是犯了一个错误,您需要在使用它们之前放置所有变量,如下所示:

        $name = $_POST['name'];
        $email = $_POST['email'];
        $phone = $_POST['phone'];
        $call = $_POST['call'];
        $website = $_POST['website'];
        $priority = $_POST['priority'];
        $type = $_POST['type'];
        $message = $_POST['message'];
        $address = "email@myserver.hu";
    
        $formcontent=" From: $name \n Phone: $phone \n Call Back: $call \n Website: $website \n Priority: $priority \n Type: $type \n Message: $message";
        $formcontent= eregi_replace("[\]",'',$formcontent);
        $mail->MsgHTML($formcontent);
    

    【讨论】:

    • 添加 php 邮件类如:require_once('../class.phpmailer.php');
    • 添加并将class.phpmailer.php复制到正确的位置并再次获得服务器500错误:(
    • 好的。我将