php 如何配置PHP发送电子邮件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17533863/
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 16:03:15  来源:igfitidea点击:

How to configure PHP to send e-mail?

phpemailsmtp

提问by abcdefgh

I need to send mail to the users of my website using php script. I have tried using mail function in php.
My code is as follows:

我需要使用 php 脚本向我网站的用户发送邮件。我曾尝试在 php 中使用邮件功能。
我的代码如下:

  $to = "[email protected]";
  $subject = "Test mail";
  $message = "My message";
  $from = "[email protected]";
  $headers = "From:" . $from;
  mail($to,$subject,$message,$headers);

When I try running the program this is what I get:

当我尝试运行程序时,这就是我得到的:

 Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set().

Please tell me what address to include in the $from variable. Do I need a smtp server for this? How do I send mails using a localhost? Please tell me what exactly to edit in the php.ini file

请告诉我要包含在 $from 变量中的地址。我需要一个 smtp 服务器吗?如何使用本地主机发送邮件?请告诉我在 php.ini 文件中究竟要编辑什么

I am new to all this.. Please help me..

我对这一切都很陌生..请帮帮我..

回答by pooria

Use PHPMailer instead: https://github.com/PHPMailer/PHPMailer

改用 PHPMailer:https: //github.com/PHPMailer/PHPMailer

How to use it:

如何使用它:

require('./PHPMailer/class.phpmailer.php');
$mail=new PHPMailer();
$mail->CharSet = 'UTF-8';

$body = 'This is the message';

$mail->IsSMTP();
$mail->Host       = 'smtp.gmail.com';

$mail->SMTPSecure = 'tls';
$mail->Port       = 587;
$mail->SMTPDebug  = 1;
$mail->SMTPAuth   = true;

$mail->Username   = '[email protected]';
$mail->Password   = '123!@#';

$mail->SetFrom('[email protected]', $name);
$mail->AddReplyTo('[email protected]','no-reply');
$mail->Subject    = 'subject';
$mail->MsgHTML($body);

$mail->AddAddress('[email protected]', 'title1');
$mail->AddAddress('[email protected]', 'title2'); /* ... */

$mail->AddAttachment($fileName);
$mail->send();

回答by pooria

You need to have a smtp service setup in your local machine in order to send emails. There are many available freely just search on google.

您需要在本地计算机中设置 smtp 服务才能发送电子邮件。有很多可以免费使用,只需在谷歌上搜索即可。

If you own a server or VPS upload the script and it will work fine.

如果您拥有服务器或 VPS,请上传脚本,它会正常工作。

回答by neves

You won't be able to send a message through other people mail servers. Check with your host provider how to send emails. Try to send an email from your server without PHP, you can use any email client like Outook. Just after it works, try to configure PHP.ini with your email client SMTP (sending e-mail) configuration.

您将无法通过其他人的邮件服务器发送消息。请咨询您的主机提供商如何发送电子邮件。尝试在没有 PHP 的情况下从您的服务器发送电子邮件,您可以使用任何电子邮件客户端,例如 Outook。在它工作之后,尝试使用您的电子邮件客户端 SMTP(发送电子邮件)配置来配置 PHP.ini。

回答by achraf akrouti

configure your php.ini like this

像这样配置你的 php.ini

SMTP = smtp.gmail.com

[mail function]
; XAMPP: Comment out this if you want to work with an SMTP Server like Mercury

; SMTP = smtp.gmail.com

; smtp_port = 465

; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = postmaster@localhost

回答by Kris M

This will not work on a local host, but uploaded on a server, this code should do the trick. Just make sure to enter your own email address for the $to line.

这在本地主机上不起作用,但上传到服务器上,此代码应该可以解决问题。只需确保为 $to 行输入您自己的电子邮件地址。

<?php
if (isset($_POST['name']) && isset($_POST['email'])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $to = '[email protected]';
    $subject = "New Message on YourWebsite.com";
    $body = '<html>
                <body>
                    <h2>Title</h2>
                    <br>
                    <p>Name:<br>'.$name.'</p>
                    <p>Email:<br>'.$email.'</p>

                </body>
            </html>';

//headers
$headers = "From: ".$name." <".$email.">\r\n";
$headers = "Reply-To: ".$email."\r\n";
$headers = "MIME-Version: 1.0\r\n";
$headers = "Content-type: text/html; charset=utf-8";

//send
$send = mail($to, $subject, $body, $headers);
if ($send) {
    echo '<br>';
    echo "Success. Thanks for Your Message.";
} else {
    echo 'Error.';
}
}
?>

<html>
    <head>
        <meta charset="utf-8">
    </head>
    <body>
        <form action="" method="post">
            <input type="text" name="name" placeholder="Your Name"><br>
            <input type="text" name="email" placeholder="Your Email"><br>
            <button type="submit">Subscribe</button>
        </form>
    </body>
</html>

回答by Mani Kandan

Here's the link that gives me the answer and we use gmail:

这是给我答案的链接,我们使用 gmail:

Installthe "fake sendmail for windows". If you are not using XAMPP you can download it here: http://glob.com.au/sendmail/sendmail.zip

安装“fake sendmail for windows”。如果您不使用 XAMPP,您可以在此处下载:http: //glob.com.au/sendmail/sendmail.zip

Modifythe php.ini file to use it (commented out the other lines):

修改php.ini 文件以使用它(注释掉其他行):

mail function

邮件功能

For Win32 only.

仅适用于 Win32。

SMTP = smtp.gmail.com
smtp_port = 25
For Win32 only.
sendmail_from = <e-mail username>@gmail.com

For Unix only.

仅适用于 Unix。

You may supply arguments as well (default: sendmail -t -i).

您也可以提供参数(默认值:)sendmail -t -i

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

(ignore the "Unix only" bit, since we actually are using sendmail)

(忽略“仅限 Unix”位,因为我们实际上使用的是 sendmail)

You then have to configure the "sendmail.ini" file in the directory where sendmail was installed:

然后,您必须在安装 sendmail 的目录中配置“sendmail.ini”文件:

sendmail

发邮件

smtp_server=smtp.gmail.com
smtp_port=25
error_logfile=error.log
debug_logfile=debug.log
auth_username=<username>
auth_password=<password>
force_sender=<e-mail username>@gmail.com

回答by Marcovecchio

To fix this, you must review your PHP.INI, and the mail services setup you have in your server.

要解决这个问题,您必须检查您的 PHP.INI,以及您在服务器中的邮件服务设置。

But my best advice for you is to forget about the mail()function. It depends on PHP.INI settings, it's configuration is different depending on the platform (Linux or Windows), and it can't handle SMTP authentication, which is a big trouble in current days. Too much headache.

但我对你最好的建议是忘记这个mail()功能。它取决于 PHP.INI 设置,它的配置因平台(Linux 或 Windows)而异,并且无法处理 SMTP 身份验证,这在当今是一个大问题。太头疼了。

Use "PHP Mailer" instead (https://github.com/PHPMailer/PHPMailer), it's a PHP class available for free, and it can handle almost any SMTP server, internal or external, with or without authentication, it works exactly the same way on Linux and Windows, and it won't depend on PHP.INI settings. It comes with many examples, it's very powerful and easy to use.

改用“PHP Mailer”(https://github.com/PHPMailer/PHPMailer),它是一个免费提供的 PHP 类,它几乎可以处理任何内部或外部 SMTP 服务器,无论是否经过身份验证,它的工作原理完全相同在 Linux 和 Windows 上也是如此,它不会依赖于 PHP.INI 设置。它带有许多示例,非常强大且易于使用。

回答by symcbean

Usually a good place to start when you run into problems is the manual. The page on configuring emailexplains that there's a big difference between the PHP mail command running on MSWindows and on every other operating system; it's a good idea when posting a question to provide relevant information on how that part of your system is configured and what operating system it is running on.

通常,当您遇到问题时,一个好的起点是手册。有关配置电子邮件的页面解释说,在 MSWindows 上运行的 PHP 邮件命令与其他所有操作系统上运行的 PHP 邮件命令之间存在很大差异;在发布问题时提供有关系统的该部分如何配置以及它在什么操作系统上运行的相关信息是一个好主意。

Your PHP is configured to talk to an SMTP server - the default for an MSWindows machine, but either you have no MTA installed or it's blocking connections. While for a commercial website running your own MTA robably comes quite high on the list of things to do, it is not a trivial exercise - you really need to know what you're doing to get one configured and running securely. It would make a lot more sense in your case to use a service configured and managed by someone else.

您的 PHP 被配置为与 SMTP 服务器通信——这是 MSWindows 机器的默认设置,但是您没有安装 MTA 或者它阻止了连接。虽然对于运行您自己的 MTA 的商业网站来说,可能会在要做的事情列表中占据相当重要的位置,但这并不是一项微不足道的练习 - 您确实需要知道自己在做什么才能安全地配置和运行。在您的情况下,使用由其他人配置和管理的服务会更有意义。

Since you'll be connecting to a remote MTA using a gmail address, then you should probably use Gmail's server; you will need SMTP authenticaton and probably SSL support - neither of which are supported by the mail() function in PHP. There's a simple example here using swiftmailer with gmailor here's an example using phpmailer

由于您将使用 gmail 地址连接到远程 MTA,那么您可能应该使用 Gmail 的服务器;您将需要 SMTP 身份验证和可能的 SSL 支持 - PHP 中的 mail() 函数不支持这两者。这里有一个使用swiftmailer 和 gmail的简单示例或者这里有一个使用 phpmailer示例