dsxi70423 2012-06-04 04:10
浏览 102
已采纳

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

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

  • 写回答

3条回答 默认 最新

  • duanbai5348 2012-06-04 04:22
    关注

    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>
      ";
      
      } catch (phpmailerException $e) {
      
        echo $e->errorMessage(); //Pretty error messages from PHPMailer
      
      } catch (Exception $e) {
      
        echo $e->getMessage(); //Boring error messages from anything else!
      
      }
      
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题