1

I am new to php and currently doing a project

I stored data in database by using a form.

I have a mail function, when on clicking the submit button, a mail is send to reputed email id. I am using phpmailer library.

This is my mail function

class mail 
{

    public function sendMail(){

    require 'vendor/autoload.php';

        $mail = new PHPMailer();  // Passing `true` enables exceptions or null without exception


    //Server settings
    $mail->SMTPDebug = 0;                                 // Enable verbose debug output
    $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 = 'xyz@gmail.com';                 // SMTP username
    $mail->Password = 'xyz123';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('xyz@gmail.com', 'xyz');
    $mail->addAddress('abc@gmail.com', 'abc');

    $mail->addCC('zxc@gmail.com');


    //Attachments
    $mail->addAttachment('lob.png', 'sample.png');         // Add attachments
       // Optional name

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Report';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';


    if (!$mail->send()) {
            return "Error sending message" . $mail->ErrorInfo;
        } else {
            return "Message sent!";
        }


}

}

This is my code to store data into database as send.php

if(isset($_POST['submit'])){
$name = $_POST['name'];
$age = $_POST['age'];
$quality = $_POST['quality'];

$sql = "INSERT INTO user(name,age,quality) VALUES('$name','$age','$quality')";
}

This is my html form

<form class="quality" method="POST" action="send.php">
<label for="name">Name</label>
<input type = "text" class = "form-quality" name="name" value="" / >
<label for="age">Age</label>
<input type="text" class ="form-quality" name="age" value="" / >
<label for="quality">Quality</label>
<input type="ratio" class ="form-quality" name="quality" value="E" / >
<input type="ratio" class ="form-quality" name="quality" value="P" / >
</form>

1) How to get the data's from database ?
2) In mail function previously sending emails to one or two id's, but i need as when i select "value = E" from ratio button , it should send to one email and if i select "value= P" it should send to another email based on user selected values stored in the database

Anyone with a best reply will be much more needed help for me

  • To retrieve data from database you can use a simple SELECT query. For your question just search on Google and you will find a lot of tutorials about php and how to get data from radio button via php. Next, code your solution and if you find some errors ask here – Sfili_81 Feb 27 '19 at 07:36
  • 2
    [How can I prevent SQL injection in PHP?](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – danblack Feb 27 '19 at 08:08

1 Answers1

1

Assuming that when you said you are doing a project is within an education environment and not for professional purpose:

When you get the data from you form on if(isset($_POST['submit'])) send those variables to the sendMail function as parameters, then inside the function you can make a variable using your parameters to build a well formed message that you can send in your $mail->Body .

Inside the function you can also check if what you recieved on your radio-button input was "E" or "P" and give different values (receivers email) and pass it to your addAddress.

If this is not a school project you MUST be very carefully on what you send or you insert to the db. Validate everything you recieve on your form before sending and go ahead and check the link in the comments about SQL injection.

This should've been a comment insetead of a reply but i don't have enough reputation to comment so sorry about that.

SpicyTacos23
  • 500
  • 5
  • 16