2011-07-27 102 views
1

我想通过PHP发送HTML邮件,但我似乎无法得到它的工作。PHP发送HTML邮件

这是我的代码:

// multiple recipients 
    $to = 'mail'; 

    // subject 
    $subject = 'Subject'; 

    // message 
    $message = " 
    <html> 
    <head> 
     <title>Thanks</title> 
    </head> 
    <body> 
     <div> 
      <b>Thanks for your email</b> 
     </div> 
    </body> 
    </html> 
    "; 

    // To send HTML mail, the Content-type header must be set 
    $headers = 'MIME-Version: 1.0' . "\r\n"; 
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 

    // Additional headers 
    $headers .= 'To: info <[email protected]>' . "\r\n"; 
    $headers .= 'From: Pynix <[email protected]>' . "\r\n"; 

    // Mail it 
    mail($to, $subject, $message, $headers); 

这也没有表现出任何发件人当我收到它在Outlook中。

任何想法?

感谢

+0

看看这里和其他相关的HTML电子邮件的答案http://stackoverflow.com/questions/3058897/sending-html-email-from-php –

+0

是sendmail或安装的东西? – Sparky

+0

@Sparky:sendmail肯定是安装的。 OP说:“当我在Outlook中收到它时”。所以,一定有什么问题......这段代码似乎直接取自PHP.net网站。 –

回答

1

我不认为你必须把To:行放在标题中,因为它是mail函数的一个参数。 然而,一些邮件客户端不喜欢光头,这里是我的这是工作:

$header = 'From: "Contact" <mail>'.PHP_EOL. 
        'Reply-to: <mail>'.PHP_EOL. 
        'MIME-Version: 1.0'.PHP_EOL. 
        'Content-Type: text/plain; charset=utf-8'.PHP_EOL. 
        'Content-Transfer-Encoding: 8bit'.PHP_EOL. 
        'X-Mailer: PHP/'.PHP_VERSION.PHP_EOL; 
0

我用SwiftMailer:

require_once('../lib/swiftMailer/lib/swift_required.php'); 
... 
function sendEmail(){ 
    //Sendmail 
    $transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs'); 

    //Create the Mailer using your created Transport 
    $mailer = Swift_Mailer::newInstance($transport); 

    $body="Dear $fname,\n\nYour job application was successful. \n\nYours,\n\nEamorr\n\n\n\n\n\n\n"; 


    //Create a message 
    $message = Swift_Message::newInstance('Subject goes here') 
    ->setFrom(array($email => "[email protected]")) 
    ->setTo(array($email => "$fname $lname")) 
    ->setBody($body); 

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

您可以轻松地同时发送纯文本和HTML电子邮件。

+0

嘿!我知道这有点偏离主题,但迟了3年,但在使用SwiftMailer时,可以将它与CKEditor结合使用吗?我的意思是,你可以在CKEditor中设计你的邮件,然后通过SwiftMailer发送CKEditor输出的HTML,它会正常显示吗? – kfirba

0

固定。

我发现这里面由于某种原因,工作完全在我的服务器上:

// Set The Headers: 
$headers  = 'From: "Me" <[email protected]>'.PHP_EOL. 
'Reply-to: <[email protected]>'.PHP_EOL. 
'Cc: <Her @There.com>'.PHP_EOL. 
'MIME-Version: 1.0'.PHP_EOL. 
'Content-type: text/html; charset=iso-8859-1'.PHP_EOL. 
'Content-Transfer-Encoding: 8bit'.PHP_EOL. 
'X-Mailer: PHP/'.PHP_VERSION.PHP_EOL; 
// Send: 
mail($to, $subject, $message, $headers); 

感谢您的输入家伙。