2011-03-14 85 views
-1

我的代碼 -幫助在PHP中使用郵件功能

$from = "From:Company\n\r"; 
$mesg = include('mail.html'); 
function mail($u,'hey',$mesg); 

發送郵件基本上,我想發郵件,在該消息需要是mail.html。

help ...需要將消息發送至$u

+0

也是我需要的郵件被抄送到[email protected] 。謝謝。 – sarthak 2011-03-14 18:50:39

+1

我不明白你的問題。你是否重新定義郵件問題? *在旁註中,我建議使用[Swift Mailer](http://swiftmailer.org/)而不是簡單的php'mail'函數。 – Czechnology 2011-03-14 18:52:13

+1

你甚至沒有打擾搜索PHP郵件的權利?所有「相關」主題都顯示在正確的答案上。 – Jakub 2011-03-14 18:52:39

回答

1

下面是一個使用Swift Mailer當例子的你如何能做到這一點(從手冊中的實例組成):

<?php 

require_once 'lib/swift_required.php'; 

//Create the message 
$message = Swift_Message::newInstance() 

    //Give the message a subject 
    ->setSubject('Your subject') 

    //Set the From address with an associative array 
    ->setFrom(array('[email protected]' => 'Your Name')) 

    //Set the To addresses with an associative array 
    ->setTo(array('[email protected]' => 'Recipient\'s name')) 

    //Set CC 
    ->setCc('[email protected]') 

    //Give it a body 
    ->setBody(file_get_contents('mail.html')) 

    ; 

//Create the Mailer 
$mailer = Swift_Mailer::newInstance(Swift_MailTransport::newInstance()); 

//Send the message 
$result = $mailer->send($message); 
+0

@Czechnology這太酷了。謝謝...你能告訴我如何使用$ u而不是收件人的電子郵件。 $ u是註冊時的用戶名。謝謝 – sarthak 2011-03-14 18:59:29

+0

@Czechnology另外,我是否需要只包含庫中所有文件中的一個文件? – sarthak 2011-03-14 19:01:13

+0

@sarthak,你不能發送電子郵件到用戶名,你需要一個電子郵件地址。如果你有用戶的電子郵件地址和他的名字,你可以使用' - > setTo(array('[email protected]'=>'Username'))''。 – Czechnology 2011-03-14 19:01:34

1

不要在郵件的前面使用function,只是做

mail($emailto, $subject, file_get_contents('mail.html'), $headers); 
+0

好的...什麼是$標頭。我如何定義它? – sarthak 2011-03-14 18:52:35

+0

$ header是你的$從 – Thew 2011-03-14 18:54:16

+2

檢查PHP的郵件文檔頁面http://www.php.net/manual/en/function.mail.php - 有很多如何構建郵件消息的例子。 '$ headers'也用於CC – 2011-03-14 18:54:24

1

這不會起作用,因爲include()包括文件和評估板(解釋的內容),你正在試圖使它像一個函數返回一個不是的值。

您需要做的是使用file_get_contents()或simillar功能將文件內容讀入變量,然後將其用作消息正文。

+0

那麼我如何才能使它工作? – sarthak 2011-03-14 18:52:54

+0

我應該使用普通的PHP郵件功能還是像swiftmail等更好的郵件功能? – sarthak 2011-03-14 18:55:01

+0

通讀整個頁面,以及php手冊頁面的郵件。一旦你花點時間瞭解它的工作原理,這真的很容易。至於你的第二個問題,那真的取決於。如果您嘗試發送大量郵件或需要高級功能(如mime附件等),則應該查看SwiftMailer,PHPMailer或類似的解決方案。 – 2011-03-14 18:58:52

1

我建議你使用PHPMailer類,它真的會給你很多設施和功能,而且下載包含的例子,你將需要包括類文件,然後使用它的免費。如需更多資料go to this link