我正在使用PHPmailer库发送电子邮件,并且需要发送附件。
目前,该文件存储在服务器上,我具有指向var中存储的文件的链接$link
。
我正在使用以下内容尝试添加附件,但是电子邮件中没有附件:
$phpmailer->AddAttachment('$link');
我也尝试过使用硬编码路径而不是变量进行尝试:
$phpmailer->AddAttachment('http://mysite.co.uk/link/to/my/file.pdf');
电子邮件出现了,但是没有任何附件。
我曾尝试使用phpmailerException
任何错误,但没有得到回应。
我还能如何使用PHPmailer发送附件?
完整的PHP代码:
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 \n\nMy 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';
问题在于,函数AddAttachment使用函数is_file(),并且在访问URL时文件返回false(这也适用于相对URL),is_file()仅接受文件路径。因此,通常phpmailer应该向错误容器添加错误。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句