Back to the module list

Create easily a mail

Info : to send the created email, use the smtp class!

Examples

Create a mail from "user@example.com" to "user2@example.com", containing two parts : a textual part with "Content of text part" and a HTML part with "Content of HTML part". There is also added an attachment (here a textual file).

require ("domframework/mail.php");
$mail = new mail ();
$mail->setFrom ("user@example.com", "User Example");
$mail->addTo ("user2@example.com", "User2 Example");
$mail->setBodyText ("Content of text part");
$mail->setBodyhtml ("Content of HTML part");
$mail->addAttachment ("file1.text", "File1 content");
echo $mail->getMail ();

The library allow too to manage the inline images :

require ("domframework/mail.php");
$mail = new mail ();
$contentID = $mail->addAttachmentInline ("file2", "Content of INLINE file");
$mail->setBodyhtml ("Content of HTML part <img src='cid:$contentID'/>");
echo $mail->getMail ();

The library can read existing mails :

require ("domframework/mail.php");
$mail = new mail ();
$mail->readMail (file_get_contents ("MAILFILE.eml"));
$mail->getDetails ();

You can get the details of the mails with :

require ("domframework/mail.php");
$mail = new mail ();
$mail->readMail (file_get_contents ("MAILFILE.eml"));
$mail->getDetails ();

This will produce an array with a lot of informations.

To get the attachment of the mail :

require ("domframework/mail.php");
$mail = new mail ();
$mail->readMail (file_get_contents ("MAILFILE.eml"));
$binaryAttachment = $mail->getAttachment (0);

The class definition

Class \mail

Namespace \

Description

 The class to create a complete email. Can read an email from a content

Properties

No property available

Methods

public function __construct ()
 The constuctor verify if the external libraries are available

public function addAttachment ($name, $fileContent, $encoding="base64", $inline=false)
 Add an attachment to the mail.
 The allowed encodings are "quoted-printable" or "base64"
 @param string $name The name of the file
 @param string $fileContent The content of the file in binary
 @param string|null $encoding The output encoding. Can be
        base64/quoted-printable
 @param boolean|null $inline Store the file in inline mode
        (multipart/related)
        If false, store the file in attached mode (multipart/mixed)

public function addAttachmentInline ($name, $fileContent, $encoding="base64")
 Add an inline attachment to the mail.
 The allowed encodings are "quoted-printable" or "base64"
 Return the Content-ID needed to be used in HTML page like :
 <img src='cid:XXXXXXXXXX'/>
 @param string $name The name of the file
 @param string $fileContent The content of the file in binary
 @param string|null $encoding The output encoding. Can be
        base64/quoted-printable
 @return string The content ID created

public function addHeader ($header, $value, $sectionID=null)
 Add a generic header
 @param string $header The name of the Header (without colon)
 @param string $value The value the store. The format must be correct !
 @param string|null $sectionID The section to modify. If null, use the main

public function addTo ($toMail, $toName="")
 Add a To: header. If it already exists, add a new recipient
 @param string $toMail The mail to add
 @param string|null $toName The name of the recipient

public function contentTypeAnalyze ($contentType)
 Analyze the Content-Type line and return an array with the values
 @param string $contentType The content Type to analyze
 @return array The analyzed Content-Type

public function convertPeopleToArray ($data)
 Convert a From/To string to array. Manage multiple recipients
 Ex. : toto toto <toto@toto.com>, titi <titi@titi.com>
      array (array ("name"=>"toto toto", "mail"=>"toto@toto.com"),
             array ("name"=>"titi", "mail"=>"titi@titi.com"))
 @param string $data The From/To field content
 @return array The array with the converted data

public function createMailEML ()
 Create the complete mail structure

public function delHeader ($header, $sectionID=null)
 Delete a specific header
 @param string $header The header to remove
 @param string|null $sectionID The section to modify. If null, use the main

