【问题标题】:How to configure xampp to send email如何配置xampp发送电子邮件
【发布时间】:2015-08-11 17:43:36
【问题描述】:

我知道这在网站的其他地方,但由于某种原因,我在这里找到的答案仍然没有帮助我在尝试了一天不同的事情之后仍然无法让它工作。我的目标是向用户输入的电子邮件发送确认码。我只知道少量的 PHP,并遵循了登录/注册系统的教程,但我相当确定 php 工作正常,问题出在我的 sendmail.ini 和 php.ini 上。这是我的 sendmail.ini

; configuration for fake sendmail

; if this file doesn't exist, sendmail.exe will look for the settings in
; the registry, under HKLM\Software\Sendmail

[sendmail]

; you must change mail.mydomain.com to your smtp server,
; or to IIS's "pickup" directory.  (generally C:\Inetpub\mailroot\Pickup)
; emails delivered via IIS's pickup directory cause sendmail to
; run quicker, but you won't get error messages back to the calling
; application.

smtp_server=smtp.gmail.com

; smtp port (normally 25)

smtp_port=587

; SMTPS (SSL) support
;   auto = use SSL for port 465, otherwise try to use TLS
;   ssl  = alway use SSL
;   tls  = always use TLS
;   none = never try to use SSL

smtp_ssl=auto

; the default domain for this server will be read from the registry
; this will be appended to email addresses when one isn't provided
; if you want to override the value in the registry, uncomment and modify

;default_domain=mydomain.com

; log smtp errors to error.log (defaults to same directory as sendmail.exe)
; uncomment to enable logging

error_logfile=error.log

; create debug log as debug.log (defaults to same directory as sendmail.exe)
; uncomment to enable debugging

debug_logfile=debug.log

; if your smtp server requires authentication, modify the following two lines

auth_username= myemail@gmail.com
auth_password= mygmailpass

; if your smtp server uses pop3 before smtp authentication, modify the 
; following three lines.  do not enable unless it is required.

pop3_server=
pop3_username=
pop3_password=

; force the sender to always be the following email address
; this will only affect the "MAIL FROM" command, it won't modify 
; the "From: " header of the message content

force_sender=myemail@gmail.com

; force the sender to always be the following email address
; this will only affect the "RCTP TO" command, it won't modify 
; the "To: " header of the message content

force_recipient=

; sendmail will use your hostname and your default_domain in the ehlo/helo
; smtp greeting.  you can manually set the ehlo/helo name if required

hostname=

还有我在 php.ini 中编辑的部分

extension=php_openssl.dll 

[mail function]
; XAMPP: Comment out this if you want to work with an SMTP Server like Mercury
SMTP =smtp.gmail.com
smtp_port =587

; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = myemail@gmail.com


sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"

;sendmail_path="C:\xampp\mailtodisk\mailtodisk.exe"

; Force the addition of the specified parameters to be passed as extra parameters
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail(), even in safe mode.
;mail.force_extra_parameters =

还有我的 PHP 代码,以备不时之需

<?php
session_start();
include('configdb.php');
if(isset($_POST['submit']))
{
 //whether the username is blank
 if($_POST['username'] == '')
 {
  $_SESSION['error']['username'] = "User Name is required.";
 }
 //whether the email is blank
 if($_POST['email'] == '')
 {
  $_SESSION['error']['email'] = "E-mail is required.";
 }
 else
 {
  //whether the email format is correct
  if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9._-]+)+$/", $_POST['email']))
  {
   //if it has the correct format whether the email has already exist
   $email= $_POST['email'];
   $sql1 = "SELECT * FROM user WHERE email = '$email'";
   $result1 = mysqli_query($mysqli,$sql1) or die(mysqli_error());
   if (mysqli_num_rows($result1) > 0)
            {
    $_SESSION['error']['email'] = "This Email is already used.";
   }
  }
  else
  {
   //this error will set if the email format is not correct
   $_SESSION['error']['email'] = "Your email is not valid.";
  }
 }
 //whether the password is blank
 if($_POST['password'] == '')
 {
  $_SESSION['error']['password'] = "Password is required.";
 }
 //if the error exist, we will go to registration form
 if(isset($_SESSION['error']))
 {
  header("Location: index.php");
  exit;
 }
 else
 {
  $username = $_POST['username'];
  $email = $_POST['email'];
  $password = $_POST['password'];
  $com_code = md5(uniqid(rand()));

  $sql2 = "INSERT INTO user (username, email, password, com_code) VALUES ('$username', '$email', '$password', '$com_code')";
  $result2 = mysqli_query($mysqli,$sql2) or die(mysqli_error());

  if($result2)
  {
   $to = $email;
   $subject = "Confirmation for $username";
   $header = "Confirmation from";
   $message = "Please click the link below to verify and activate your account. rn";
   $message .= "http://www.yourname.com/confirm.php?passkey=$com_code";

   $sentmail = mail($to,$subject,$message,$header);

   if($sentmail)
            {
   echo "Your confirmation link has been sent to your e-mail address.";
   }
   else
         {
    echo "Error while sending confirmation link to your e-mail address";
   }
  }
 }
}
?>

