Tag Archives: mail

PHP and PHPMailer: How to Check if Your Mail Send Failed CodeUnit 21 DEC 2011

PHPMailer is a great little mail sending workhorse class for PHP, widely expanding on the regular PHP mail() function and particularly easy to make use of, making it a personal favourite of mine.

Anyway, to check whether or not your mail send was successful when using PHPMailer, all you need to do is check the Boolean variable returned by the Send() function, and if false, check what the error message contained in ErrorInfo is.

In practice:

require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "smtp1.example.com;smtp2.example.com";
$mail->SMTPAuth = true;
$mail->Username = 'smtpusername';
$mail->Password = 'smtppassword';

$mail->AddAddress("email@example.com");
$mail->Subject = "Test 1";
$mail->Body = "Test 1 of PHPMailer.";

if(!$mail->Send())
{
   echo "Error sending: " . $mail->ErrorInfo;
}
else
{
   echo "E-mail sent";
}

As simple as that! :)

PHP: How to Send a PDF File Attachment via E-mail CodeUnit 08 JUN 2011

I’ve moved to the fantastic PHPMailer PHP class to handle all my e-mail send outs for my projects and have thus far been quite impressed by the ease-of-use and robustness of the class. Today I’ll quickly note how you can send an attachment (like a PDF file for example) with a mail send out.

As you can see from the code snippet below, attaching a file is as simple as passing a valid file path to the file to be included to the AddAttachment() function call and then hitting Send().

//create the mail class and fill in all the required settings
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "smtp.server.net";
$mail->SMTPAuth = true;
$mail->Username = "username@domain.com";
$mail->Password = "password1";
$mail->From = "username@domain.com";
$mail->FromName = "Software Simian";
$mail->AddAddress("targetguy@domain.com", "Target Guy");
$mail->AddReplyTo("username@domain.com", "Software Simian");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = "Subject";
$mail->Body    = "Message";
$mail->AltBody = strip_tags("Message:);

//and now for the actual bit when it comes to adding a file attachment to an e-mail
$mail->AddAttachment("/home/username/fileToUpload/report.pdf");

//and send. Now was that not easy?
if(!$mail->Send()){
    $resultstatus = 'Failed';
}

Couldn’t be any simpler, could it? :)

Related Link: http://phpmailer.worxware.com/

PHP Code to Extract Attachments from Mail Files CodeUnit 08 DEC 2009

PHP-logoFor a recent project I needed to parse .mai mail files to locate any Excel or Zip files that may have been sent through as attachments. The following block of code is the result of my work:


Obviously by extending the content types being searched on with a simple or statement, you can alter the script to extract just about any type of attachment, making it quite useful indeed.

It’s fast and appears to work pretty nicely, so hopefully it can be of some use to you then! :)