dolibarr 25.0.0-alpha
CMailFile Class Reference

Class to send emails (with attachments or not) Usage: $mailfile = new CMailFile($subject,$sendto,$replyto,$message,$filepath,$mimetype,$filename,$cc,$ccc,$deliveryreceipt,$msgishtml,$errors_to,$css,$trackid,$moreinheader,$sendcontext,$replyto); $mailfile->sendfile();. More...

Public Member Functions

 __construct ($subject, $to, $from, $msg, $filename_list=array(), $mimetype_list=array(), $mimefilename_list=array(), $addr_cc="", $addr_bcc="", $deliveryreceipt=0, $msgishtml=0, $errors_to='', $css='', $trackid='', $moreinheader='', $sendcontext='standard', $replyto='', $upload_dir_tmp='', $in_reply_to='', $references='')
 CMailFile.
 
 sendfile ()
 Send mail that was prepared by constructor.
 
 dump_mail ()
 Write content of a SMTP request into a dump file (mode = all) Used for debugging.
 
 save_dump_mail_in_err ($message='')
 Save content if mail is in error Used for debugging.
 
 checkIfHTML ($msg)
 Correct an incomplete html string.
 
 buildCSS ()
 Build a css style (mode = all) into this->styleCSS and this->bodyCSS.
 
 write_smtpheaders ()
 Create SMTP headers (mode = 'mail')
 
 write_mimeheaders ($filename_list, $mimefilename_list)
 Create header MIME (mode = 'mail')
 
 write_body ($msgtext)
 Return email content (mode = 'mail')
 
 write_images ($images_list)
 Attach an image to email (mode = 'mail')
 
 checkSmtpTargetAllowed ($host, $port)
 Check that the SMTP target (host and port) is allowed.
 
 check_server_port ($host, $port)
 Try to create a socket connection.
 
 server_parse ($socket, $response)
 This function has been modified as provided by SirSir to allow multiline responses when using SMTP Extensions.
 

Static Public Member Functions

static encodetorfc2822 ($stringtoencode)
 Encode subject according to RFC 2822 - http://en.wikipedia.org/wiki/MIME#Encoded-Word.
 
static getValidAddress ($address, $format, $encode=0, $maxnumberofemail=0)
 Return a formatted address string for SMTP protocol.
 
static getArrayAddress ($address)
 Return a formatted array of address string for SMTP protocol.
 

Private Member Functions

 _encode_file ($sourcefile)
 Read a file on disk and return encoded content for emails (mode = 'mail')
 
 write_files ($filename_list, $mimetype_list, $mimefilename_list, $cidlist)
 Attach file to email (mode = 'mail')
 
 findHtmlImages ($images_dir)
 Search images into html message and init array this->images_encoded if found This is called in the CMailFile constructor but only if MAIN_MAIL_ADD_INLINE_IMAGES_IF_IN_MEDIAS is set (Off by default)
 
 findHtmlImagesIsSrcData ($images_dir)
 Search images with src="data:image..." format into html message.
 

Detailed Description

Class to send emails (with attachments or not) Usage: $mailfile = new CMailFile($subject,$sendto,$replyto,$message,$filepath,$mimetype,$filename,$cc,$ccc,$deliveryreceipt,$msgishtml,$errors_to,$css,$trackid,$moreinheader,$sendcontext,$replyto); $mailfile->sendfile();.

Definition at line 43 of file CMailFile.class.php.

Constructor & Destructor Documentation

◆ __construct()

CMailFile::__construct ( $subject,
$to,
$from,
$msg,
$filename_list = array(),
$mimetype_list = array(),
$mimefilename_list = array(),
$addr_cc = "",
$addr_bcc = "",
$deliveryreceipt = 0,
$msgishtml = 0,
$errors_to = '',
$css = '',
$trackid = '',
$moreinheader = '',
$sendcontext = 'standard',
$replyto = '',
$upload_dir_tmp = '',
$in_reply_to = '',
$references = '' )

CMailFile.

