Skip to content
Advertisement

Use PHPmailer to send attachment

I’m using PHPmailer library to send emails and I need to send an attachment.

Currently the file is stored on the server and I have the link to the file stored in var $link.

I’m using the following to try and add the attachment but the email arrives with no attachment:

$phpmailer->AddAttachment('$link');

I have also tried it with a hardcoded path instead of my variable:

$phpmailer->AddAttachment('http://mysite.co.uk/link/to/my/file.pdf');

The email shows up but does not have any attachment.

I have tried using phpmailerException to grab any errors but get no response.

How else can I send an attachment with PHPmailer?

Full PHP code:

require_once 'library/PHPMailer.php';
        $phpmailer = new PHPMailer();
        $phpmailer->isSMTP(); //switch to smtp
        $phpmailer->Host = 'smtp.sendgrid.net';
        $phpmailer->SMTPAuth = true;
        $phpmailer->SMTPSecure = 'ssl';
        $phpmailer->Port = 465;
        $phpmailer->Username = 'username';
        $phpmailer->Password = 'password';
        $phpmailer->AddAttachment('http://mysite.co.uk/blah/cv.pdf');
        //$phpmailer->AddAttachment('$link');

        $subject = 'Job Application';

        $messageConfirmation = "The <b> 'Apply for Job' </b> form has recently been completed on <b>www.mysite.com.</b><br/><br/> <b>Please see contact details below:<br /><br/> </b>";
        $messageConfirmation.= "<b>Applying for:</b> $title <br/><br/>";
        $messageConfirmation .= "<b>Name:</b> $name <br/><br/><b>Surname:</b> $surname <br/><br/> <b>Email:</b> $email <br/><br/> <b>Comment:</b> $comment <br/><br/>";
        $messageConfirmation .= "<b>CV:</b> $link<br /><br />";
        $messageConfirmation .= "Please can you call them back in the next 24 hours. <br/><br/> Thank you nnMy Site<br/><br/>";


        $imgheader = 'http://blah.co.uk/blah/wp-content/uploads/2014/11/header.png';
        $imgfooter = 'http://blah.co.uk/blah/wp-content/uploads/2014/11/footer.png';

        $message = '<!DOCTYPE HTML>'.
            '<head>'.
            '<meta http-equiv="content-type" content="text/html">'.
            '<title>Email notification</title>'.
            '</head>'.
            '<body>'.
            '<div id="header" style="width: 600px;height: auto;margin: 0 auto;color: #fff;text-align: center;font-family: Open Sans,Arial,sans-serif;">'.
            '<img src="'.$imgheader.'" >'.
            '</div>'.

            '<div id="outer" style="width: 600px;margin: 0 auto;margin-top: 10px;">'.
            '<div id="inner" style="width: 600px;margin: 0 auto; padding-left: 20px;background-color: #fff;font-family: Open Sans,Arial,sans-serif;font-size: 13px;font-weight: normal;color: #444;margin-top: 10px;">'.
            '<p>'.$messageConfirmation .'</p>'.

            '</div>'.
            '</div>'.

            '<div id="footer" style="width: 600px;height: auto;margin: 0 auto;text-align: center;padding: 10px;font-family: Verdena;">'.
            '<img src="'.$imgfooter.'" >'.
            '</div>'.
            '</body>';


        $phpmailer->IsHTML(true);
        $phpmailer->AddAddress(CONTACTUSFORMEMAIL);
        $phpmailer->From = CONTACTUSFORMEMAIL;
        $phpmailer->FromName ='blah';
        $phpmailer->WordWrap   = 50; // set word wrap to 50 characters
        $phpmailer->Subject    = $subject;
        $phpmailer->Body       = $message;
        $phpmailer->Send();



        $data['success'] = true;
        $data['message'] = 'Thank you for your application';

Advertisement

Answer

The Problem is that the function AddAttachment uses the function is_file() and is file returns false when accessing URLs (this also applies to relative URLs), is_file() only accepts filepaths. So normally phpmailer should add an error to the error container.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement