【问题标题】:php Email Classphp 电子邮件类
【发布时间】:2011-12-24 14:36:25
【问题描述】:

我有一个发送电子邮件的 php 脚本。它目前使用 php mail() 函数。

然后该函数连接到 linux 机器上的 sendmail 功能。然而,对于这个实现,我们想要创建一个不依赖于 sendmail 设置或什至存在的 php 脚本,以便我们可以将代码转移到任何机器上并拥有所有功能。

首先这可能吗?我在网上看过一些可下载的课程,所以我认为是的。

第二有没有人这样做过,你知道一个好的课程或开始的地方吗?

我确实需要随电子邮件一起发送附件。

【问题讨论】:

标签: php email


【解决方案1】:

那里有很多示例 - 我使用 swiftmailer ...http://swiftmailer.org/http://code.google.com/p/swiftmailer/

示例用法:

require_once 'swift/lib/swift_required.php';

$smtp = Swift_SmtpTransport::newInstance('smtp.host.tld', 25)
  ->setUsername(' ... ')
  ->setPassword(' ... ');

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

$message = Swift_Message::newInstance('Your subject');
$message
  ->setTo(array(
    'user1@example.org',
    'user2@example.org' => 'User Two',
    'user3@exmaple.org' => 'Another User Name'
  ))
  ->setFrom(array('your@address.com' => 'Your Name'))
  ->attach(Swift_Attachment::fromPath('/path/to/doc1.pdf'))
  ->attach(Swift_Attachment::fromPath('/path/to/doc2.pdf'))
  ->setBody(
    'Here is an image <img src="' . $message->embed(Swift_Image::fromPath('/path/to/image.jpg')) . '" />',
    'text/html'
  )
  ->addPart('This is the alternative part', 'text/plain')
  ;

if ($mailer->send($message))
{
  echo "Message sent!";
}
else
{
  echo "Message could not be sent.";
}

【讨论】:

  • 哦,请去掉那个 & 符号,它是 PHP4 风格的。
【解决方案2】:

您可能还想考虑Zend_Mail。我们已经使用了一段时间了,没有问题。

【讨论】:

    【解决方案3】:

    PHPMailer 是一个非常好的库,您可以使用它通过 SMTP 发送邮件,因此您不必依赖 sendmail。 PHPMailer 还提供了一种将文件附加到电子邮件的简单方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-07
      • 1970-01-01
      • 1970-01-01
      • 2013-08-02
      • 2016-06-07
      • 1970-01-01
      • 1970-01-01
      • 2014-08-17
      相关资源
      最近更新 更多