OzzC
OzzC

Reputation: 821

using phpmailer class to send attachment in email: Could not access file

I have this function below "sendMail", to send emails using phpmailer class. And its working fine, Im already sending emails with sucess.

But now Im trying to add one attachment to my function, so I added this to my code:

$mail->AddAttachment(BASE.'/'.'images/2014/05/image4.jpg','image 4');

And Im having this error:

Could not access file: localhost/project/images/2014/05/image4.jpg

But this directory is exactly the correct directory to my image, so I dont understand why this error is happening.

Somebody there knows if Im missing something here?

My function to send email:

 function sendMail($subject,$message,$emissor,$emissorName,$receptor,$receptorName, $reply = NULL, $replyName = NULL){

    require_once('mail/class.phpmailer.php'); 

    $mail = new PHPMailer(); 
    $mail->IsSMTP(); 
    $mail->SMTPAuth = true; 
    $mail->IsHTML(true);
    $mail->SMTPSecure = "tls"; 
    $mail->Host = MAILHOST; 
    $mail->Port = MAILPORT; 
    $mail->Username = MAILUSER;
    $mail->Password = MAILPASS;    
    $mail->From = utf8_decode($emissor); 
    $mail->FromName = utf8_decode($emissorName); 
    $mail->Subject = utf8_decode($subject); 
    $mail->Body = utf8_decode($message);
    $mail->AddAddress(utf8_decode($receptor),utf8_decode($receptorName)); 
    $mail->AddAttachment(BASE.'/'.'images/2014/05/image4.jpg','image 4');

    if($reply != NULL){
    $mail->AddReplyTo(utf8_decode($reply),utf8_decode($replyNome)); 
    }

    if($mail->Send()){
    return true;
    }
    else{
    return false;
    }
}

And my server acess file, when I acess this url: htttp://localhost/projeto/banner-imagens/2014/05/bib_idh.jpg, I get my image!

Upvotes: 0

Views: 5068

Answers (1)

dmullings
dmullings

Reputation: 7200

Your path is incorrect. Remove BASE and replace it with the actual file path to the project folder. Put in the full path or relative path based on the script.

Add an attachment from a path on the filesystem. http://phpmailer.github.io/PHPMailer/classes/PHPMailer.html#method_addAttachment

IF that doesn't fix it also make sure the file is readable. Meaning the file permissions are set so that web server can access the file.

Upvotes: 1

Related Questions