从 localhost 测试 PHP 的邮件功能

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

Test PHP's mail function from localhost

phpemailtestinglocalhost

提问by FFish

I need to test a function that uses PHP's mail()
How can I do this without uploading the script to a server and test it online?
What's even more I am developing with no Internet connection at all.
I am on Mac OSX running localhost from XAMPP.

我需要测试一个使用 PHP 的 mail() 的函数,
如何在不将脚本上传到服务器并在线测试的情况下执行此操作?
更重要的是,我在完全没有 Internet 连接的情况下进行开发。
我在 Mac OSX 上从 XAMPP 运行 localhost。

回答by Daniel Egeberg

You don't have to install an MTA on your computer to test PHP's mail()function. On Unix based systems (Linux, *BSD, OS X, etc.) you can set sendmail_pathto something like tee mail.out > /dev/null. This will put the emails (including the headers) in a file called mail.out.

您不必在计算机上安装 MTA 来测试 PHP 的mail()功能。在基于 Unix 的系统(Linux、*BSD、OS X 等)上,您可以设置sendmail_path为类似tee mail.out > /dev/null. 这会将电子邮件(包括标题)放在一个名为mail.out.

Here is an example of how it would work:

以下是它如何工作的示例:

daniel@daniel-laptop:~$ cat | php -d sendmail_path='tee mail.out > /dev/null'
<?php
mail('[email protected]', 'the subject', 'the body');
?>
daniel@daniel-laptop:~$ cat mail.out
To: [email protected]
Subject: the subject
X-PHP-Originating-Script: 1000:-


the body

You can set sendmail_pathin your php.inifile. If you want to append emails to the file instead of overwriting each time, you can use tee -ainstead of just tee.

你可以sendmail_path在你的php.ini文件中设置。如果您想将电子邮件附加到文件而不是每次都覆盖,您可以使用tee -a而不仅仅是tee.

回答by Xavier John

To test sending email from apache do the following

要测试从 apache 发送电子邮件,请执行以下操作

create a folder to store the email.

创建一个文件夹来存储电子邮件。

/home/username/Documents/TestEmails

Give permission to apache. From the Documents folder, run

授予 apache 权限。从 Documents 文件夹中,运行

sudo chgrp -R www-data TestEmails

Modify the php.ini file, mine is located at

修改php.ini文件,我的位于

/etc/php5/apache2/php.ini

set sendmail_path

设置 sendmail_path

sendmail_path ='cat > /home/username/Documents/TestEmails/mail.txt'

Restart apace2

快速重启2

sudo service apache2 restart

回答by James

Hmm. I haven't tried this, but in php.ini you can set "sendmail_path" ... so in theory you could write your own shell script that simply wrote the input into text files, and change your php.ini to use that? Then simply run tests and check text files!

唔。我没有试过这个,但是在 php.ini 中你可以设置“sendmail_path”......所以理论上你可以编写自己的 shell 脚本,将输入简单地写入文本文件,然后更改你的 php.ini 以使用它?然后只需运行测试并检查文本文件!

回答by trapper_hag

If you're on Windows/using something like WAMP/UWAMP/XAMPP and need to test mail then Papercut is well worth a look:

如果您使用的是 Windows/使用 WAMP/UWAMP/XAMPP 之类的东西并且需要测试邮件,那么 Papercut 非常值得一看:

https://github.com/ChangemakerStudios/Papercut

https://github.com/ChangemakerStudios/Papercut

You can leave your SMTP settings in php.ini as the defaults (localhost/25) and it just works. It looks like an email client and shows all the parts of/details of a message in separate sections.

您可以将 php.ini 中的 SMTP 设置保留为默认值 (localhost/25),它就可以正常工作。它看起来像一个电子邮件客户端,并在单独的部分中显示消息的所有部分/详细信息。

回答by chimeraha

Building on the answer @Daniel-Egeberg provided, this is what worked for me on Ubuntu 18.04:

基于@Daniel-Egeberg 提供的答案,这在 Ubuntu 18.04 上对我有用:

I opened /etc/php/7.2/apache2/php.ini and set:

我打开 /etc/php/7.2/apache2/php.ini 并设置:

sendmail_path='tee /path/to/file/mail.out'

sendmail_path='tee /path/to/file/mail.out'

restarted:

重新启动:

sudo service apache2 restart

须藤服务 apache2 重启

then created /path/to/file/mail.out and changed permissions for it:

然后创建 /path/to/file/mail.out 并更改其权限:

chmod 666 /path/to/file/mail.out

chmod 666 /path/to/file/mail.out

回答by Neel Basu

Setup a pop3 server in local Machine. Many available for free. and send mails within your local domain using sendmail.

在本地机器上设置一个 pop3 服务器。许多免费提供。并使用 sendmail 在您的本地域内发送邮件。

By default its not required to set the sendmail path in Linux. at least I never needed it. just use the mail() function and hit mails on local domain

默认情况下,它不需要在 Linux 中设置 sendmail 路径。至少我从来不需要它。只需使用 mail() 函数并点击本地域上的邮件