php php邮件的smtp配置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3525818/
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
smtp configuration for php mail
提问by shin
I am sending mails from my website by using php mail function. But now it is not working and I contacted our hosting team then they told me to use smtp as they did some changes in server. I don't know how to do it. Current code (with php mail function) is as follows, can anyone help me about the changes which I have to do with this.
我正在使用 php 邮件功能从我的网站发送邮件。但是现在它不起作用,我联系了我们的托管团队,然后他们告诉我使用 smtp,因为他们对服务器进行了一些更改。我不知道该怎么做。当前代码(带有 php 邮件功能)如下,任何人都可以帮助我了解与此相关的更改。
<?php
$mail_To="[email protected]";
$headers = "";
$headers .= "From: [email protected]\n";
$headers .= "Reply-To: [email protected]\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "X-Mailer: php";
$mail_Subject = " Live TV key";
$mail_Body = "<p>Muscle-tube</p>";
mail($mail_To, $mail_Subject, $mail_Body,$headers);
?>
回答by JCD
PHP's mail()
function does not have support for SMTP. You're going to need to use something like the PEAR Mail package.
PHP 的mail()
功能不支持 SMTP。您将需要使用类似PEAR Mail 包的东西。
Here is a sample SMTP mail script:
这是一个示例 SMTP 邮件脚本:
<?php
require_once("Mail.php");
$from = "Your Name <[email protected]>";
$to = "Their Name <[email protected]>";
$subject = "Subject";
$body = "Lorem ipsum dolor sit amet, consectetur adipiscing elit...";
$host = "mailserver.blahblah.com";
$username = "smtp_username";
$password = "smtp_password";
$headers = array('From' => $from, 'To' => $to, 'Subject' => $subject);
$smtp = Mail::factory('smtp', array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if ( PEAR::isError($mail) ) {
echo("<p>Error sending mail:<br/>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message sent.</p>");
}
?>
回答by Alexis Wilke
Note that PHP mail settings come from your php.ini file. The default looks more or less like this:
请注意,PHP 邮件设置来自您的 php.ini 文件。默认看起来或多或少是这样的:
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25
; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = [email protected]
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
;sendmail_path =
; 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 =
; Add X-PHP-Originating-Script: that will include uid of the script followed by the filename
mail.add_x_header = On
; Log all mail() calls including the full path of the script, line #, to address and headers
;mail.log =
By editing your php.ini file you should be able to fix the problem without changing your PHP scripts. Also, you can test a connection with the telnet tool and the HELO, MAIL FROM, RCPT TO, DATA, QUIT commands if you directly connect to an SMTP server. With sendmail, you don't even need that, sendmail should know what it's doing (although in your case it probably wasn't and the sendmail settings probably needed a little help.)
通过编辑 php.ini 文件,您应该能够在不更改 PHP 脚本的情况下解决问题。此外,如果您直接连接到 SMTP 服务器,您可以使用 telnet 工具和 HELO、MAIL FROM、RCPT TO、DATA、QUIT 命令测试连接。使用 sendmail,您甚至不需要它,sendmail 应该知道它在做什么(尽管在您的情况下它可能不是,并且 sendmail 设置可能需要一点帮助。)
回答by Chaya Cooper
Since some of the answers give here relate to setting up SMTP in general (and not just for @shinod particular issue where it had been working and stopped), I thought it would be helpful if I updated the answer because this is a lot simpler to do now than it used to be :-)
由于这里给出的一些答案通常与设置 SMTP 相关(而不仅仅是针对它一直在工作和停止的 @shinod 特定问题),我认为如果我更新答案会有所帮助,因为这更简单现在做的比以前好:-)
In PHP 4 the PEAR Mail package is typically already installed, and this really simple tutorial shows you the few lines of code that you need to add to your php file http://email.about.com/od/emailprogrammingtips/qt/PHP_Email_SMTP_Authentication.htm
在 PHP 4 中,PEAR Mail 包通常已经安装,这个非常简单的教程向您展示了需要添加到 php 文件的几行代码http://email.about.com/od/emailprogrammingtips/qt/PHP_Email_SMTP_Authentication .htm
Most hosting companies list the SMTP settings that you'll need. I use JustHost, and they list theirs at https://my.justhost.com/cgi/help/26(under Outgoing Mail Server)
大多数托管公司都会列出您需要的 SMTP 设置。我使用 JustHost,他们在https://my.justhost.com/cgi/help/26 (在外发邮件服务器下)列出了他们的
回答by ptica
php's email()
function hands the email over to a underlying mail transfer agent
which is usually postfix
on linux systems
php 的email()
函数将电子邮件移交给mail transfer agent
通常postfix
位于 linux 系统上的底层
so the preferred method on linux is to configure your postfix to use a relayhost, which is done by a line of
所以在 linux 上的首选方法是配置你的 postfix 以使用中继主机,这是由一行完成的
relayhost = smtp.example.com
relayhost = smtp.example.com
in /etc/postfix/main.cf
在 /etc/postfix/main.cf
however in the OP's scenario I somehow suspect that it's a job that his hosting team
should have done
但是在 OP 的场景中,我以某种方式怀疑这是他hosting team
应该完成的工作
回答by symcbean
But now it is not working and I contacted our hosting team then they told me to use smtp
但现在它不起作用,我联系了我们的托管团队,然后他们告诉我使用 smtp
Newsflash - it was using SMTP before. They've not provided you with the information you need to solve the problem - or you've not relayed it accurately here.
Newsflash - 它以前使用 SMTP。他们没有为您提供解决问题所需的信息 - 或者您没有在此处准确传达。
Its possible that they've disabled the local MTA on the webserver, in which case you'll need to connect the SMTP port on a remote machine. There are lots of toolkits which will do the heavy lifting for you. Personally I like phpmailerbecause it adds other functionality.
他们可能已经禁用了网络服务器上的本地 MTA,在这种情况下,您需要连接远程计算机上的 SMTP 端口。有很多工具包可以为您完成繁重的工作。我个人喜欢phpmailer因为它增加了其他功能。
Certainly if they've taken away a facility which was there before and your paying for a service then your provider should be giving you better support than that (there are also lots of programs to drop in in place of a full MTA which would do the job).
当然,如果他们拿走了以前存在的设施并且您为服务付费,那么您的提供商应该为您提供比这更好的支持(还有很多程序可以代替完整的 MTA工作)。
C.
C。