0

Hi everyone i am struggling to fix my problem. I am trying to create a ticket system which a user can send a ticket and ask for help. The problem is that email is not working - it will not send any message after the user submit. I copied this codes on the internet and make some few changes of it here's the code

new_ticket.php

<form action = "action_ticket.php" method="POST">
    <div class="container mt-4">
        <div class="row">
           <div class="col-12">
               <div class="card">

                   <div class="card-header">Submit a ticket about your concern <span class="text-danger">Note: Please keep your ticket ID for security purposes</span></div>
                 
                   <div class="card-body">
                

          <div class="row">
                            <div class="col-12 col-md-6">
                                <div class="form-group">
                                    <label>Your E-Mail Address <span class="text-danger">*</span></label>
                                    <input type="email" required name="email" id="email" autocomplete = "off" placeholder="Your Email Address" class="form-control">
                                </div>
                            </div>
                            <div class="col-12 col-md-6">
                                    <div class="form-floating mb-3">
                                    <select id="department" name="department" class="form-select">
                                        <option value="" selected="selected" disabled="disabled">where you want so send this ticket</option>

                                    </select>
                                    <label for="floatingSelect">Select<span class="text-danger">*</span></label>
                                    </div>
                                </div>
                                <div class="col-12 col-md-6">
                                <div class="form-group">
                                    <label>Subject <span class="text-danger">*</span></label>
                                    <input type="text" required name="subject" id="subject" autocomplete = "off" placeholder="Subject means your concern" class="form-control">
                                </div>
                            </div>
                            <div class="col-12">
                                <div class="form-group">
                                    <textarea name="message" class="form-control" id="message" cols="30" rows="5" placeholder="Explain your concern here"></textarea>
                                </div>
                            </div>
                            <div class="col-12 col-md-12">
                                <div class="text-right">
                                    <input type="hidden" name="submit" value="form">
                                    <button class="btn btn-outline-primary" type="submit">Submit</button>
                                    <a href="pages-faq.php?id=<?php echo $_SESSION["login_key"];?>" class="btn btn-outline-secondary">Back</a>
                                </div>
                            </div>
                       </div>
                   </div>
               </div>
           </div>
        </div>
    </div>
</form>

and this is my other file

action_ticket.php

if(isset($_POST['submit'])){

    $email=$_POST['email'];
    $subject=$_POST['subject'];
    $message=$_POST['message'];
  

    $unid=random_bytes(10);
    $unid=bin2hex($unid);

    $db=new DB();

    $sql="INSERT INTO hdms_tickets (ticket_id,status,email,subject,message) 
    VALUES ('$unid','0','$email','$subject','$message')" or die("<script>alert('Error');</script>");

    $inset=$db->conn->query($sql);
    if($inset){
        $success='Your ticket has been created. Your ticket id is '.$unid ;
        $to = $email;
        $subject = "helpdesk support Ticket";
        // Always set content-type when sending HTML email
        $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
        $txt = "Dear  your tickets has been sent to our help desk support team and we will back to you shortly. and here your ticket id $unid please keep your ticket id";
        $headers = "From: helpdesk@gmail.com" . "\r\n";
        mail($to,$subject,$txt,$headers);
    }else{
       $error = "Ticket did not send!";
    }
    echo '<script>showAlert();window.setTimeout(function () {HideAlert();},3000);</script>';  echo "<meta http-equiv='refresh' content='0;url=view_ticket.php'>"; }

the tickets submits to the backend and received by the staff but the problem is that the email is not automatically send to the user once the user submit the ticket i actually upload this to online sever this is for my final project Thank you in advance have a great day!

brombeer
  • 8,716
  • 5
  • 21
  • 27
Nelson
  • 1
  • 1
  • 1
  • Unrelated? `$headers = "From: helpdesk@gmail.com" . "\r\n";` will overwrite your previously defined `$headers` – brombeer Apr 11 '22 at 07:36
  • how do i fix it ? – Nelson Apr 11 '22 at 07:39
  • `$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";` adds to the existing string, so why not also use that same approach? – brombeer Apr 11 '22 at 07:40
  • so you mean sir i have to replace this $headers = "From: helpdesk@gmail.com" . "\r\n"; and use this instead $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";? – Nelson Apr 11 '22 at 07:41
  • (Don't assume I'm a _sir_) Are you aware of the difference between `$headers = ...` and `$headers .= ...`? To make it short, use `$headers .= "From: helpdesk@gmail.com" . "\r\n";` if you don't want to lose your `MIME-Type` and `Content-type` headers – brombeer Apr 11 '22 at 07:47
  • Okay I'm sorry . I am not aware of the difference since I'm new to programming that's why it takes me days and yet can't figure it out what's the problem – Nelson Apr 11 '22 at 07:54
  • No problem. Maybe read the [Mail](https://www.php.net/manual/en/book.mail.php) manual, create a little test script that only sends mail to make sure mailing is set up correct. Also consider using a lib like PHPMailer to send mails – brombeer Apr 11 '22 at 07:58
  • Does this answer your question? [PHP mail function doesn't complete sending of e-mail](https://stackoverflow.com/questions/24644436/php-mail-function-doesnt-complete-sending-of-e-mail) – Tangentially Perpendicular Apr 11 '22 at 08:31
  • @brombeer okay i will study phpmailer thank you – Nelson Apr 12 '22 at 01:57

0 Answers0