Parameters
string$subjectTopic/Subject of mail
string$toRecipients emails (RFC 2822: "Name firstname <email>[, ...]" or "email[, ...]" or "<email>[, ...]"). Note: the keyword 'SUPERVISOREMAIL' is not allowed here and must be replaced by caller.
string$fromSender email (RFC 2822: "Name firstname <email>[, ...]" or "email[, ...]" or "<email>[, ...]")
string$msgMessage
?string[]$filename_listList of files to attach (full path of filename on file system)
?string[]$mimetype_listList of MIME type of attached files
?string[]$mimefilename_listList of attached file name in message
string$addr_ccEmail cc (Example: 'abc@d.nosp@m.ef.c.nosp@m.om, ghk@l.nosp@m.mn.c.nosp@m.om')
string$addr_bccEmail bcc (Note: This is autocompleted with MAIN_MAIL_AUTOCOPY_TO if defined)
int<0,1>$deliveryreceipt Ask a delivery receipt
int<-1,1>$msgishtml 1=String IS already html, 0=String IS NOT html, -1=Unknown make autodetection (with fast mode, not reliable)
string$errors_toEmail for errors-to
string|array<string,string>$css Css option (should be array, legacy: empty string if none)
string$trackidTracking string (contains type and id of related element)
string$moreinheaderMore in header. $moreinheader must contains the "\r\n" at end of each line
string$sendcontext'standard', 'emailing', 'ticket', 'password', ... (used to define which sending mode and parameters to use)
string$replytoReply-to email (will be set to the same value than From by default if not provided)
string$upload_dir_tmpTemporary directory (used to convert images embedded as img src=data:image)
string$in_reply_toMessage-ID of the message we reply to
string$referencesString with list of Message-ID of the thread ('<123> <456> ...')

Definition at line 229 of file CMailFile.class.php.

References $conf, $dolibarr_main_url_root, ascii_check(), buildCSS(), checkIfHTML(), dol_nl2br(), dol_sanitizeEmail(), dol_string_unaccent(), dol_syslog(), dol_textishtml(), encodetorfc2822(), findHtmlImages(), findHtmlImagesIsSrcData(), getArrayAddress(), getDolGlobalString(), getValidAddress(), write_body(), write_files(), write_mimeheaders(), and write_smtpheaders().

Member Function Documentation

◆ _encode_file()

CMailFile::_encode_file ( $sourcefile)
private

Read a file on disk and return encoded content for emails (mode = 'mail')

Parameters
string$sourcefilePath to file to encode
Returns
int<-1,-1>|string Return integer <0 if KO, encoded string if OK

Definition at line 1547 of file CMailFile.class.php.

References dol_osencode(), and dol_syslog().

Referenced by write_files().

◆ buildCSS()

CMailFile::buildCSS ( )

Build a css style (mode = all) into this->styleCSS and this->bodyCSS.

Returns
void

Definition at line 1685 of file CMailFile.class.php.

Referenced by __construct().

◆ check_server_port()

CMailFile::check_server_port ( $host,
$port )

Try to create a socket connection.

Parameters
string$hostAdd ssl:// for SSL/TLS.
int$portExample: 25, 465
Returns
int|array<string,int|string> Socket id if OK, = 0 if KO

Definition at line 2068 of file CMailFile.class.php.

References checkSmtpTargetAllowed(), dol_syslog(), getDolGlobalString(), server_parse(), and utf8_check().

◆ checkIfHTML()

CMailFile::checkIfHTML ( $msg)

Correct an incomplete html string.

Parameters
string$msgString
Returns
string Completed string

Definition at line 1659 of file CMailFile.class.php.

Referenced by __construct(), and write_body().

◆ checkSmtpTargetAllowed()

CMailFile::checkSmtpTargetAllowed ( $host,
$port )

Check that the SMTP target (host and port) is allowed.

Only standard SMTP ports are allowed and private or reserved IPs are refused, unless the variable $dolibarr_mail_allow_unrestricted_smtp_target is defined into the conf.php file (for example to use an internal SMTP relay). This is used to prevent the email setup to be used to scan the internal network.

Parameters
string$hostHost name or IP. Can contain a protocol prefix like ssl:// and a port is not expected here.
int | string$portPort number, example: 25, 465
Returns
string Empty string if allowed, error message if refused

Definition at line 2004 of file CMailFile.class.php.

References isIPAllowed(), and resolveDns().

Referenced by check_server_port(), and sendfile().

◆ dump_mail()

CMailFile::dump_mail ( )

Write content of a SMTP request into a dump file (mode = all) Used for debugging.

Note that to see full SMTP protocol, you can use tcpdump -w /tmp/smtp -s 2000 port 25"

Returns
void

Definition at line 1571 of file CMailFile.class.php.

References archiveOrBackupFile(), dolChmod(), and getDolGlobalInt().

Referenced by sendfile().

◆ encodetorfc2822()

static CMailFile::encodetorfc2822 ( $stringtoencode)
static

Encode subject according to RFC 2822 - http://en.wikipedia.org/wiki/MIME#Encoded-Word.

Parameters
string$stringtoencodeString to encode
Returns
string string encoded

Definition at line 1534 of file CMailFile.class.php.

References $conf.

Referenced by __construct(), and sendfile().

◆ findHtmlImages()

CMailFile::findHtmlImages ( $images_dir)
private

Search images into html message and init array this->images_encoded if found This is called in the CMailFile constructor but only if MAIN_MAIL_ADD_INLINE_IMAGES_IF_IN_MEDIAS is set (Off by default)

Parameters
string$images_dirPath to store physical images files. For example $dolibarr_main_data_root.'/medias'
Returns
int >0 if OK, <0 if KO

Definition at line 2187 of file CMailFile.class.php.

References dol_hash().

Referenced by __construct().

◆ findHtmlImagesIsSrcData()

CMailFile::findHtmlImagesIsSrcData ( $images_dir)
private

Search images with src="data:image..." format into html message.

If we find some, we create them on disk. This is called in the CMailFile constructor.

Parameters
string$images_dirLocation of where to store physically images files. For example $dolibarr_main_data_root.'/medias'
Returns
int >0 if OK, <0 if KO

Definition at line 2277 of file CMailFile.class.php.

References $conf, dol_is_dir(), dol_is_file(), dol_mkdir(), dol_syslog(), and dolChmod().

Referenced by __construct().

◆ getArrayAddress()

static CMailFile::getArrayAddress ( $address)
static

Return a formatted array of address string for SMTP protocol.

Parameters
string$addressExample: 'John Doe john@.nosp@m.doe..nosp@m.com, Alan Smith alan@.nosp@m.smit.nosp@m.h.com' or 'john@.nosp@m.doe..nosp@m.com, alan@.nosp@m.smit.nosp@m.h.com'
Returns
array<string,?string> array(email => name)
See also
getValidAddress()

Definition at line 2462 of file CMailFile.class.php.

References getDolGlobalString().

Referenced by __construct().

◆ getValidAddress()

static CMailFile::getValidAddress ( $address,
$format,
$encode = 0,
$maxnumberofemail = 0 )
static

Return a formatted address string for SMTP protocol.

Parameters
string$addressExample: 'John Doe john@.nosp@m.doe..nosp@m.com, Alan Smith alan@.nosp@m.smit.nosp@m.h.com' or 'john@.nosp@m.doe..nosp@m.com, alan@.nosp@m.smit.nosp@m.h.com'
int$formatSet to 0 = auto, 1 = emails with <>, 2 = emails without <>, 3 = auto + label between "", 4 label or email, 5 mailto link
int$encodeSet to 0 = No encode name, 1 = Encode name to RFC2822
int$maxnumberofemailSet to 0 = No limit. Otherwise, maximum number of emails returned ($address may contains several email separated with ','). Add '...' if there is more.
Returns
string If format 0: 'john@.nosp@m.doe..nosp@m.com' or 'John Doe john@.nosp@m.doe..nosp@m.com' or '=?UTF-8?B?Sm9obiBEb2U=?= john@.nosp@m.doe..nosp@m.com' If format 1: 'john@.nosp@m.doe..nosp@m.com' If format 2: 'john@.nosp@m.doe..nosp@m.com' If format 3: 'john@.nosp@m.doe..nosp@m.com' or '"John Doe" john@.nosp@m.doe..nosp@m.com' or '"=?UTF-8?B?Sm9obiBEb2U=?=" john@.nosp@m.doe..nosp@m.com' If format 4: 'John Doe' or 'john@.nosp@m.doe..nosp@m.com' if no label exists If format 5: John Doe or john@doe.com if no label exists
See also
getArrayAddress()