就像我说的那样,我已经研究了一段时间,但似乎无法弄清楚,我认为这很简单,但由于我仍在学习,所以我没有看到它。感谢大家的帮助!

【问题讨论】:

  • 究竟是什么不能使用所有这些代码?邮件是否发送到内部邮件服务器,但没有传输到下一个负责的邮件服务器?

标签: php xampp


【解决方案1】:

您只需配置 C:\xampp\php\php.ini 和 c:\xampp\sendmail\sendmail.ini 即可让 Gmail 发送邮件。

在 php.ini 文件中找到 [mail function] 并更改如下

SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = your-gmail-id@gmail.com
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"

现在打开 C:\xampp\sendmail\sendmail.ini。将 sendmail.ini 中的所有现有代码替换为以下代码

[sendmail]
    
smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=your-gmail-id@gmail.com
auth_password=your-gmail-id-password
force_sender=your-gmail-id@gmail.com

现在你已经完成了!创建一个带有邮件功能的 PHP 文件并从 localhost 发送邮件。

注意:- 将您的电子邮件 ID 替换为“your-gmail-id@gmail.com”

【讨论】:

  • 是的,这对我有用。谢谢@sumit 我已经拯救了我的一天。
【解决方案2】:

如果你是通过本地服务器发送邮件,你需要配置

c:\xampp\sendmail\sendmail.ini(通过 Gmail 发送)和C:\xampp\php\php.ini

php.ini

找到extension=php_openssl.dll 并从该行的开头删除分号。所以这允许你从本地主机发送邮件。

然后找到mail function并更改以下设置

SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = my-gmail-id@gmail.com
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"

sendmail.ini(路径 - C:\xampp\sendmail\sendmail.ini

改变这个

[sendmail]    
smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=GMail ID@gmail.com//your gmail ID
auth_password=GMail PASSWORD // Your gmail password
force_sender=GMail ID@gmail.com //your gmail ID

更改上述代码中的GMail ID@gmail.comGMail PASSWORDGMail ID@gmail.com

终于

如果 php.ini 文件中还有另一个 sendmail_path : sendmail_path="C:\xampp\mailtodisk\mailtodisk.exe",请在下面的行中添加注释。

【讨论】:

  • 感谢您的回复!但由于某种原因它仍然无法正常工作:/。用户被添加到我拥有的数据库中,但我仍然从我的 php 代码中得到 else 语句,上面写着“向您的电子邮件地址发送确认链接时出错”并且电子邮件没有发送并且它不在我的垃圾邮件中文件夹。
  • 缺少一条说明:Google 现在默认阻止这种连接,要解除阻止,您需要进入 GMail 的安全设置并启用“不太安全”的连接。以下是来自 Google 的说明:support.google.com/accounts/answer/6010255?hl=en
猜你喜欢
  • 2013-11-21
  • 2017-01-25
  • 1970-01-01
  • 2018-03-13
  • 2013-07-06
  • 2013-03-13
  • 2016-07-29
  • 2013-05-05
  • 2017-02-22
相关资源
最近更新 更多