PHP:在本地主机发送邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15267423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:55:54  来源:igfitidea点击:

PHP : send mail in localhost

phpemail

提问by Henri

I would like to send email through php code hosted locally.

我想通过本地托管的 php 代码发送电子邮件。

<?php 
$email  = "[email protected]"; 
$titre   = "My subject"; 
$message = "Text message !"; 
mail($email, $titre, $message); 
?>

When I run this code, I get the following error :

当我运行此代码时,出现以下错误:

Warning: mail() [<a href='function.mail'>function.mail</a>]: Failed to connect to mailserver at &quot;localhost&quot; port 25, verify your &quot;SMTP&quot; and &quot;smtp_port&quot; setting in php.ini or use ini_set() in C:\...

I went into the php.inifile and it seems to be already well configured.

我进入了php.ini文件,它似乎已经配置好了。

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25

How can I fix this ?

我怎样才能解决这个问题 ?

Thank you

谢谢

采纳答案by Quentin

It is configured to use localhost:25for the mail server.

它被配置为localhost:25用于邮件服务器。

The error message says that it can't connect to localhost:25.

错误消息说它无法连接到localhost:25.

Therefore you have two options:

因此,您有两个选择:

  1. Install / Properly configure an SMTP server on localhost port 25
  2. Change the configuration to point to some other SMTP server that you canconnect to
  1. 在本地主机端口 25 上安装/正确配置 SMTP 服务器
  2. 将配置更改为指向您可以连接的其他一些 SMTP 服务器

回答by Menno

You will need to install a local mailserver in order to do this. If you want to send it to external e-mail addresses, it might end up in unwanted e-mails or it may not arrive at all.

您需要安装本地邮件服务器才能执行此操作。如果您想将其发送到外部电子邮件地址,它可能会以不需要的电子邮件告终,或者可能根本不会到达。

A good mailserver which I use (I use it on Linux, but it's also available for Windows) is Axigen: http://www.axigen.com/mail-server/download/

我使用的一个很好的邮件服务器(我在 Linux 上使用它,但它也可用于 Windows)是 Axigen:http://www.axigen.com/mail-server/download/

You might need some experience with mailservers to install it, but once it works, you can do anything you want with it.

您可能需要一些邮件服务器方面的经验来安装它,但是一旦它工作起来,您就可以对它做任何想做的事情。

回答by user2684198

I spent hours on this. I used to not get errors but mails were never sent. Finally I found a solution and I would like to share it.

我花了几个小时在这上面。我过去没有收到错误,但从未发送过邮件。最后我找到了一个解决方案,我想分享一下。

<?php
include 'nav.php';
/*
    Download PhpMailer from the following link:
    https://github.com/Synchro/PHPMailer (CLick on Download zip on the right side)
    Extract the PHPMailer-master folder into your xampp->htdocs folder
    Make changes in the following code and its done :-)

    You will receive the mail with the name Root User.
    To change the name, go to class.phpmailer.php file in your PHPMailer-master folder,
    And change the name here: 
    public $FromName = 'Root User';
*/
require("PHPMailer-master/PHPMailerAutoload.php"); //or select the proper destination for this file if your page is in some   //other folder
ini_set("SMTP","ssl://smtp.gmail.com"); 
ini_set("smtp_port","465"); //No further need to edit your configuration files.
$mail = new PHPMailer();
$mail->SMTPAuth = true;
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->SMTPSecure = "ssl";
$mail->Username = "[email protected]"; //account with which you want to send mail. Or use this account. i dont care :-P
$mail->Password = "trials.php.php"; //this account's password.
$mail->Port = "465";
$mail->isSMTP();  // telling the class to use SMTP
$rec1="[email protected]"; //receiver. email addresses to which u want to send the mail.
$mail->AddAddress($rec1);
$mail->Subject  = "Eventbook";
$mail->Body     = "Hello hi, testing";
$mail->WordWrap = 200;
if(!$mail->Send()) {
echo 'Message was not sent!.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo  //Fill in the document.location thing
'<script type="text/javascript">
                        if(confirm("Your mail has been sent"))
                        document.location = "/";
        </script>';
}
?>

回答by Nisanth Kumar

try this

尝试这个

ini_set("SMTP","aspmx.l.google.com");
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers .= "From: [email protected]" . "\r\n";
mail("[email protected]","test subject","test body",$headers);