天道酬勤,学无止境

php mailer attachments

I have been using this script to send emails to certain staff but because of changes to my system i have to now send attachements with the email and i have tried multipul peices of code to accomplish this but have been unsuccessful... I still recive the email but without the attachement which is quite pointless in this case i have placed the script i am using bellow

i have removed the real addresses i was using and smtp server

    require("PHPMailer/class.phpmailer.php");

$mail = new PHPMailer();

$mail->IsSMTP();    // set mailer to use SMTP
$mail->Host = "SMTP.SErver.com";    

$mail->From = "From@email.com";    
$mail->FromName = "HCSC";  
$mail->AddAddress("To@email.com", "Example"); 
$mail->AddReplyTo("Reply@email.com", "Hcsc"); 

$mail->WordWrap = 50;    
$mail->IsHTML(false);    

$mail->Subject = "AuthSMTP Test";
$mail->Body    = "AuthSMTP Test Message!";
$mail->AddAttachment("matt.txt"); //this is basicly what i am trying to attach as a test but will be using excel spreadsheets in the production

if(!$mail->Send())
{
   echo "Message could not be sent. <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}

echo "Message has been sent";

i have also tried a few other emtods of attaching the file but none seem to work any help is greatly appricated

评论

Your code looks fairly straightforward and syntactically correct. Is the script returning any error messages?

If you're receiving the message without any issues, then the problem doesn't look to be in your code.

A few things to check:

  • Make sure that the file "matt.txt" is both readable by your webserver and that the path is correct. The path to the file needs to be included in the $mail->AddAttachment() method call and should be relative to the script's location.
  • Verify that your mail server isn't stripping any attachments out due to restrictions and/or try sending a different attachment file type (try a .zip or a .jpg file)
  • If you're running a newer version of phpMailer, you can try catching any exceptions that are thrown (perhaps one that isn't preventing the message from going out, but just preventing the attachment from being included) using the following syntax: (taken from phpMailer Example Code)

    require 'PHPMailer/PHPMailerAutoload.php';
    $mail = new PHPMailer(true);
    try {
    
      $mail->IsSMTP();    // set mailer to use SMTP
      $mail->Host = "SMTP.SErver.com";    
    
      $mail->From = "From@email.com";    
      $mail->FromName = "HCSC";  
      $mail->AddAddress("To@email.com", "Example"); 
      $mail->AddReplyTo("Reply@email.com", "Hcsc"); 
    
      $mail->WordWrap = 50;    
      $mail->IsHTML(false);    
    
      $mail->Subject = "AuthSMTP Test";
      $mail->Body    = "AuthSMTP Test Message!";
      $mail->AddAttachment("matt.txt");
      echo "Message Sent OK<p></p>\n";
    
    } catch (phpmailerException $e) {
    
      echo $e->errorMessage(); //Pretty error messages from PHPMailer
    
    } catch (Exception $e) {
    
      echo $e->getMessage(); //Boring error messages from anything else!
    
    }
    

you need to provide the full path of the file and the filename functions excepts two/three arguments

// Setup mail class, recipients and body
$mailer->AddAttachment('/home/mywebsite/public_html/file.zip', 'file.zip');

http://www.askapache.com/php/phpfreaks-eric-rosebrocks-phpmailer-tutorial.html#File_Attachments_PHP_Mail_PHPMailer

Ok this has not closed yet so I thought since I had exactly the same problem I would give you my solution which worked for me. I believe the previous mailer had it right. You need to identify the absolute path to your file. This is not always an easy thing to do especially if you operate on a shared server. In any event I embedded the following code on my page and it echoed out the exact path to that page. From there on it was easy to work out how to drop a level into the directory that held the files I wanted to attach.

  'var_dump(stream_resolve_include_path("Thankyou.php"));'

Just copy and paste that line and put it into your page insert your page name and replace the Thankyou.php I have inserted (keep the speech marks). Run the page and retrieve your absolute path. Just delete it afterwords and insert the new path in the AddAttachment segment of PHPMailer.

