php - 使用wamp服务器通过php脚本发送邮件

标签 php email wamp mail-server

我使用的是 WAMP 服务器 2.4。我想向我的 Gmail 帐户发送一封邮件。下面给出了我用于相同目的的代码。我阅读了一些论坛,他们建议我在 php.ini 文件中进行更改。但这不是永久的解决方案。还有一些人建议 mail() 不适用于 gmail ?有什么解决办法吗?

       <?php
      //Checking if entries are ok 
      if(isset($_POST['submit']))
      {

         if(isset($_POST['username']))
         $id = $_POST['username'];

         else 
         echo "Cannot be blank";

         //ensuring mail goes to registered user
         $query="SELECT * FROM table1 WHERE id = '$id' ";
         $result= mysql_query($query,$con);


         if (!($result) )
         {
            die('Error: ' . mysql_error($con));
         }

         else
         {

              $values = mysql_fetch_array($result);
         }

       // Sending mail...
         if(mysql_num_rows($result)== 1)
         {
          if(isset($_POST['email']))
          $to= $_POST['email'];
              else
              echo "invalid email";
              $msg = 'Name :' .$values['name'] ."\n"
                    .'Id:' .$values['id']."\n"
                    .'Email:' .$values['email']."\n"
          ."Your password is:" . $values[password];

            mail($to,"Forget your Password",$msg);
            header('location: sent_mail.php');


    }
        else 
        echo "Verify your username again";
}
    else{
    echo "sending failed";
    header('location: forget_password.php');
    exit(0);
}

最佳答案

您可以使用PHPMailerPHPMimeMail从本地主机发送邮件到 gmail。要将电子邮件发送到 gmail,您应该像 SMTP 邮件一样发送经过身份验证的邮件。您应该在邮件配置中配置您的邮件用户名、密码和邮件主机。

Gmail 的示例 PHPmailer 脚本:

require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

$mail             = new PHPMailer();

$body             = file_get_contents('contents.html');
$body             = preg_replace('/[\]/','',$body);

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
                                           // 1 = errors and messages
                                           // 2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 465;                   // set the SMTP port for the GMAIL server
$mail->Username   = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f29f939b9eb2959f939b9edc919d9f" rel="noreferrer noopener nofollow">[email protected]</a>";  // GMAIL username
$mail->Password   = "12345";            // GMAIL password

$mail->SetFrom('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a1ccc0c8cde1c6ccc0c8cd8fc2cecc" rel="noreferrer noopener nofollow">[email protected]</a>', 'Balaji K');

$mail->AddReplyTo("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="96fbf7fffaa4d6f1fbf7fffab8f5f9fb" rel="noreferrer noopener nofollow">[email protected]</a>");

$mail->Subject    = "PHPMailer Test Subject via smtp (Gmail), basic";

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);
$mail->Body($body);
$address = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4b3c23243f240b243f232e392f24262a222565282426" rel="noreferrer noopener nofollow">[email protected]</a>";
$mail->AddAddress($address, "John Doe");

$mail->AddAttachment("images/phpmailer.gif");      // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}

关于php - 使用wamp服务器通过php脚本发送邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19872034/

相关文章:

php - 无法在 php 中连接表和获取数组

javascript - 将数据发送到php(form method=post),然后发送回javascript

sql - 向 UTL_MAIL.SEND 提供凭据以绕过 ORA-29278

rest - 将呈现的电子邮件发送到 SendGrid 的最快方法

php - 使用 symfony2 但不使用 PHP 脚本的 ERR_CONNECTION_RESET

PHP MySQL 使用 array() 打印每月数据不起作用

php - 按值从 mysql_query 结果过滤数组

ruby-on-rails-3 - 在邮件主题中添加 TM 符号

wamp - 本地主机文件夹的别名

c# - 无法从 Visual Studio C# 连接到数据库