简体   繁体   中英

Cannot send email attachment PDF with CakePHP Mail

Anyone can help me please to solve this problem. Im trying to send PDF attachment with CakePHP. I think all thing should be work fine. But why I get email result like in screenshoot?

在此处输入图片说明

My code to send pdf:

function send_notif_email($email=null,$template='default',$data=null,$subject='Notification',$noreplay=true,$attachment=array()) {
    $Email = new CakeEmail('default');
    $emailData = $data;

    if($Email->template($template)
    ->emailFormat('html')
    ->to($email)
    ->from(($noreplay == true)?Configure::read('Meta.noreplay'):Configure::read('Meta.email'))
    ->replyTo(($noreplay == true)?Configure::read('Meta.noreplay'):Configure::read('Meta.email'))
    ->setHeaders(array(
        'From' => ($noreplay == true)?Configure::read('Meta.noreplay'):Configure::read('Meta.email'), 
        'Reply-To' => ($noreplay == true)?Configure::read('Meta.noreplay'):Configure::read('Meta.email'),
        'MIME-Version' => '1.0', 
        'Content-type' => 'text/html; charset=iso-8859-1', 
    ))
    ->attachments($attachment)
    ->subject($subject)
    ->viewVars(compact('emailData')) /*set data*/
    ->send()) {
        return true;
    }

    return false;
}

I just check all attachment array and filepath, all is fine:

array(
        'penawaran_0001_VN_SPN_X_2015' => '/home3/salesku/public_html/penawaran-online/app/webroot/upload/offer/00001/penawaran_0001_VN_SPN_X_2015.pdf',
        'X100.pdf' => '/home3/salesku/public_html/penawaran-online/app/webroot/upload/attachment/00001/X100.pdf'
    )

Thanks for help.

I think I just found the problem. It looks likely setHeader should removed from method send_notif_email.

Not its work with new code:

function send_notif_email($email=null,$template='default',$data=null,$subject='Notification',$noreplay=true,$attachment=array()) {
    $Email = new CakeEmail('default');
    $emailData = $data;

    if($Email->template($template)
    ->emailFormat('html')
    ->to($email)
    ->from(($noreplay == true)?Configure::read('Meta.noreplay'):Configure::read('Meta.email'))
    ->replyTo(($noreplay == true)?Configure::read('Meta.noreplay'):Configure::read('Meta.email'))
    ->attachments($attachment)
    ->subject($subject)
    ->viewVars(compact('emailData')) /*set data*/
    ->send()) {
        return true;
    }

    return false;
}

Thanks for all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM