简体   繁体   English

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

[英]How to configure PHP to send e-mail?

I need to send mail to the users of my website using php script.我需要使用 php 脚本向我网站的用户发送邮件。 I have tried using mail function in php.我曾尝试在 php 中使用邮件功能。
My code is as follows:我的代码如下:

  $to = "myweb@gmail.com";
  $subject = "Test mail";
  $message = "My message";
  $from = "webp@gmail.com";
  $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.请告诉我要包含在 $from 变量中的地址。 Do I need a smtp server for this?我需要一个 smtp 服务器吗? How do I send mails using a localhost?如何使用本地主机发送邮件? Please tell me what exactly to edit in the php.ini file请告诉我在 php.ini 文件中究竟要编辑什么

I am new to all this.. Please help me..我对这一切都很陌生..请帮帮我..

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   = 'me.sender@gmail.com';
$mail->Password   = '123!@#';

$mail->SetFrom('me.sender@gmail.com', $name);
$mail->AddReplyTo('no-reply@mycomp.com','no-reply');
$mail->Subject    = 'subject';
$mail->MsgHTML($body);

$mail->AddAddress('abc1@gmail.com', 'title1');
$mail->AddAddress('abc2@gmail.com', 'title2'); /* ... */

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

You need to have a smtp service setup in your local machine in order to send emails.您需要在本地计算机中设置 smtp 服务才能发送电子邮件。 There are many available freely just search on google.有很多可以免费使用,只需在谷歌上搜索即可。

If you own a server or VPS upload the script and it will work fine.如果您拥有服务器或 VPS,请上传脚本,它会正常工作。

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.尝试在没有 PHP 的情况下从您的服务器发送电子邮件,您可以使用任何电子邮件客户端,例如 Outook。 Just after it works, try to configure PHP.ini with your email client SMTP (sending e-mail) configuration.在它工作之后,尝试使用您的电子邮件客户端 SMTP(发送电子邮件)配置来配置 PHP.ini。

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.但我对你最好的建议是忘记mail()函数。 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.它取决于 PHP.INI 设置,它的配置因平台(Linux 或 Windows)而异,并且无法处理 SMTP 身份验证,这在当今是一个大问题。 Too much headache.太头疼了。

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.使用“PHP Mailer”代替( https://github.com/PHPMailer/PHPMailer ),它是一个免费提供的 PHP 类,它可以处理几乎任何内部或外部的 SMTP 服务器,无论是否经过身份验证,它的工作原理完全相同在 Linux 和 Windows 上也是如此,它不会依赖于 PHP.INI 设置。 It comes with many examples, it's very powerful and easy to use.它带有许多示例,非常强大且易于使用。

Here's the link that gives me the answer and we use gmail:这是给我答案的链接,我们使用 gmail:

Install the "fake sendmail for windows".安装“fake sendmail for windows”。 If you are not using XAMPP you can download it here: http://glob.com.au/sendmail/sendmail.zip如果您不使用 XAMPP,您可以在此处下载: http : //glob.com.au/sendmail/sendmail.zip

Modify the 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

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

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 = 'your.email@address.com';
    $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>

Usually a good place to start when you run into problems is the manual .通常,当您遇到问题时,一个好的起点是手册 The page on configuring email explains that there's a big difference between the PHP mail command running on MSWindows and on every other operating system;有关配置电子邮件的页面解释说,在 MSWindows 上运行的 PHP 邮件命令与其他所有操作系统上运行的 PHP 邮件命令之间存在很大差异; 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.在发布问题时提供有关系统的该部分如何配置以及它在什么操作系统上运行的相关信息是一个好主意。

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.您的 PHP 被配置为与 SMTP 服务器通信——这是 MSWindows 机器的默认设置,但是您没有安装 MTA 或者它阻止了连接。 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.虽然对于运行您自己的 MTA 的商业网站来说,可能会在要做的事情列表中占据相当重要的位置,但这并不是一项微不足道的练习 - 您确实需要知道自己在做什么才能安全地配置和运行。 It would make a lot more sense in your case to use a service configured and managed by someone else.在您的情况下,使用由其他人配置和管理的服务会更有意义。

Since you'll be connecting to a remote MTA using a gmail address, then you should probably use Gmail's server;由于您将使用 gmail 地址连接到远程 MTA,那么您可能应该使用 Gmail 的服务器; you will need SMTP authenticaton and probably SSL support - neither of which are supported by the mail() function in PHP.您将需要 SMTP 身份验证和可能的 SSL 支持 - PHP 中的 mail() 函数不支持这两者。 There's a simple example here using swiftmailer with gmail or here's an example using phpmailer这里有一个使用swiftmailer 和 gmail的简单示例或者这里有一个使用 phpmailer示例

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

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