繁体   English   中英

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

[英]Sending mail via php script using wamp 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   = "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!";
}

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

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

因此,为了在本地主机和远程主机上都制作通用代码,我检查了仅本地文件(例如z_local)的存在,并相应地设置了标头。 如果本地文件不存在,则必须位于远程ISP上,然后选择标题“发件人:info@remotesite.com”

暂无
暂无

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

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