views:

760

answers:

3

Hi there,

I'm trying to send out a Plain/HTML multipart email out and I'm currently using PHP's mail() function. Many people have recommended PHPMailer so I thought I'd give it a go.

However, as everything seems to be nowadays, it appears very complicated. I downloaded it and it talks about installing it and configuring MySQL connections and SMTP connections!? All I want to do is use a nice class that will build the MIME emails for me and send them! I understand the SMTP possibilities but it all seems so complex!

Is there some way of simply just using it, for example, include a php file (no server installation or re-compiling PHP!) and then just using the class to build and send the email?

I'd be very grateful if someone could explain things simply! I'm sure it's possible and I can't believe after my hours of searching there's no really good, simple article about it online. Everything's TOO complicated when I know it doesn't need to be!

Many thanks,

Michael

+1  A: 

Try SwiftMailer instead.

Zed
Thanks, I got this working perfectly first time! Great docs too! :-)
Michael Waterfall
+1  A: 

I don't know anything about PHPMailer, but I recommend using Zend_Mail. Here's a simple example with an attachment:

$mail = new Zend_Mail();
$mail->setBodyText('This is the text of the mail.');
$mail->createAttachment($myImage,
                        'image/gif',
                        Zend_Mime::DISPOSITION_INLINE,
                        Zend_Mime::ENCODING_8BIT);
$mail->setFrom('[email protected]', 'Some Sender');
$mail->addTo('[email protected]', 'Some Recipient');
$mail->setSubject('TestSubject');
$mail->send();

It probably does everything you want (Attachments, HTML, SMTP-Configuration, ...). By default it uses sendmail, like the mail() function, so you don't have to configure anything like SMTP if you don't need it.

It also has very good documentation, so you won't have trouble finding examples.

mooware
+1  A: 

Pretty way (from this link), first extend PHPMailer and set the defaults for your site :

require("class.phpmailer.php");

class my_phpmailer extends phpmailer {
    // Set default variables for all new objects
    var $From     = "[email protected]";
    var $FromName = "Mailer";
    var $Host     = "smtp1.example.com;smtp2.example.com";
    var $Mailer   = "smtp";                         // Alternative to IsSMTP()
    var $WordWrap = 75;

    // Replace the default error_handler
    function error_handler($msg) {
        print("My Site Error");
        print("Description:");
        printf("%s", $msg);
        exit;
    }

    // Create an additional function
    function do_something($something) {
        // Place your new code here
    }
}

Then include the above script where needed (in this example it is named mail.inc.php) and use your newly created my_phpmailer class somewhere on your site:

require("mail.inc.php");//or the name of the first script

// Instantiate your new class
$mail = new my_phpmailer;

// Now you only need to add the necessary stuff
$mail->AddAddress("[email protected]", "Josh Adams");
$mail->Subject = "Here is the subject";
$mail->Body    = "This is the message body";
$mail->AddAttachment("c:/temp/11-10-00.zip", "new_name.zip");  // optional name

if(!$mail->Send())
{
   echo "There was an error sending the message";
   exit;
}

echo "Message was sent successfully";
karim79