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

使用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 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值