【问题标题】:Sending mail via php script using wamp server使用 wamp 服务器通过 php 脚本发送邮件
【发布时间】:2013-11-21 05:29:41
【问题描述】:

我正在使用 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);
}

【问题讨论】:

    标签: php email wamp mail-server


    【解决方案1】:

    您可以使用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   = "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!";
    }
    

    【讨论】:

      【解决方案2】:

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

      现在,您可能认为它不起作用,但您应该知道,如果“发件人”字段的域与接收电子邮件的实际域不同,则许多邮件客户端会拒绝电子邮件。所以如果 php.ini 文件包含例如 smtp.ziggo.nl 确保头部包含: From: info@ziggo.nl

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-19
        • 2012-11-19
        • 1970-01-01
        • 2015-01-06
        相关资源
        最近更新 更多