简体   繁体   English

使用 php 代码通过 gmail 发送 email

[英]Sending email via gmail using php code on Go Daddy Webhost

I want to send a mail using php through Gmail.我想通过 Gmail 使用 php 发送邮件。 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.请帮助我如何将我的网站(托管在 GoDaddy 上)连接到 gmail 帐户,并告诉正确的路径。

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在这个 php 代码中,我正在尝试在数据库中添加数据,如果代码有效,用户应该从公司获得 email

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首先,您需要使用以下代码段检查 mail() function 上的错误响应

$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出现错误信息后,您将能够对其进行整理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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