Definition at line 2393 of file CMailFile.class.php.

References getDolGlobalString().

Referenced by __construct(), sendfile(), Contrat\sendReminderForExpiredServices(), Adherent\sendReminderForExpiredSubscription(), and showDirectPublicLink().

◆ save_dump_mail_in_err()

CMailFile::save_dump_mail_in_err ( $message = '')

Save content if mail is in error Used for debugging.

Parameters
string$messageAdd also a message
Returns
void

Definition at line 1618 of file CMailFile.class.php.

References dol_move(), dol_now(), dol_print_date(), dolChmod(), and getDolGlobalString().

Referenced by sendfile().

◆ sendfile()

CMailFile::sendfile ( )

Send mail that was prepared by constructor.

Returns
bool True if mail sent, false otherwise. Negative int if error in hook. String if incorrect send mode.

@phan-suppress PhanTypeMismatchReturnNullable False positif by phan for unclear reason.

Definition at line 858 of file CMailFile.class.php.

References $conf, ascii_check(), checkSmtpTargetAllowed(), dol_syslog(), dump_mail(), encodetorfc2822(), getDolGlobalInt(), getDolGlobalString(), getSupportedOauth2Array(), getValidAddress(), and save_dump_mail_in_err().

◆ server_parse()

CMailFile::server_parse ( $socket,
$response )

This function has been modified as provided by SirSir to allow multiline responses when using SMTP Extensions.

Parameters
resource$socketSocket
string$responseResponse string
Returns
boolean true if success

Definition at line 2159 of file CMailFile.class.php.

Referenced by check_server_port().

◆ write_body()

CMailFile::write_body ( $msgtext)

Return email content (mode = 'mail')

Parameters
string$msgtextMessage string
Returns
string String content

Definition at line 1824 of file CMailFile.class.php.

References $conf, checkIfHTML(), getDolGlobalString(), and write_images().

Referenced by __construct().

◆ write_files()

CMailFile::write_files ( $filename_list,
$mimetype_list,
$mimefilename_list,
$cidlist )
private

Attach file to email (mode = 'mail')

Parameters
string[]$filename_listTableau
string[]$mimetype_listTableau
string[]$mimefilename_listTableau
?string[]$cidlistArray of CID if file must be completed with CID code
Returns
string|int<-1,-1> String with files encoded or -1 when error

Definition at line 1920 of file CMailFile.class.php.

References _encode_file(), and dol_syslog().

Referenced by __construct().

◆ write_images()

CMailFile::write_images ( $images_list)

Attach an image to email (mode = 'mail')

Parameters
array<array{name:string,fullpath:string,content_type:string,cid:string,image_encoded:string}>$images_list Array of array image
Returns
string String with encoded images

Definition at line 1970 of file CMailFile.class.php.

References dol_syslog().

Referenced by write_body().

◆ write_mimeheaders()

CMailFile::write_mimeheaders ( $filename_list,
$mimefilename_list )

Create header MIME (mode = 'mail')

Parameters
?string[]$filename_listArray of filenames
?string[]$mimefilename_listArray of mime types
Returns
string mime headers

Definition at line 1796 of file CMailFile.class.php.

References dol_syslog().

Referenced by __construct().

◆ write_smtpheaders()

CMailFile::write_smtpheaders ( )

Create SMTP headers (mode = 'mail')

Returns
string headers

Definition at line 1711 of file CMailFile.class.php.

References dol_syslog(), and getDolGlobalString().

Referenced by __construct().


The documentation for this class was generated from the following file: