如何使用邮件功能配置smtp设置?

时间:2013-04-04 13:45:37

标签: php email smtp sendmail

如何配置带有邮件功能smtp的smtp设置是必需的,因为由于安全原因,虚拟主机不允许在没有smtp设置的情况下发送电子邮件。

warning error: mail() has been disabled for security reasons

现在如何使用此邮件功能()配置smtp设置?

<?php 
if(isset($_REQUEST['confirm'])){
$your_email = 'non-reply@test.com'; //CHANGE TO YOUR SETTINGS
$domain = $_SERVER["HTTP_HOST"]; //YOUR DOMAIN AND EXTENSION
$to = $email;
$subject = 'Demo Your Order';
$message = 'test1234';
$headers .= 'MIME-Version: 1.0' . "\r\n";  
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";  
$headers .= "From: Demo.Pk<'.$your_email.'@'.$domain.'>\r\n" .  
//"Reply-To: $from \r\n" .  
"X-Mailer: PHP/" . phpversion(); 

mail($to, $subject, $message, $headers);
}

&GT;

1 个答案:

答案 0 :(得分:2)

似乎邮件已禁用或PHP.ini中没有正确的设置。我认为,你仍然可以使用SMTP设置发送电子邮件到其他一些SMTP服务器,如gmail。请检查以下代码,它可能会帮助您解决此问题。下面的代码使用Mail.php frm PEAR

<?php

       require_once "Mail.php";

        $from = "<me@gmail.com>";
        $to = "<you@gmail.com>";
        $subject = "Hi!";
        $body = "Hi,\n\nHope this helps?";

        $host = "ssl://smtp.gmail.com";
        $port = "465";
        $username = "me@gmail.com";  //<> give errors
        $password = "mypassword";

        $headers = array ('From' => $from,
          'To' => $to,
          'Subject' => $subject);
        $smtp = Mail::factory('smtp',
          array ('host' => $host,
            'port' => $port,
            'auth' => true,
            'username' => $username,
            'password' => $password));

        $mail = $smtp->send($to, $headers, $body);

        if (PEAR::isError($mail)) {
          echo("<p>" . $mail->getMessage() . "</p>");
         } else {
          echo("<p>Message successfully sent!</p>");
         }

    ?>

您还可以使用其他一些邮件库,如PHPMailer