简体   繁体   English

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

[英]Sending mail via php script using wamp server

I am using WAMP server 2.4. 我正在使用WAMP服务器2.4。 I want to send a mail to my gmail account. 我想发送邮件到我的Gmail帐户。 The code I used for the same is given below. 我用于相同的代码如下。 I read some forums and they suggested me to make changes in php.ini file.But it is not a permanent solution.Also some suggested that mail() does not work for gmail ? 我读过一些论坛,他们建议我在php.ini文件中进行更改。但这不是永久解决方案。也有人建议mail()对gmail不起作用? Is there some solution to this? 有解决办法吗?

       <?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);
}

You can use PHPMailer or PHPMimeMail to send mail from your localhost to gmail. 您可以使用PHPMailerPHPMimeMail将邮件从您的本地主机发送到gmail。 To send email to gmail, you should send authenticated mail like SMTP mail. 要将电子邮件发送到gmail,您应该发送经过身份验证的邮件,例如SMTP邮件。 You should configure your mail username,password, and your mail host in your mail configurtion. 您应该在邮件配置中配置邮件用户名,密码和邮件主机。

sample PHPmailer script for Gmail : 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!";
}

Folks. 乡亲。 After many hours of trying I found that: You don't need additional tools like mailservers or pear additions. 经过数小时的尝试,我发现:您不需要其他工具,例如邮件服务器或添加的pear。 These only provide additional potential security leaks that you have no control or insight over. 这些仅提供您无法控制或了解的其他潜在安全漏洞。 To get mail function to work on localhost, all you need to do is change the php.ini file. 要使邮件功能在localhost上运行,您需要做的就是更改php.ini文件。 I just checked my outlook account settings and copied those to the php.ini file. 我只是检查了我的Outlook帐户设置并将其复制到php.ini文件中。 So SMTP server, port, username and password. 因此,SMTP服务器,端口,用户名和密码。

Now, You may think it does not work but You should know that many mail clients reject emails if the From field has a different domain than the actual domain an email is received from. 现在,您可能认为它不起作用,但是您应该知道,如果“发件人”字段的域与接收电子邮件的实际域不同,则许多邮件客户端会拒绝电子邮件。 So if the php.ini file contains for example smtp.ziggo.nl Be sure that the header contains: From: info@ziggo.nl 因此,如果php.ini文件包含例如smtp.ziggo.nl,请确保标头包含:发件人:info@ziggo.nl

So for making universal code on both localhost and remote host, I check for the existence of a file that I only have locally (eg z_local) and set the headers accordingly. 因此,为了在本地主机和远程主机上都制作通用代码,我检查了仅本地文件(例如z_local)的存在,并相应地设置了标头。 If the local file does not exist I must be on the remote ISP and choose the header "From: info@remotesite.com" 如果本地文件不存在,则必须位于远程ISP上,然后选择标题“发件人:info@remotesite.com”

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

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