php邮件附件 [英] php mailer attachments

查看:143
本文介绍了php邮件附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用这个脚本来发送电子邮件给某些工作人员,但由于我的系统的更改,我现在必须发送附件与电子邮件,我已经尝试了多重代码来完成这一点,但一直没有成功...我仍然收录电子邮件,但没有附件,这是非常无意义的在这种情况下,我已经放置脚本我正在使用下面

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

我已经删除了我正在使用的真正的地址和smtp服务器

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.

需要检查的几件事:


  • 确保文件matt .txt可以被您的网络服务器读取,路径是正确的。文件的路径需要包含在 $ mail-> AddAttachment()方法调用中,并且应该与脚本的位置相关。

  • 验证您的邮件服务器不会因为限制而剥离任何附件,和/或尝试发送不同的附件文件类型(尝试.zip或.jpg文件)

  • 重新运行较新版本的phpMailer,您可以尝试捕获抛出的任何异常(可能是一个不阻止消息出去,但只是阻止附件被包含在内),使用以下语法:(取自phpMailer示例代码

  • 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!

}


这篇关于php邮件附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