PHP Classes

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

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 61%Total: 1,080 All time: 3,452 This week: 66Up
Version License PHP version Categories
universal-php-mailer 4.8Custom (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.

Picture of Peter Kahl
  Performance   Level  
Name: Peter Kahl <contact>
Classes: 37 packages by
Country: United Kingdom
Age: ???
All time rank: 41521 in United Kingdom
Week rank: 170 Up5 in United Kingdom Up
Innovation award
Innovation award
Nominee: 23x

Winner: 2x

Documentation

Universal PHP Mailer????

Downloads License If this project has business value for you then don't hesitate to support me with a small donation.

Easy to use mailer capable of sending any type of content including plain text, HTML, inline images, and any kind and any number of attachments. The value of this library lies in that it generates whole mail string including all headers in compliance with standards, while being very simple to integrate into your system.

Some Features

  • Send emails with multiple To, Cc, Bcc and Reply-To addresses
  • Send emails with 1 or more attachments
  • Send emails having nothing but 1 attachment
  • Send emails of any complexity (plain, HTML, inline images, attachments)
  • UTF-8 support for headers and message body text
  • Time zone support for Date header
  • Logging for debug etc.

Automatic Configuration & Ease of Integration

Say, you want to email a message consisting only of a PDF attachment. Nothing else. No text. How do you do that? You need not worry! Take your attachment, apply it to the Mailer and voila! Done.

Say, you want to email a message consisting of text/plain, text/html, several inline images and several attachments of various kind. How do you do that? You need not worry! Take all and any of your content, apply it to the Mailer and voila! Done.

Efficiency for Bulk Mailing

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.

Handles Any Kind of Content

If we want to send a mail that consists of only 1 (category of) content, it is a Non-multipart mail. Non-multipart mail use cases--

| Case | text/plain | text/html | inline image | attachment | | :---:|:----------:| :--------:| :-----------:| :---------:| | 1 | - | - | - | 1 | | 2 | 1 | - | - | - | | 3 | - | 1 | - | - |

Once we need to send a mail with 2 or more contents (regardless of category), we're talking about Multipart mail. Multipart mail use cases--

| Case | text/plain | text/html | inline image | attachment | | :---:|:----------:| :--------:| :-----------:| :---------:| | 4 | - | - | - | ? 2 | | 5 | 1 | - | - | ? 1 | | 6 | - | 1 | ? 0 | ? 0 | | 7 | 1 | 1 | ? 0 | ? 0 |

Usage Examples

The Basics

use peterkahl\universalPHPmailer\universalPHPmailer;

$mailor = new universalPHPmailer;

// Set the parameters
$mailor->SMTPserver      = 'smtp.gmail.com';           # The SMTP host we will connect to
$mailor->SMTPport        = 587;                        # using this port; default is 25
$mailor->SMTPusername    = 'example@gmail.com';        # username for authentication
$mailor->SMTPpassword    = ''; # password
$mailor->forceSMTPsecure = true;                       # Insist on STARTTLS and nothing else.
$mailor->CAfile          = '/path/to/cacert.pem';      # Where we keep the CA bundle
$mailor->SMTPhelo        = 'www.myamazingwebsite.com'; # Our HELO hostname
$mailor->CacheDir        = '/path/to/cache_dir';       # Writeable directory
$mailor->hostName        = 'myamazingwebsite.com';     # For Message ID header
$mailor->DateHeaderZone  = 'Europe/London';            # Time zone for date header (new in version 4.0); default is Etc/GMT

// Subject. What's this about.
$mailor->Subject = 'Vibrant growth on epic scale';

// Who the sender is.
$mailor->SenderFrom = array('james.jones@hotmai1.con' => 'James Jones');

// Only 1 To
$mailor->RecipientTo = array(
                            'john.smith@hotmai1.con' => 'John Smith'
                            );

// Or multiple To
$mailor->RecipientTo = array(
                            'john.smith@hotmai1.con' => 'John Smith',
                            'paul.smith@hotmai1.con' => 'Paul',
                            'jane@hotmai1.con'       => '',
                            );

// You may have some Cc
$mailor->RecipientCc = array(
                            'paul.smith@hotmai1.con' => 'Paul',
                            'jane@hotmai1.con'       => '',
                            );

// You may have 1 or more Bcc
$mailor->RecipientBcc = array(
                            'root@hotmai1.con'       => 'Sean Connolly',
                            );

// You may have 1 or more Reply-To
$mailor->ReplyToHeader = array(
                            'pauline.smith@hotmai1.con' => 'Pauline',
                            'john@hotmai1.con'          => '',
                            );

Sending text/plain mail:

$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 '. $msgID;
}

Sending text/html mail:

$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 '. $msgID;
}

Sending text/html + inline images mail:

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

// The below string has the <IMG> tag with respective content IDs.
$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 '. $msgID;
}

Sending text/plain + text/html + inline images mail:

// First add the images. This gives us content ID strings.
$cidA = $mailor->addInlineImage('/some/path/imageA.jpg');
$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.';

// The below string has the <IMG> tag with respective content IDs.
$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 '. $msgID;
}

Sending text/plain + text/html + inline images + attachment mail:

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

// This is how we add attachments.
$mailor->addAttachment('/some/path/contract.pdf');

$mailor->textPlain = 'Hi John,

I have attached the contract PDF document.

Best regards,
J.J.';

// The below string has the <IMG> tag with respective content IDs.
$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 '. $msgID;
}

Sending text/html + inline images mail in a loop (high volume) while reusing the socket connection:

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

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

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

  $mailor->RecipientTo = array(
                              $recipient['email'] => $recipient['name']
                              );

  # If you want image CID's to be unique message to message, you should
  # unset these properties and add the attachments inside the loop!
  //$mailor->unsetInlineImages();
  //$mailor->unsetAttachments();
  //$cidA = $mailor->addInlineImage('/some/path/imageA.jpg');
  //$cidB = $mailor->addInlineImage('/some/path/imageB.png');

  $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 '. $msgID .' .... To: '. $recipient['email'] . PHP_EOL;
  }
}

Formatting of Display Name

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

use peterkahl\universalPHPmailer\universalPHPmailer;

$mailor = new universalPHPmailer;

$mailor->SMTPserver      = 'smtp.gmail.com';
$mailor->SMTPport        = 587;
$mailor->SMTPusername    = 'example@gmail.com';
$mailor->SMTPpassword    = '';
$mailor->forceSMTPsecure = true;
$mailor->CAfile          = '/path/to/cacert.pem';
$mailor->SMTPhelo        = 'www.myamazingwebsite.com';
$mailor->CacheDir        = '/path/to/cache_dir';
$mailor->hostName        = 'myamazingwebsite.com';

$mailor->RecipientTo = array(
                            'mao@backintime.sample' => $mailor->formatDisplayName('Mao "Chairman" ???')
                            );

$mailor->Subject    = '??';
$mailor->SenderFrom = array('james.jones@hotmai1.con' => $mailor->formatDisplayName('James J. Jones'));

$mailor->textPlain = 'Hi ??,

Is there life in the afterworld?

Best regards,
J.J.J.';

$msgID = $mailor->sendMessage();

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

*

Considerations

This package applies some measures in order to mitigate malicious abuse attempts. Despite this, it is advised 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 uses simple email address validation. It is advised that you validate all email addresses before applying them to this package.

*

Acknowledgement

The SMTP-related methods of this package are a fork from https://github.com/PHPMailer/PHPMailer/blob/master/class.smtp.php


  Files folder image Files (5)  
File Role Description
Files folder imagesrc (2 files)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (5)  /  src  
File Role Description
  Plain text file MIMEtypes.php Class Class source
  Plain text file universalPHPmailer.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:1,080
This week:0
All time:3,452
This week:66Up
User Ratings User Comments (1)
 All time
Utility:88%StarStarStarStarStar
Consistency:86%StarStarStarStarStar
Documentation:86%StarStarStarStarStar
Examples:-
Tests:-
Videos:-
Overall:61%StarStarStarStar
Rank:1015
 
thats a very good class ;-)
7 years ago (José Filipe Lopes Santos)
70%StarStarStarStar