如何用php向Gmail发送邮件?

如何用php向Gmail发送邮件?,php,email,gmail,Php,Email,Gmail,我尝试在我的网站上创建contactUs页面,现在我在本地主机上使用xampp和netbeans 文件contactUs.php: 我的问题是它不起作用。我正在填写表格并提交,收到成功消息,但在我的Gmail帐户中没有收到电子邮件 我还需要什么吗?我想我需要从发送表单的位置定义它;在php.ini文件中定义一些东西也许?也许gmail会拒绝你的请求。 因为您使用的是默认的邮件功能 您可以使用smtp协议发送电子邮件,使用pop3接收电子邮件。您是否用php配置了电子邮件 您可以使用phpinfo

我尝试在我的网站上创建contactUs页面,现在我在本地主机上使用xampp和netbeans

文件contactUs.php:

我的问题是它不起作用。我正在填写表格并提交,收到成功消息,但在我的Gmail帐户中没有收到电子邮件


我还需要什么吗?我想我需要从发送表单的位置定义它;在php.ini文件中定义一些东西也许?

也许gmail会拒绝你的请求。 因为您使用的是默认的邮件功能


您可以使用smtp协议发送电子邮件,使用pop3接收电子邮件。

您是否用php配置了电子邮件

您可以使用phpinfo检查您的配置;更改php.ini,您可以通过调整SMTP选项等使用internet提供商为发送电子邮件提供的信息。这仅适用于windows。您可以使用ISP为您提供的信息进行internet连接

第二种选择是使用sendmail发送电子邮件,这样您就可以使用gmail作为xampp的smtp服务器。这里有说明:sendmail是xampp中内置的allready
从服务器发送电子邮件有时会导致间歇性问题。如果我是你,我会使用外部smtp服务,比如gmail。我创建了一个简单的代码来测试您的邮件服务器发送电子邮件,请在此处查看:
您可以尝试许多配置,有关更多详细信息,请参阅说明

是否已选中“垃圾邮件”文件夹@gmail?是否已在本地配置发送邮件?问题可能是由于设置了不同于服务器主机名的From:头。GMail可能会把这些邮件扔进垃圾邮件文件夹。嗨,我检查了垃圾邮件文件夹,那里什么都没有@COLO如何在本地配置发送邮件?我只是做了上面在php文件中写的事情。您用php配置邮件了吗??默认的邮件功能应该可以正常工作,谷歌不会拒绝你的请求。可能是在两行之间的某个地方被拒绝了嗨,我没有配置php.ini。我需要换成这样?SMTP=SMTP.gmail.com SMTP_port=587您需要对其进行配置,否则它将永远无法工作,下面是指向答案的直接链接,它会一步一步地解释:您还可以将php配置为将邮件中继到您的internet提供商,在某些情况下它会工作。我来自荷兰,我可以使用mail.chello.nl或其他东西作为我的smtp中继。使用gmail作为smtp服务器要求您配置sendmail也将无法工作…:-您使用的是windows还是linux发行版?设置SMTP仅适用于Windows。使用linux发行版时,需要使用sendmail_路径。如果这不起作用,您需要检查您在sendmail配置中定义的error.log/debug.log。
<form name="contactform" method="post" action="send_form_email.php">
  <table width="450px">

    <tr>
      <td valign="top">
        <label for="first_name">First Name *</label>
      </td>
      <td valign="top">
        <input  type="text" name="first_name" maxlength="50" size="30"></input>
      </td>
    </tr>

    <tr>
      <td valign="top">
        <label for="last_name">Last Name *</label>
      </td>
      <td valign="top">
        <input  type="text" name="last_name" maxlength="50" size="30"></input>
      </td>
    </tr>

    <tr>
      <td valign="top">
        <label for="email">Email Address *</label>
      </td>
      <td valign="top">
        <input  type="text" name="email" maxlength="80" size="30"></input>
      </td>
    </tr>

    <tr>
      <td valign="top">
        <label for="telephone">Telephone Number</label>
      </td>
      <td valign="top">
        <input  type="text" name="telephone" maxlength="30" size="30"></input>
      </td>
    </tr>

    <tr>
      <td valign="top">
        <label for="comments">Comments *</label>
      </td>
      <td valign="top">
        <textarea  name="comments" maxlength="1000" cols="25" rows="6"></textarea>
      </td>
    </tr>

    <tr>
      <td colspan="2" style="text-align:center">
        <input type="submit" value="Submit"> </input>
      </td>
    </tr>

  </table>
</form>
<?php
if (isset($_POST['email'])) {
    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "my@gmail.com";
    $email_subject = "ssss";

    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }

    // validation expected data exists
    if (!isset($_POST['first_name']) ||
        !isset($_POST['last_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['telephone']) ||
        !isset($_POST['comments'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');
    }

    $first_name = $_POST['first_name']; // required
    $last_name = $_POST['last_name']; // required
    $email_from = $_POST['email']; // required
    $telephone = $_POST['telephone']; // not required
    $comments = $_POST['comments']; // required
    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

  if (!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }

  $string_exp = "/^[A-Za-z .'-]+$/";
  if (!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }

  if (!preg_match($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }

  if (strlen($comments) < 2) {
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
  }

  if (strlen($error_message) > 0) {
    died($error_message);
  }

  $email_message = "Form details below.\n\n";

  function clean_string($string) {
    $bad = array("content-type","bcc:","to:","cc:","href");
    return str_replace($bad,"",$string);
  }

  $email_message .= "First Name: ".clean_string($first_name)."\n";
  $email_message .= "Last Name: ".clean_string($last_name)."\n";
  $email_message .= "Email: ".clean_string($email_from)."\n";
  $email_message .= "Telephone: ".clean_string($telephone)."\n";
  $email_message .= "Comments: ".clean_string($comments)."\n";

  // create email headers
  $headers = 'From: '.$email_from."\r\n".
  'Reply-To: '.$email_from."\r\n" .
  'X-Mailer: PHP/' . phpversion();

  // send the email
  mail($email_to, $email_subject, $email_message, $headers);
?>

<!-- include your own success html here -->

Thank you for contacting us. We will be in touch with you very soon.

<?php
}
?>