php邮件的smtp配置

shi*_*hin 11 php email smtp

我通过使用php邮件功能从我的网站发送邮件.但现在它没有工作,我联系了我们的托管团队,然后他们告诉我使用smtp,因为他们在服务器上做了一些更改.我不知道怎么做.当前代码(带有php邮件功能)如下,任何人都可以帮助我解决我必须做的更改.

<?php
$mail_To="someone@gmail.com";
$headers = "";
$headers .= "From: livetv@muscle-tube.com\n";
$headers .= "Reply-To: livetv@muscle-tube.com\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);
?>
Run Code Online (Sandbox Code Playgroud)

JCD*_*JCD 13

PHP的mail()功能不支持SMTP.您将需要使用类似PEAR Mail包的东西.

这是一个示例SMTP邮件脚本:

<?php
require_once("Mail.php");

$from = "Your Name <email@blahblah.com>";
$to = "Their Name <otheremail@whatever.com>";
$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>");
}
?>
Run Code Online (Sandbox Code Playgroud)


Ale*_*lke 5

请注意,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 = me@example.com

; 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 =
Run Code Online (Sandbox Code Playgroud)

通过编辑php.ini文件,您应该能够在不更改 PHP 脚本的情况下解决问题。此外,如果直接连接到 SMTP 服务器,您可以使用 telnet 工具和HELOMAIL FROMRCPT TODATA、命令测试连接。QUIT使用sendmail,您甚至不需要它,sendmail应该知道它在做什么(尽管在您的情况下可能不是,并且设置sendmail可能需要一些帮助。)

更新:在大多数情况下,telnet不再安装,因为它被认为是危险的(即,它为您提供了一个明文连接,通常在本地网络上没问题,但对远程计算机来说就不那么好了)。相反,我们有nc非常相似的测试方法,例如 SMTP,但实际上不允许远程 shell 连接。话虽如此,越来越多的 SMTP 也将使用加密,因此最好的测试工具仍然是sendmail.