1

I have a php script that sends an email. It currently uses the php mail() function.

That function then connects to sendmail feature on the linux machine. However for this implementation we were wanting to create a php script that doesn't rely on sendmail being setup or even existing so that we can move to code to any machine and have all the functionality self contained.

First off is this even possible? I've seen a few downloadable classes online so I think that it is.

Second has anyone done this before and do you know a good class or place to start?

I do need to send attachments along with the email.

Treffynnon
  • 21,365
  • 6
  • 65
  • 98
jcmitch
  • 2,088
  • 9
  • 27
  • 33
  • possible duplicate of [How to send an email in PHP reliably?](http://stackoverflow.com/questions/3297531/how-to-send-an-email-in-php-reliably). There are tons of other related questions all advocating various classes. – Mike B Nov 08 '11 at 16:53
  • possible duplicate of [php mail() function on localhost](http://stackoverflow.com/questions/5342822/php-mail-function-on-localhost) – Treffynnon Nov 08 '11 at 17:09

3 Answers3

4

PHPMailer is a very nice library which you can use to send mails via SMTP, so you don't have to rely on sendmail. PHPMailer also provides an easy way to attach files to your email.

halfdan
  • 33,545
  • 8
  • 78
  • 87
2

You may also want to consider Zend_Mail. We've been using it for awhile now without issue.

Mike Purcell
  • 19,847
  • 10
  • 52
  • 89
1

There are lots of examples out there - I use swiftmailer ... http://swiftmailer.org/ or http://code.google.com/p/swiftmailer/

example usage :

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.";
}
Manse
  • 37,765
  • 10
  • 83
  • 108