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

时间:2013-11-09 03:28:11

标签: 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);
}

2 个答案:

答案 0 :(得分:4)

您可以使用PHPMailerPHPMimeMail将邮件从localhost发送到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   = "mail@gmail.com";  // GMAIL username
$mail->Password   = "12345";            // GMAIL password

$mail->SetFrom('mail@gmail.com', 'Balaji K');

$mail->AddReplyTo("mail2@gmail.com");

$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 = "whoto@otherdomain.com";
$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!";
}

答案 1 :(得分:0)

民间。经过几个小时的尝试,我发现:您不需要其他工具,如邮件服务器或梨添加。这些只会提供您无法控制或洞察的其他潜在安全漏洞。要使邮件功能在localhost上运行,您需要做的就是更改php.ini文件。我刚检查了我的Outlook帐户设置并将其复制到php.ini文件中。所以SMTP服务器,端口,用户名和密码。

现在,您可能认为它无效但您应该知道,如果“发件人”字段的域名与收到电子邮件的实际域名不同,则许多邮件客户端会拒绝电子邮件。因此,如果php.ini文件包含例如smtp.ziggo.nl请确保标题包含:From:info@ziggo.nl

因此,为了在localhost和远程主机上创建通用代码,我检查是否存在我本地只有的文件(例如z_local)并相应地设置标头。如果本地文件不存在,我必须在远程ISP上并选择标题“From:info@remotesite.com”