PHP Mail Attachments

I use this script to send emails to specific personnel, but due to changes in my system I should now send the attachΓ© via email, and I tried using multi-level code snippets to accomplish this, but were unsuccessful. I still get the letter, but without the attachment, which is completely pointless in this case, I posted the script, I use below

I deleted the real addresses that I used 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 also tried several other emtods to attach the file, but none of them work. Help is very convenient.

+1
source share
3 answers

Your code looks pretty simple and syntactically correct. Does the script return error messages?

If you receive the message without any problems, the problem is not in your code.

A few things to check:

  • Make sure the "matt.txt" file is read by your web server and that the path is correct. The file path must be included in the call to the $ mail-> AddAttachment () method and must be relative to the location of the script.
  • Make sure your mail server does not delete attachments due to restrictions and / or tries to send a different type of attachment file (try the .zip or .jpg file).
  • If you are using a newer version of phpMailer, you can try to catch all the exceptions that have been selected (perhaps this does not prevent the message from being output, but simply prevents the attachment from being included) using the following syntax: ( taken from the 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! } 
+5
source

you need to specify the full path to the file and the file name function, with the exception of 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

+2
source

Well, this is not closed yet, so I thought, since I had the same problem, I would give you my solution, which worked for me. I believe the previous postman was right. You need to determine the absolute path to your file. This is not always easy to do, especially if you are running a shared server. In any case, I put the following code on my page, and he repeated the exact path to this page. From there it was easy to figure out how to reset the level to the directory in which the files that I would like to add are stored.

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

Just copy and paste this line and put it on your page, paste the name of your page and replace the Thankyou.php that I pasted (keep the voice tags). Start the page and get the absolute path. Just remove its afterword and insert the new path into the AddAttachment segment of PHPMailer.

+1
source

All Articles