douxiandiyo58855
douxiandiyo58855
2015-01-23 12:04
浏览 27
已采纳

使用PHPmailer发送附件

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 

My 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';
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • dongtang4954
    dongtang4954 2015-01-23 12:47
    已采纳

    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.

    点赞 评论
  • dtpoius74857
    dtpoius74857 2015-01-23 12:17

    The method AddAttachment receives the path of the file.

    If you see the source code, you will see that it uses fopen function to open the file, so, I think it doesn't work with links, you should download the file by yourself and pass the path to the function.

    点赞 评论
  • douqiang6448
    douqiang6448 2015-01-23 12:40

    Hi Please try the following code

    <?php
            $mail = new PHPMailer();
            $mail->From = $email_from;
            $mail->SetFrom($email_from, $name_from);
            $mail->AddAddress($email_to, $name_to);
            $mail->AddReplyTo($email_from, $name_from);
            $mail->MsgHTML($message_goes_here);
            $mail->Subject = $your_subject;
            $mail->AddAttachment($file_name_goes_here);
            if($mail->Send()) {
            } else {
                echo "
    Mail not sent. " .  $mail->ErrorInfo;
            }
    ?>
    
    点赞 评论
  • doubu1970
    doubu1970 2015-01-23 13:09

    I know it might sound like a radical idea, but you could try reading the docs, or perhaps checking return values of functions, maybe even trying an up to date version of PHPMailer, or basing your code on a recent example.

    PHPMailer will only throw exceptions if you enable them at instantiation with $mail = new PHPMailer(true);, and as @TheGreenKey said, it will set an error notice and produce an error message if you enable debugging.

    If you want to add URL contents as an an attachment, you can do this:

    $mail->addStringAttachment(file_get_contents('http://mysite.co.uk/link/to/my/file.pdf'), 'cv.pdf');`
    

    That's never going to be fast, reliable or efficient though.

    点赞 评论

相关推荐