PHPMailer::addAttachment PHP Method

addAttachment() public method

Returns false if the file could not be found or read.
public addAttachment ( string $path, string $name = '', string $encoding = 'base64', string $type = '', string $disposition = 'attachment' ) : boolean
$path string Path to the attachment.
$name string Overrides the attachment name.
$encoding string File encoding (see $Encoding).
$type string File extension (MIME) type.
$disposition string Disposition to use
return boolean
    public function addAttachment($path, $name = '', $encoding = 'base64', $type = '', $disposition = 'attachment')
    {
        try {
            if (!@is_file($path)) {
                throw new phpmailerException($this->lang('file_access') . $path, self::STOP_CONTINUE);
            }
            // If a MIME type is not specified, try to work it out from the file name
            if ($type == '') {
                $type = self::filenameToType($path);
            }
            $filename = basename($path);
            if ($name == '') {
                $name = $filename;
            }
            $this->attachment[] = array(0 => $path, 1 => $filename, 2 => $name, 3 => $encoding, 4 => $type, 5 => false, 6 => $disposition, 7 => 0);
        } catch (phpmailerException $exc) {
            $this->setError($exc->getMessage());
            $this->edebug($exc->getMessage());
            if ($this->exceptions) {
                throw $exc;
            }
            return false;
        }
        return true;
    }

Usage Example

Example #1
1
 function send()
 {
     $registry = Registry::getInstance();
     $site_root_absolute = $registry->get('site_root_absolute');
     $mail = new PHPMailer();
     #        $mail->SMTPDebug = 3;                               // Enable verbose debug output
     $mail->isSMTP();
     // Set mailer to use SMTP
     $mail->Host = 'smtp-relay.gmail.com;smtp.gmail.com';
     // Specify main and backup SMTP servers
     #        $mail->Host = 'smtp.gmail.com';                       // Specify main and backup SMTP servers
     $mail->SMTPAuth = true;
     // Enable SMTP authentication
     $mail->Username = '******';
     // SMTP username
     $mail->Password = '******';
     // SMTP password
     $mail->SMTPSecure = 'tls';
     // Enable TLS encryption, `ssl` also accepted
     $mail->Port = 587;
     // TCP port to connect to
     $mail->isHTML(true);
     // Set email format to HTML
     $mail->setFrom($this->from);
     $mail->addAddress($this->to);
     // Add a recipient
     #        $mail->addAddress('*****@*****.**');               // Name is optional
     if ($this->reply) {
         $mail->addReplyTo($this->reply);
     }
     #        $mail->addCC('*****@*****.**');
     $mail->addBCC('*****@*****.**');
     $mail->Subject = $this->subject;
     $mail->Body = $this->html;
     $mail->AltBody = $this->text;
     if (is_array($this->attachments)) {
         foreach ($this->attachments as $attach) {
             $mail->addAttachment($site_root_absolute . self::BASE_PATH . $this->id . EmailTemplateAttahchment::PATH . $attach->filename);
         }
     }
     if (is_array($this->images)) {
         foreach ($this->images as $image) {
             $mail->addAttachment($site_root_absolute . self::BASE_PATH . $this->id . EmailTemplateEmbedded::PATH . $image->filename, $image->cid, 'base64', null, 'inline');
         }
     }
     foreach ($this->makeHeaders($this->headers) as $name => $value) {
         $mail->addCustomHeader($name, $value);
     }
     $result = $mail->send();
     if (!$result) {
         $this->errorInfo = $mail->ErrorInfo;
     }
     return $result;
 }
All Usage Examples Of PHPMailer::addAttachment