2013-10-10 52 views
0

我無法用php mail()函數發送郵件。在ovh共享託管windows服務器上使用php郵件功能

我想使用我的託管郵箱地址:[email protected]_domain.com。

託管是使用Windows服務器的'perso'報價。

使用郵件功能之前在php上做一些配置嗎?

我可以訪問php.ini嗎?

或者可能在ovh manager中檢查/取消勾選。

我已經在谷歌搜索和嘗試一些解決方案,但我發現很多東西,我不知道什麼是有用或不。

我也向OVH技術支持發送了請求,但我仍在等待答案。

現在,我做了一個非常基本的腳本來測試功能,但它並沒有發送郵件:

<?php 
$headers ='From: [email protected]_domain.com'."\n"; 
$headers .='Reply-To: [email protected]_domain.com'."\n"; 
$headers .='Content-Type: text/plain; charset="iso-8859-1"'."\n"; 
$headers .='Content-Transfer-Encoding: 8bit'; 
if(mail('[email protected]', 'test mail', 'Message of the mail', $headers)){ 
    echo 'ok'; 
}else{ 
    echo 'erreur'; 
} 
?> 
+0

您是否收到錯誤或電子郵件? – Brewal

+0

是的,我有一個錯誤。但是,像郵件功能返回true或false,我只是假。沒有關於這個問題的細節。 – tyd01

+0

嘗試在腳本的頂部添加'error_reporting(E_ALL | E_STRICT);'和'ini_set('display_errors',1);'看看你是否更詳細 – Brewal

回答

1

警告:電子郵件():無法連接在「localhost」的端口25的郵件服務器,在php.ini中驗證你的「SMTP」和「smtp_port」設置或使用ini_set()

這意味着郵件試圖發送感謝localhost。但是ovh提供了郵寄的特定域名:smtp.mydomain.com。所以,你將不得不使用ini_set()因爲你不能共享主機上更改php.ini

ini_set("SMTP", "smtp.mydomain.com"); 
ini_set("sendmail_from", "[email protected]"); 

$headers ='From: [email protected]_domain.com'."\n"; 
$headers .='Reply-To: [email protected]_domain.com'."\n"; 
$headers .='Content-Type: text/plain; charset="iso-8859-1"'."\n"; 
$headers .='Content-Transfer-Encoding: 8bit'; 

if (
    mail(
     '[email protected]', 
     'test mail', 
     'Message of the mail', 
     $headers 
    ) 
){ 
    echo 'ok'; 
} else { 
    echo 'erreur'; 
} 
echo "Check your email now....<br/>"; 

但我會推薦使用這個庫,如SwiftMailer,或PHPMailer

+0

我的作品!謝謝,這是我喜歡這個網站。我可以學習很多東西而不會失去幾天搜索和排序谷歌的結果。更多的是,答案非常快,不像ovh支持。 我會嘗試使用PHPMailer,但我現在已經有了一個可行的解決方案。 – tyd01

0

您的代碼看起來是正確的,所以它肯定不是一個PHP的問題,除非該功能未停用。如果您有權訪問,您應該檢查SMTP服務器的日誌。或者你可以嘗試uisng一個微小的PHP庫的郵件,大多數已知庫提供優良的異常處理和記錄的可能性,

+0

事實上,使用[SwiftMailer](https://github.com/swiftmailer/swiftmailer)會讓它更容易。 – Brewal

0

我建議你使用Swiftmailer(http://swiftmailer.org/

要發送電子郵件很簡單,只要下載庫,將它解壓縮,包括主要的PHP文件和例如代碼,這行:

require 'lib/swift_required.php'; 

$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl") 
    ->setUsername(SENDER_USERNAME) 
    ->setPassword(SENDER_PASSWORD); 


$mailer = Swift_Mailer::newInstance($transport); 

$message = Swift_Message::newInstance($subject) 
    ->setFrom(array(MAIL_FROM)) 
    ->setTo(array(MAIL_TO)) 
    ->setBody($message); 

$result = $mailer->send($message); 

正如你所看到的,它應該是簡單的配置你的smtp服務器或現有的郵件提供,如使用gmail的例子。

相關問題