Back to Mail class

Method addAttachment

public \Joomla\CMS\Mail\Mail|bool
addAttachment
(mixed $path, mixed $name = '', mixed $encoding = 'base64', mixed $type = 'application/octet-stream', mixed $disposition = 'attachment')
Add file attachment to the email
Parameters
  • mixed $path Either a string or array of strings [filenames]
  • mixed $name Either a string or array of strings [names]. N.B. if this is an array it must contain the same number of elements as the array of paths supplied.
  • mixed $encoding The encoding of the attachment
  • mixed $type The mime type
  • string $disposition The disposition of the attachment
Returns
  • \Joomla\CMS\Mail\Mail|bool Returns this object for chaining on success or boolean false on failure when exception throwing is disabled.
Since
  • 3.0.1
-
  • \InvalidArgumentException if the argument array counts do not match
  • \PHPMailer\PHPMailer\Exception if setting the attachment failed and exception throwing is enabled
Class: Mail
Project: Joomla

Method addAttachment - Source code

/**
 * Add file attachment to the email
 *
 * @param   mixed   $path         Either a string or array of strings [filenames]
 * @param   mixed   $name         Either a string or array of strings [names]. N.B. if this is an array it must contain the same
 *                                number of elements as the array of paths supplied.
 * @param   mixed   $encoding     The encoding of the attachment
 * @param   mixed   $type         The mime type
 * @param   string  $disposition  The disposition of the attachment
 *
 * @return  Mail|boolean  Returns this object for chaining on success or boolean false on failure when exception throwing is disabled.
 *
 * @since   3.0.1
 * @throws  \InvalidArgumentException  if the argument array counts do not match
 * @throws  phpmailerException 			if setting the attachment failed and exception throwing is enabled
 */
public function addAttachment($path, $name = '', $encoding = 'base64', $type = 'application/octet-stream', $disposition = 'attachment')
{
    // If the file attachments is an array, add each file... otherwise just add the one
    if (isset($path)) {
        $result = true;
        if (\is_array($path)) {
            if (!empty($name) && \count($path) != \count($name)) {
                throw new \InvalidArgumentException('The number of attachments must be equal with the number of name');
            }
            foreach ($path as $key => $file) {
                if (!empty($name)) {
                    $result = parent::addAttachment($file, $name[$key], $encoding, $type);
                } else {
                    if (!empty($name)) {
                        $result = parent::addAttachment($file, $name[$key], $encoding, $type, $disposition);
                    } else {
                        $result = parent::addAttachment($file, $name, $encoding, $type, $disposition);
                    }
                }
            }
            // Check for boolean false return if exception handling is disabled
            if ($result === false) {
                return false;
            }
        } else {
            $result = parent::addAttachment($path, $name, $encoding, $type);
        }
        // Check for boolean false return if exception handling is disabled
        if ($result === false) {
            return false;
        }
    }
    return $this;
}