Search code examples
phpcodeigniteremailphpmailer

I cannot send pdf attachment with my phpmailer


I am having an attachment problem with my php mailer. All the informations (emails, names, pdf_link etc) are correct and I can receive the email but there is no pdf in the attachment.

Could you please check my codes and tell my what my mistake is, I hope you can help me.

//mailing starts

  $mail = new PHPMailer();

  $mail->isSMTP();
  $mail->Timeout = 20;
  $mail->SMTPDebug = 0;
  //Set the hostname of the mail server
  $mail->Host = 'my-host-information';

  $mail->Port = 465;//587
  $mail->SMTPSecure = 'ssl';//tls
  $mail->SMTPAuth = true;
  $mail->Username = "myemail@mywebsite.com";
  
  $mail->Password = "****";
  //Set who the message is to be sent from
  $mail->setFrom('myemail@mywebsite.com', 'Name here');
  $mail->addAddress($member_email, '');
  $mail->AddAttachment($pdf_link, $name = $image_name, $encoding = 'base64', $type = 'application/pdf');

        $message_it = 'Grazie per il tuo interesse. La tua fattura proforma è stata predisposta e la trovi in allegato. Puoi visualizzarlo sul nostro sistema .<br><br>Cordiali Saluti,<br>My Company';

        $message_en = 'Thanks for your interest. Your proforma invoice has been prepared and you can find it attached. You can view it on our system. <br> <br> Best Regards, <br> My Company ';

        $combined_name_it = 'Caro ' .$member_name.' '. $member_company;
        $combined_email = $email;
        $combined_message_it = $message_it;

        $combined_name_en = 'Dear ' .$member_name.' '. $member_company;
        $combined_email = $email;
        $combined_message_en = $message_en;

  if($email_lang == 'it'){
  $mail->isHTML(true);
      $mail->Subject = 'La tua proforma è pronta ';         // Email subject
      $mail->Body    = "$combined_name_it,$combined_email<br /><br />$combine_message_it";              // Mail Content
    $mail->CharSet = 'utf-8';
  }else{
    $mail->isHTML(true);
        $mail->Subject = 'Your proforma is ready.';         
        $mail->Body    = "$combined_name_en<br />$combined_email<br /><br />$combined_message_en";  
      $mail->CharSet = 'utf-8';
  }


  $result = $mail->send();

  if($result){
    $session = \Config\Services::session();
    $session->setFlashdata('success', 'Proforma updated');
    return redirect()->to($_SERVER['HTTP_REFERER']);
  }else{
      $data['validation'] = $this->validator;
      $data['title'] = 'Proforma Update';
      echo view('admin/assets/header', $data);
      return redirect()->to($_SERVER['HTTP_REFERER']);
      }


  //mailing ends

Solution

  • After solving the issue, I can say that the problem was PhpMailer expected to see the folder part of the url, not the base one. When I included base_url() which includes http:// mywebsites.com etc part, it kept sending the email without the attachment. But, when I replaced it with the folder part, let's say "uploads/invoices/invoice-1.pdf" it worked.

    Thanks @Synchro