public function getAttachment ($number, $inline=false)
 Get an attachment of the mail
 @param integer $number the number of attach to get starting to 0
 @param boolean|null $inline Return only the attachments Inline if true
 @return the content of the attachment. Can be binary

public function getAttachmentDetails ($number, $inline=false)
 Get the attachment details
 @param integer $number the number of attach to get starting to 0
 @param boolean|null $inline Return only the attachments Inline if true
 @return array containing the information of the attachment

public function getBodyHTML ()
 Return the HTML body if exists in UTF-8. If the body is not in UTF-8, it
 is converted
 Return false if it doesn't exists
 @return string|false The HTML body converted in UTF-8 or false if there is
         no HTML part in the mail

public function getBodyText ()
 Get the text body if exists in UTF-8. If the body is not in UTF-8, it is
 converted
 Return false if it doesn't exists
 @return string|false The Text body converted in UTF-8 or false if there is
         no Text part in the mail

public function getDate ()
 Get the Date header if defined.
 Return false if not defined
 @return string|bool The date Header if defined or false if not defined

public function getDateTimestamp ()
 Return the Date header (if defined) in timestamp
 Return false if not defined
 @return integer|bool The date Header if defined or false if not defined

public function getDetails ()
 Return an array with the details of the mail :
 the number of attachments, the from, to, subject in UTF-8, if there is
 a text and/or html body
 @return array The details of the mail

public function getFrom ()
 Return the From header as it is written in the mail
 @return string The From Header defined in the mail

public function getFromArray ()
 Return the From header converted to array with mail and name keys
 @return array The From details

public function getHeader ($header, $headers=null)
 Get a generic header
 If there is multiple headers with the same name, return the first
 @param string $header The header to get
 @param array|null $headers Optional headers to examine
 @return string|bool the literal value or false if it doesn't exist

public function getHeaderValue ($header, $headers=null)
 Get a generic header with removing the carriage return
 If there is multiple headers with the same name, return the first
 @param string $header The header to get
 @param array $headers The _headersArray array
 @return string|bool the literal value or false if it doesn't exist

public function getMail ()
 Return the complete mail
 @return string The complete mail

public function getTo ()
 Get the To Header as it is written in the mail
 @return string The To Header defined in the mail

public function provideMessageID ()
 Create a messageID
 @return string the textual MessageID

public function readMail ($content)
 Read the complete mail to analyze
 Destroy all the previous definitions of mail
 @param string $content The complete mail to read

public function sectionMainID ()
 Get the section ID of the main part
 @return bool|string the section ID of the main part or FALSE if not found

public function setBodyHTML ($htmlContent, $charset="utf-8", $encoding="quoted-printable")
 Define a HTML body. If the HTML body already exists, overwrite it
 If there is an text body, manage the boundary in alternative mode
 @param string $htmlContent in UTF-8
 @param string $charset to be stored in the mail
 @param string $encoding the encoding in the mail

public function setBodyText ($textContent, $charset="utf-8", $encoding="quoted-printable")
 Add a Text body. If the text body already exists, overwrite it
 If there is an HTML body, manage the boundary in alternative mode
 @param string $textContent in UTF-8
 @param string $charset to be stored in the mail
 @param string $encoding the encoding in the mail

public function setDate ($date)
 Set the Date
 @param string $date In RFC 2822 format

public function setDateTimestamp ($timestamp)
 Set the Date
 @param string $timestamp In Timestamp format

public function setFrom ($fromMail, $fromName="")
 Add a From: header. If it already exists, overwrite the existing one
 @param string $fromMail The from Mail to define
 @param string|null $fromName The from Name to define

public function setHeader ($header, $value, $sectionID=null)
 Set a generic header
 @param string $header The name of the Header (without colon)
 @param string $value The value the store. The format must be correct !
 @param string|null $sectionID The section to modify. If null, use the main

public function setSubject ($subject)
 Set the subject
 @param string $subject In UTF8