简体   繁体   中英

Sending email via gmail using php code on Go Daddy Webhost

I want to send a mail using php through Gmail. I've seen many websites how to do it but I don't Understand how to do them. I am still stuck at the code I've written at first. Please help me how to connect my website (hosted on GoDaddy) to gmail account, and tell correct path to do that.

I am writing a code for clearer vision. In this php code, I am trying to add data in database and if the code works, the user should get an email from the company

if (isset($_POST['name']) && isset($_POST['mobile']) && isset($_POST['email']) && isset($_POST['practice']) && isset($_POST['date']) && isset($_POST['time']))
  {
    include "_credentials.php";
    $name = $_POST['name'];
    $mobile = $_POST['mobile'];
    $email = $_POST['email'];
    $practice = $_POST['practice'];
    $date = $_POST['date'];
    $time = $_POST['time'];
    $sql = "INSERT INTO <database> (Name, Number, email, Practice, Time, Date) VALUES ('".$name."', ".$mobile.",'".$email."','".$practice."','".$date."','".$time."');";
    if ($conn->query($sql) === TRUE) {
      $to = $email;
      $host = "ssl://smtp.gmail.com";
      $subject = "Appointment Confirmation";
      $headers = "MIME-Version: 1.0" . "\r\n";
      $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
      $headers .= 'From: abc@gmail.com' . "\r\n";
      $txt = "Hello ".$name.",\n Your Appointment is booked with Doctor at Clinic on ".$date." at ".$time.".";
      mail($to,$subject,$message,$headers);
    } else {
      echo "Error: " . $sql . "<br>" . $conn->error;
    }
$conn->close();
  }

First of all you need need to check for error response on mail() function using the following snippet

$success = mail('example@example.com', 'My Subject', $message);
if (!$success) {
    $errorMessage = error_get_last()['message'];
}

After the error message, you will be able to sort it out

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM