PHP Classes
elePHPant
Icontem

Universal PHP Mailer: Compose and send email with attachments and images

Recommend this page to a friend!
  Info   View files Documentation   View files View files (4)   DownloadInstall with Composer Download .zip   Reputation   Support forum (2)   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2017-01-14 (3 days ago) RSS 2.0 feedStarStarStar 58%Total: 291 This week: 119All time: 7,088 This week: 2Up
Version License PHP version Categories
universal-php-mailer 1.0.1Custom (specified...5HTML, Email, PHP 5, Files and Folders, T...
Description Author

This class can compose and send email with attachments and images.

It can compose text and HTML email messages that can include one or more attached files or embedded images.

The class can send the messages via SMTP server or using the mail() function.

  Performance   Level  
Name: Peter Kahl <contact>
Classes: 7 packages by
Country: Hong Kong Hong Kong
Age: ???
All time rank: 10575 in Hong Kong Hong Kong
Week rank: 5 Up1 in Hong Kong Hong Kong Up
Innovation award
Innovation award
Nominee: 2x

Details

Universal PHP Mailer

Simple yet powerful PHP mailer. It can use the PHP mail() function or the SMTP method.

It is capable of sending any type of content, including plain text, html, inline image, and any kind of attachment, hence Universal. You can use it to send very simple mail and even some very complex content combinations. Just give it whatever you have and fire it off. It will automatically configure itself to compose the correct MIME mail string with whatever parts and multiparts are appropriate.

When using the SMTP method, the mailer reuses the same socket connection for sending multiple messages, thus achieving better efficiency than the mail() function method.

These are the possible combinations (automatically configured):

Non-multipart mail:

  • only one attachment
  • only one text/plain
  • only one text/html

Multipart mail:

  • two or more attachment
  • text/plain + text/html + zero or more attachment
  • text/html + inline images + zero or more attachment
  • text/plain + text/html + inline images + zero or more attachment

Security

This package applies some measures in order to mitigate malicious abuse attempts. However, it is advisable that you always validate and/or sanitise all user input.

You should validate and santise all email addresses.

You should filter out (sanitise) line breaks (\n) from header strings.

Email Address Format

This package requires that email addresses be compliant with RFC5322, i.e. contain only printable ASCII characters. If you intend to use IDN and Unicode character email addresses, you must convert them to ASCII before applying them to this package.

Email Address Validation

This package does not validate email addresses. Therefore, you should validate all email addresses before applying them to this package.

Usage

How to send text/plain:

require 'universalphpmailer.class.php';

$mailor = new universalPHPmailer;

$mailor->toName    = 'John Smith';
$mailor->toEmail   = 'john.smith@udwiwobg';
$mailor->subject   = 'Lorem Ipsum';
$mailor->fromName  = 'James Jones';
$mailor->fromEmail = 'james.jones@udwiwobg';
$mailor->hostName  = 'zarscwfo';

$mailor->textPlain = 'Hi John,

I am testing this mailer from GitHub. Please let me know if you\'ve received this message.

Best regards,
J.J.';

$msgID = $mailor->sendMessage();

if (!empty($msgID)) {
  echo 'Successfully sent message ID ..... '. $msgID;
}

How to send text/html:

require 'universalphpmailer.class.php';

$mailor = new universalPHPmailer;

$mailor->toName    = 'John Smith';
$mailor->toEmail   = 'john.smith@udwiwobg';
$mailor->subject   = 'Lorem Ipsum';
$mailor->fromName  = 'James Jones';
$mailor->fromEmail = 'james.jones@udwiwobg';
$mailor->hostName  = 'zarscwfo';

$mailor->textHtml  = '<body>
  <p>Hi John,</p>
  <p>I am testing this mailer from GitHub. Please let me know if you\'ve received this message.</p>
  <p>Best regards,<br>J.J.</p>
</body>';

$msgID = $mailor->sendMessage();

if (!empty($msgID)) {
  echo 'Successfully sent message ID ..... '. $msgID;
}

How to send text/html + inline images:

require 'universalphpmailer.class.php';
require 'mimetypes.class.php';

$mailor = new universalPHPmailer;

$mailor->toName    = 'John Smith';
$mailor->toEmail   = 'john.smith@udwiwobg';
$mailor->subject   = 'Lorem Ipsum';
$mailor->fromName  = 'James Jones';
$mailor->fromEmail = 'james.jones@udwiwobg';
$mailor->hostName  = 'zarscwfo';

$cidA = $mailor->addInlineImage('/some/path/imageA.jpg'); # This gives us the content ID string
$cidB = $mailor->addInlineImage('/some/path/imageB.png');

$mailor->textHtml  = '<body>
  <p>Hi John,</p>
  <p>I am testing this mailer from GitHub. Please let me know if you\'ve received this message.</p>
  <p>Best regards,<br>J.J.</p>
  <div><img src="cid:'.$cidA.'" width="200" height="100" alt="Image A"></div>
  <div><img src="cid:'.$cidB.'" width="200" height="100" alt="Image B"></div>
</body>';

$msgID = $mailor->sendMessage();

if (!empty($msgID)) {
  echo 'Successfully sent message ID ..... '. $msgID;
}

How to send text/plain + text/html + inline images:

require 'universalphpmailer.class.php';
require 'mimetypes.class.php';

$mailor = new universalPHPmailer;

$mailor->toName    = 'John Smith';
$mailor->toEmail   = 'john.smith@udwiwobg';
$mailor->subject   = 'Lorem Ipsum';
$mailor->fromName  = 'James Jones';
$mailor->fromEmail = 'james.jones@udwiwobg';
$mailor->hostName  = 'zarscwfo';

$cidA = $mailor->addInlineImage('/some/path/imageA.jpg'); # This gives us the content ID string
$cidB = $mailor->addInlineImage('/some/path/imageB.png');

$mailor->textPlain = 'Hi John,

I am testing this mailer from GitHub. Please let me know if you\'ve received this message.

Best regards,
J.J.';

$mailor->textHtml  = '<body>
  <p>Hi John,</p>
  <p>I am testing this mailer from GitHub. Please let me know if you\'ve received this message.</p>
  <p>Best regards,<br>J.J.</p>
  <div><img src="cid:'.$cidA.'" width="200" height="100" alt="Image A"></div>
  <div><img src="cid:'.$cidB.'" width="200" height="100" alt="Image B"></div>
</body>';

$msgID = $mailor->sendMessage();

if (!empty($msgID)) {
  echo 'Successfully sent message ID ..... '. $msgID;
}

How to send text/plain + text/html + inline images + attachment:

require 'universalphpmailer.class.php';
require 'mimetypes.class.php';

$mailor = new universalPHPmailer;

$mailor->toName    = 'John Smith';
$mailor->toEmail   = 'john.smith@udwiwobg';
$mailor->subject   = 'Lorem Ipsum';
$mailor->fromName  = 'James Jones';
$mailor->fromEmail = 'james.jones@udwiwobg';
$mailor->hostName  = 'zarscwfo';

$cidA = $mailor->addInlineImage('/some/path/imageA.jpg'); # This gives us the content ID string
$cidB = $mailor->addInlineImage('/some/path/imageB.png');

$mailor->addAttachment('/some/path/contract.pdf');

$mailor->textPlain = 'Hi John,

I have attached the contract PDF document.

Best regards,
J.J.';

$mailor->textHtml  = '<body>
  <p>Hi John,</p>
  <p>I have attached the contract PDF document.</p>
  <p>Best regards,<br>J.J.</p>
  <div><img src="cid:'.$cidA.'" width="200" height="100" alt="Image A"></div>
  <div><img src="cid:'.$cidB.'" width="200" height="100" alt="Image B"></div>
</body>';

$msgID = $mailor->sendMessage();

if (!empty($msgID)) {
  echo 'Successfully sent message ID ..... '. $msgID;
}

How to send text/html + inline images in a loop (multiple recipients):

require 'universalphpmailer.class.php';
require 'mimetypes.class.php';

$mailor = new universalPHPmailer;

$mailor->subject   = 'Weekly best deal newsletter';
$mailor->fromName  = '"Mail Robot (don\'t reply)"'; # Display name per RFC5322
$mailor->fromEmail = 'james.jones@udwiwobg';
$mailor->hostName  = 'zarscwfo';

$recipientArr = array(
  0 => array(
            'name'  => 'John Doe', # Display name per RFC5322
            'email' => 'j.doe@udwiwobg',
  ),
  1 => array(
            'name'  => '"Jane V. Wise" (smart cookie)', # Display name and comment per RFC5322
            'email' => 'j.wise@udwiwobg',
  ),
  2 => array(
            'name'  => '"Robert W. Simth"', # Display name per RFC5322
            'email' => 'robert.smith@udwiwobg',
  ),
);

# These 2 images are same for all recipients
$cidA = $mailor->addInlineImage('/some/path/imageA.jpg'); # This gives us the content ID string
$cidB = $mailor->addInlineImage('/some/path/imageB.png');

# The loop
foreach ($recipientArr as $recipient) {

  $mailor->toName    = $recipient['name'];
  $mailor->toEmail   = $recipient['email'];

  $mailor->textHtml  = '<body>
    <p>Hi '.$recipient['name'].',</p>
    <p>You are receiving this newsletter because you have subscribed.</p>
    <p>Best regards,<br>'.$mailor->fromName.'</p>
    <div><img src="cid:'.$cidA.'" width="200" height="100" alt="Image A"></div>
    <div><img src="cid:'.$cidB.'" width="200" height="100" alt="Image B"></div>
  </body>';

  $msgID = $mailor->sendMessage();

  if (!empty($msgID)) {
    echo 'Successfully sent message ID ..... '. $msgID .' .... To: '. $recipient['email'] . PHP_EOL;
  }
}

If you want to be sure that display name in headers is per RFC5322, use the method formatDisplayName. This will avoid some undesired behaviour.

(Here I am using an example of text/plain mail):

require 'universalphpmailer.class.php';

$mailor = new universalPHPmailer;

$mailor->toName    = $mailor->formatDisplayName('Mao "Chairman" ???');
$mailor->toEmail   = 'mao@backintime.sample';
$mailor->subject   = '??';
$mailor->fromName  = $mailor->formatDisplayName('James J. Jones');
$mailor->fromEmail = 'james.jones@udwiwobg';
$mailor->hostName  = 'host.name.sample';

$mailor->textPlain = 'Hi ??,

Is there life in the afterworld?

Best regards,
J.J.J.';

$msgID = $mailor->sendMessage();

if (!empty($msgID)) {
  echo 'Successfully sent message ID ..... '. $msgID;
}

Acknowledgements

Peter Kahl had written much of the SMTP-related methods of this package as a result of inspiration from the following class and extends his thanks to the authors thereof:

> PHPMailer RFC821 SMTP email transport class. > > Implements RFC 821 SMTP commands and provides some utility methods for sending mail to an SMTP server. > > @package PHPMailer > > @author Chris Ryan > > @author Marcus Bointon <phpmailer@synchromedia.co.uk> > > <https://github.com/PHPMailer/PHPMailer/blob/master/class.smtp.php>

  Files folder image Files  
File Role Description
Accessible without login Plain text file LICENSE Lic. License text
Plain text file mimetypes.class.php Class Class source
Accessible without login Plain text file README.md Doc. Documentation
Plain text file universalphpmailer.class.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:291
This week:119
All time:7,088
This week:2Up
User Ratings User Comments (1)
 All timeMonth
Utility:83%StarStarStarStarStar83%Equal
Consistency:83%StarStarStarStarStar83%Equal
Documentation:83%StarStarStarStarStar83%Equal
Examples:--
Tests:--
Videos:--
Overall:58%StarStarStar58%Equal
Rank:13564Up
 
thats a very good class ;-)
8 days ago (Josť Filipe Lopes Santos)
70%StarStarStarStar