简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM