我正在使用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';
答案 0 :(得分:1)
您好请尝试以下代码
<?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 "\r\nMail not sent. " . $mail->ErrorInfo;
}
?>
答案 1 :(得分:1)
问题是函数AddAttachment使用函数is_file()并且文件在访问URL时返回false(这也适用于相对URL),is_file()只接受文件路径。所以通常phpmailer应该向错误容器添加错误。
答案 2 :(得分:0)
方法AddAttachment接收文件的路径。
如果您看到源代码,您将看到它使用fopen函数打开文件,因此,我认为它不能使用链接,您应该自己下载文件并将路径传递给功能
答案 3 :(得分:0)
使用 phpmailer 发送任何类型的附件。
<?php
require 'PHPMailer/class.phpmailer.php';
if(isset($_POST['send']))
{
$file_name = $_FILES['file']['name']; //attachment
$file_tmp =$_FILES['file']['tmp_name'];
$abc=microtime();
$path="upload/$abc".$file_name;
move_uploaded_file($file_tmp,$path); //moving attachment to upload folder
$mail = new PHPMailer(true);
$mail->IsSMTP();
$mail->SMTPAuth = false;
$mail->Port = 25;
$mail->Host = "localhost";
$mail->Username = "webmail_username";
$mail->Password = "webmail_password";
$mail->IsSendmail();
$mail->From = "abc@your.com";
$mail->FromName = "your.com";
$mail->AddAddress('receiver_emailid');
$mail->Subject = "Thank you for contacting US";
$mail->WordWrap = 80;
$body="Thank You For Contacting Us";
$mail->MsgHTML($body);
$mail->IsHTML(true);
$mail->AddAttachment($path,$file_name); //Attachment File
if(!$mail->Send())
{
echo "Mail Not Sent";
}
else
{
echo '<script language="javascript">';
echo 'alert("Thank You Contacting Us We Will Response You As Early Possible")';
echo '</script>';
}
}
了解更多详情:- click here
答案 4 :(得分:-1)
我知道这可能听起来像是一个激进的想法,但您可以尝试reading the docs,或者可能会检查函数的返回值,甚至可能尝试an up to date version of PHPMailer,或者将代码基于最近的示例。< / p>
PHPMailer只会在$mail = new PHPMailer(true);
实例化时启用异常,并且如@TheGreenKey所说,如果启用调试,它将设置错误通知并产生错误消息。
如果要将URL内容添加为附件,可以执行以下操作:
$mail->addStringAttachment(file_get_contents('http://mysite.co.uk/link/to/my/file.pdf'), 'cv.pdf');`
但是,它永远不会快速,可靠或高效。