受限制的 HTML

  • 允许的HTML标签:<a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • 自动断行和分段。
  • 网页和电子邮件地址自动转换为链接。

相关推荐
  • 尝试使用swift mailer,gmail smtp,php发送邮件(trying to send mail using swift mailer, gmail smtp, php)
    问题 这是我的代码: <?php require_once 'Swift/lib/swift_required.php'; $transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465) ->setUsername('me@ff.com') ->setPassword('pass'); $mailer = Swift_Mailer::newInstance($transport); $message = Swift_Message::newInstance('Wonderful Subject') ->setFrom(array('me@ff.com' => 'MY NAME')) ->setTo(array('you@ss.com' => 'YOU')) ->setBody('This is the text of the mail send by Swift using SMTP transport.'); //$attachment = Swift_Attachment::newInstance(file_get_contents('path/logo.png'), 'logo.png'); //$message->attach($attachment); $numSent = $mailer-
  • Angular $http post 请求在 PHP 中未定义(Angular $http post request undefined in PHP)
    问题 无法发出$http发布请求,在 php 中为$_POST["name"]和所有其他发布的数据未定义。 但是在我的控制台中正确打印了所有数据,你能帮我哪里出错吗? 我在触发点击事件时发送数据,我是 angular 新手,请帮我解决这个问题,感谢提前回复。 angular.element(document.querySelector('#applyJob')).unbind('click').bind('click', function () { console.log($scope.userName+$scope.userEmail+$scope.userMobileNo+$scope.subject+$scope.userCoverLetter+$scope.attach); $http({ method: "POST", url: "mailer.php", data: { name : $scope.userName, mail : $scope.userEmail, no : $scope.userMobileNo, subject : $scope.subject, message : $scope.userCoverLetter, attach : $scope.attach }, headers: {'Content-Type': 'application/x
  • PHP Mailer - 不发送超过 100 KB 附件的电子邮件(PHP Mailer - Email is not sent for more than 100 KB attachment)
    问题 我正在使用 PHPMailer 库发送电子邮件。 我已将代码上传到不同的服务器,现在电子邮件不适用于大小超过 100 KB 的附件。 它给出了错误 “无法实例化邮件功能。”。 它适用于小于 100 KB 的附件。 似乎服务器上可能有一些大小限制。 如果是这种情况,那么在哪里检查? 我使用的是 windows server ,以下是 php mailer 中的设置 $mail = new PHPMailer(); $mail->isMail(); $mail->isHTML(true); $mail->Host = 'localhost'; $mail->SMTPAuth = false; $mail->AddAddress('whoto@otherdomain.com', 'John Doe'); $mail->SetFrom('donotreply@yourdomain.com', 'First Last'); $mail->Subject = 'PHPMailer Test Subject via mail(), advanced'; $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; $mail->MsgHTML(file_get_contents(
  • 如何在 PHP 邮件功能中使用 TCPDF(How to use TCPDF with PHP mail function)
    问题 $to = 'my@email.ca'; $subject = 'Receipt'; $repEmail = 'rep@sales.ca'; $fileName = 'receipt.pdf'; $fileatt = $pdf->Output($fileName, 'E'); $attachment = chunk_split($fileatt); $eol = PHP_EOL; $separator = md5(time()); $headers = 'From: Sender <'.$repEmail.'>'.$eol; $headers .= 'MIME-Version: 1.0' .$eol; $headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\""; $message = "--".$separator.$eol; $message .= "Content-Transfer-Encoding: 7bit".$eol.$eol; $message .= "This is a MIME encoded message.".$eol; $message .= "--".$separator.$eol; $message .= "Content-Type: text/html
  • How to use TCPDF with PHP mail function
    $to = 'my@email.ca'; $subject = 'Receipt'; $repEmail = 'rep@sales.ca'; $fileName = 'receipt.pdf'; $fileatt = $pdf->Output($fileName, 'E'); $attachment = chunk_split($fileatt); $eol = PHP_EOL; $separator = md5(time()); $headers = 'From: Sender <'.$repEmail.'>'.$eol; $headers .= 'MIME-Version: 1.0' .$eol; $headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\""; $message = "--".$separator.$eol; $message .= "Content-Transfer-Encoding: 7bit".$eol.$eol; $message .= "This is a MIME encoded message.".$eol; $message .= "--".$separator.$eol; $message .= "Content-Type: text/html; charset
  • 需要带有文件附件脚本的 PHP Mailer Form 来发送带有或不带有附件的表单信息(Need PHP Mailer Form with File Attachment script to send form information with or without attachment)
    问题 我在尝试让我的脚本发送包含从表单收集的信息的文件附件时遇到了一些麻烦。 我使用带有文件附件的 PHP Mailer Form 并进行了修改以适合我的规范。 到目前为止,它会发送没有附加图像的表单信息,但是当我上传图像时,它只会发送附件而不会发送表单中的其他信息。 我需要它能够发送带有或不带有附件的表单信息。 我是 php 的初学者,所以我不确定为什么要这样做。 这是我的代码 <?php if(isset($_POST['submit'])){ $sendTo = '<myemail@somesite.com'; $subject = 'Request'; $from = $_POST['email_address']; // Allowed file types. add file extensions WITHOUT the dot. $allowtypes=array("jpg", "png", "gif"); // Require a file to be attached: false = Do not allow attachments true = allow only 1 file to be attached $requirefile="true"; // Maximum file size for attachments in KB NOT Bytes
  • trying to send mail using swift mailer, gmail smtp, php
    Here is my code: <?php require_once 'Swift/lib/swift_required.php'; $transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465) ->setUsername('me@ff.com') ->setPassword('pass'); $mailer = Swift_Mailer::newInstance($transport); $message = Swift_Message::newInstance('Wonderful Subject') ->setFrom(array('me@ff.com' => 'MY NAME')) ->setTo(array('you@ss.com' => 'YOU')) ->setBody('This is the text of the mail send by Swift using SMTP transport.'); //$attachment = Swift_Attachment::newInstance(file_get_contents('path/logo.png'), 'logo.png'); //$message->attach($attachment); $numSent =
  • PHP Mailer - Email is not sent for more than 100 KB attachment
    I am using PHPMailer library for sending email. I have uploaded code to different server and now email is not working for attachments of size more than 100 KB. It is giving error "Cannot instantiate mail function.". It works with attachments less than 100 KB. It seems that there might be some size limit on the server. If this is the case then where to check this ? I am using windows server, following are the settings in php mailer $mail = new PHPMailer(); $mail->isMail(); $mail->isHTML(true); $mail->Host = 'localhost'; $mail->SMTPAuth = false; $mail->AddAddress('whoto@otherdomain.com', 'John
  • Need PHP Mailer Form with File Attachment script to send form information with or without attachment
    I'm having some trouble trying to get my script to send a file attachment with the information collected from the form. I used PHP Mailer Form with File Attachment and modified to fit my specification. So far it will send the form information without the attached image but when I upload an image it will only send the attachment and no other information from the form. I need it to be able to send the form information with or without an attachment. I'm a beginner with php so I'm not sure why its doing this. Here is my code <?php if(isset($_POST['submit'])){ $sendTo = '<myemail@somesite.com';
  • Angular $http post request undefined in PHP
    Not able to make $http post request, getting undefined for $_POST["name"] in php and for all other posted data. but in my console printing all the data correctly, can u help me where I did mistake. I am sending data when click event is triggered, I am new to angular, please help me to solve this problem, Thanks to replies in advance. angular.element(document.querySelector('#applyJob')).unbind('click').bind('click', function () { console.log($scope.userName+$scope.userEmail+$scope.userMobileNo+$scope.subject+$scope.userCoverLetter+$scope.attach); $http({ method: "POST", url: "mailer.php", data
  • Swift mailer attachment
    I have tried email extensions in yii twice. 1.YII-MAIL 2.PHP MAILER Now i would like to try out swift mailer.I have downloaded the package from here http://swiftmailer.org/download and added it to the extensions folder in YII. Here i have a form ,with fields for name,email,phone and an attachment. I am saving the file uploaded to a folder called resumes under images folder ,at the same time i am sending a mail with all details along with the uploaded file as an attachment .But on clicking create button i am getting this error include(Swift_Message.php) [<a href='function.include'>function
  • Swift 邮件附件(Swift mailer attachment)
    问题 我已经在 yii 中尝试过两次电子邮件扩展。 1.YII-MAIL 2.PHP MAILER 现在我想试试 swift mailer。我已经从 http://swiftmailer.org/download 下载了这个包,并将它添加到 YII 的扩展文件夹中。 在这里,我有一个表单,其中包含姓名、电子邮件、电话和附件字段。 我将上传的文件保存到图像文件夹下名为 resumes 的文件夹中,同时我正在发送一封邮件,其中包含所有详细信息以及上传的文件作为附件。但是在单击创建按钮时,我收到此错误 include(Swift_Message.php) [<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory 这是我迄今为止尝试过的控制器操作 public function actionCreate() { $this->layout='static_inner'; $model=new LriCareer; // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if(isset($_POST[
  • blank pdf attachment sent using FPDF and SwiftMailer
    I am trying to create a PDF stream using FPDF library and to send the pdf over e-mail using Swift Mailer. Below is my code. The mail is sent successfully and even pdf is also attached but the pdf is blank. It has a size of 1Kb and can be opened as a pdf. My code is : <?php include('./fpdf/fpdf.php'); require_once './lib/swift_required.php'; $pdf = new FPDF(); $pdf->AddPage(); $pdf->SetFont('Arial','B',16); $pdf->Text(40, 10, "Dummy text"); $data=$pdf->Output('./emails/order.pdf', 'F'); $message = Swift_Message::newInstance('Subject') ->setFrom(array("admin@mysite.com" => 'Company Admin')) -
  • 使用 FPDF 和 SwiftMailer 发送的空白 pdf 附件(blank pdf attachment sent using FPDF and SwiftMailer)
    问题 我正在尝试使用 FPDF 库创建 PDF 流,并使用 Swift Mailer 通过电子邮件发送 pdf。 下面是我的代码。 邮件发送成功,甚至还附加了pdf,但pdf是空白的。 它的大小为 1Kb,可以以 pdf 格式打开。 我的代码是: <?php include('./fpdf/fpdf.php'); require_once './lib/swift_required.php'; $pdf = new FPDF(); $pdf->AddPage(); $pdf->SetFont('Arial','B',16); $pdf->Text(40, 10, "Dummy text"); $data=$pdf->Output('./emails/order.pdf', 'F'); $message = Swift_Message::newInstance('Subject') ->setFrom(array("admin@mysite.com" => 'Company Admin')) ->setTo('my@email.com') ->setBody('This is body text', 'text/html'); $attachment = Swift_Attachment::fromPath('./emails/order.pdf'); //$attachment =
  • How do i add the ability to attach a file on a online mailer in PHP?
    I've been now searching to find out this for a long time, but couldn't find a exact answer on my specific question. And it goes like this: How do i add the possibility to attach a file to a online emailer I've written in PHP? :) Here is the code: <?php session_start(); if( isset($_POST['submit'])) { if( $_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSION['security_code'] ) ) { $to=$_POST["to"]; $from=$_POST["from"]; $name=$_POST["name"]; $subject=$_POST["subject"]; $message=$_POST["message"]; $message=$message."\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"; $head="From: ".$from
  • 如何添加在 PHP 中的在线邮件程序上附加文件的功能?(How do i add the ability to attach a file on a online mailer in PHP?)
    问题 我现在一直在寻找这一点很长一段时间,但找不到我的具体问题的确切答案。 它是这样的:我如何添加将文件附加到我用 PHP 编写的在线电子邮件的可能性? :) 这是代码: <?php session_start(); if( isset($_POST['submit'])) { if( $_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSION['security_code'] ) ) { $to=$_POST["to"]; $from=$_POST["from"]; $name=$_POST["name"]; $subject=$_POST["subject"]; $message=$_POST["message"]; $message=$message."\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"; $head="From: ".$from."\r\n".'Reply-To: '.$From."\r\n"; $her=$head.' < '.$from.' >'; $techomag=mail($to, $subject, $message, $her); echo "Your Message Has been send successfully...:)
  • 附件间歇性工作(Attachments working intermittently)
    问题 我的 phpMailer 脚本有问题。 我可以从脚本发送电子邮件,我什至可以从脚本发送附件。 但是,它仅在某些时候有效,而在其他时候无效。 我尝试更换服务器,但我遇到了同样的问题,所以我假设这是一个编码问题。 当它不起作用时,电子邮件仍然很完整,但会从任何附件中删除。 附件肯定会在同一位置发送到服务器上,然后此脚本会发送一个附件数组,然后将其添加到消息中。 在一种情况下,相同的代码和相同的文件将发送但不发送! 非常混淆! 通常有些会发送一段时间,然后有些不会发送一段时间。 这是代码: <?php include('connection.php'); require_once('PHPMailer/PHPMailerAutoload.php'); class Mailer extends PHPMailer { public function copyToFolder($folderPath) { $message = $this->MIMEHeader . $this->MIMEBody; $path = "INBOX" . (isset($folderPath) && !is_null($folderPath) ? ".".$folderPath : ""); // Location to save the email $imapStream = imap_open("
  • Attachments working intermittently
    I am having issues with my phpMailer script. I can email from the script, I can even send attachments from the script. However, it only works SOMETIMES and not at other times. I tried changing servers and I have the same issues so I am assuming that it is a coding issue. when It does not work, the emails still go thorough but it is stripped from any attachments. The attachments are definitely making it onto the server in the same location and then this script is sent an array of attachments which it then adds onto the message. In one instance the same code and same files will send and not send
  • 不使用 SMTP 的 PHPmailer(PHPmailer without using SMTP)
    问题 我已将 PHPMailer 文件夹添加到我的 Web 服务器上的根文件夹中,但我收到了一个与 SMTP 相关的错误。 有没有办法在不需要登录邮件帐户的情况下使用 PHPMailer? 我可以让服务器将电子邮件发送到一个地址吗? 我在搜索这个网站,发现了这个 $email = new PHPMailer(); $email->From = 'you@example.com'; $email->FromName = 'Your Name'; $email->Subject = 'Message Subject'; $email->Body = $bodytext; $email->AddAddress( 'destinationaddress@example.com' ); $file_to_attach = 'PATH_OF_YOUR_FILE_HERE'; $email->AddAttachment( $file_to_attach , 'NameOfFile.pdf' ); return $email->Send(); 但我收到一条错误"Message could not be sent.Mailer Error: You must provide at least one recipient email address." ,指出"Message could not be
  • PHPMailer Mailer错误:消息正文为空(PHPMailer Mailer Error: Message body empty)
    问题 我正在尝试获取适用于PHPMailer的基本示例。 我所做的就是上传整个PHPMailer_5.2.2文件夹,根据您看到的代码配置下面的页面,并且我不断收到Mailer Error: Message body empty ,但我可以清楚地看到contents.html文件中包含html,并且是'没空。 这是我从PHPMailer使用的示例文件PHPMailer_5.2.2 / examples / test_smtp_gmail_basic.php 我尝试使用Outlook中适用于Gmail的设置正常工作,我知道我的用户名和密码,SMTP端口为587,并将其设置为TLS,但尝试在下面的代码中将TLS替换为SSL,但仍然出现相同的错误。 我还尝试了以下建议的代码: 改变了这个: $mail->MsgHTML($body); 对此 $mail->body = $body; 我仍然遇到相同的错误,还需要配置其他内容吗? 这是我第一次使用PHPMailer,我可以使用标准的php邮件,但是我想尝试一下,因为我要发送电子邮件的页面包含很多html,并且我不想遍历所有html并输入字符转义符,以便有人推荐使用。 <?php //error_reporting(E_ALL); error_reporting(E_STRICT); date_default_timezone_set(