如何使用ubuntu操作系统在PHP中发送邮件?

时间:2012-03-19 10:24:34

标签: php email pear

这是我的代码,我在项目文件夹中包含了mime.phpmail.php and Mail.php等所有文件,但邮件功能仍无效。

<?php
require "Mail.php";
require_once 'Mail/mime.php';
require_once 'Mail/mail.php';

$to = "mymail@gmail.com";
$from = "sendmail@gmail.com";
$subject = utf8_decode("mail");
$message = utf8_decode("hi");
$attachment_data = utf8_decode("testingmail1-filecontent");
$attachment_filename = utf8_decode("testingmail1.php");
send_mail_utf8_with_attachment($to, $from, $subject, $message ,$attachment_data, $attachment_filename);

function send_mail_utf8_with_attachment($to, $from, $subject, $message = "",$attachment_data="", $attachment_filename="")
{
    $params = array();
    $params['head_charset'] = 'utf-8';
    $params['text_charset'] = 'utf-8';
    $params['html_charset'] = 'utf-8';
    $params['eol'] = "\n";
    $hdrs = array('From'    => $from ,'Subject' => $subject );
    $mime = new Mail_mime($params);
    $mime->setTXTBody($message);

    if (strlen($attachment_data) && strlen($attachment_filename))
    {
        $mimeType = "application/octet-stream";
        $mime->addAttachment($attachment_data, $mimeType, $attachment_filename, false);
    }

    $body = $mime->get();
    $hdrs = $mime->headers($hdrs);
    $mail =& Mail::factory('mail');
    return $mail->send($to, $hdrs, $body);
} 
?>

我已实施所有安装:

sudo apt-get install php-pear
sudo pear install mail
sudo pear install Net_SMTP
sudo pear install Auth_SASL
sudo pear install mail_mime

但它仍无效。

2 个答案:

答案 0 :(得分:2)

您必须在Ubuntu上配置SMTP服务器。我个人通常使用ssmtp&amp; Gmail的 看看相关的tuto:http://blog.seeit.org/2010/08/php-mail-with-ubuntu-desktop-and-gmail/

答案 1 :(得分:1)

也许您发送邮件的php.ini设置不正确。尝试类似mail('caffeinated@example.com', 'My Subject', $message);的内容。如果这是有效的,你在php.ini中的邮件设置是正确的。