无法使用我的代码发送附件?

时间:2012-11-13 16:28:19

标签: phpmailer attachment

我正在使用phpMailer我不知道我的代码有什么问题,执行后返回失败? 我的函数调用phpmailer.php页面并获取参数:

function sendEmail($to,$from,$sender_name="",$subject,$body,$attachement_path){ 
  require("PHPMailer/class.phpmailer.php");
  $mail = new PHPMailer(); 
  $host="mail.mysite.com";
  $userName="info";
  $password="password!";
  $mail->IsSMTP();                                      // set mailer to use SMTP i.e. smtp1.example.com;smtp2.example.com
  $mail->Host = $host;  // specify main and backup server
  $mail->SMTPAuth = true;     // turn on SMTP authentication
  $mail->Username = $userName;  // SMTP username i.e email id of an email address
  $mail->Password = $password; // SMTP password for the specified email address         
  $mail->From = $from;
  $mail->FromName = $sender_name;
  $mail->AddAddress($to);   //mail,name
  $mail->AddAddress("myBCCMailAddress@gmail.com");                  // name is optional
  $mail->AddReplyTo($to);//to, name         
  $mail->WordWrap = 50; 
  $mail->AddAttachment($attachement_path);
  $mail->IsHTML(true);                                 // set email format to HTML
  $mail->Subject = $subject;
  $mail->Body    = $body;
  //$mail->AltBody = "This is the body in plain text for non-HTML mail clients";      
  if(!$mail->Send())
  {
     return false;
      }
  else{
      return true;
      }
  }

这是我上面函数的调用方法:

if(sendEmail($to,"info@mysite.com","My Company Name",$subject,$body,$path)){
              echo 'mail sent';
              }
           else{
               echo('mail failed to send '.$to);
               }

1 个答案:

答案 0 :(得分:0)

这是我使用的脚本。

        // phpmailer is required earlier
        $mail = new PHPMailer();
        $mail->IsSMTP();
        $mail->isHtml(true);
        $mail->AddAddress($customer_email);

        $mail->From = 'billing@example.com';
        $mail->FromName = 'MyName';
        $mail->Sender = 'mailman@example.com';

        $mail->AddBcc('accounting@example.com);
        $mail->Subject = 'Invoince;

        $mail->Body = 'Some exiting text explaining the customer about his invoice';
        $mail->AddAttachment('/var/invoices/'.$invoce_nr.'.pdf', 'Faktura.pdf');
        $mail->Body = iconv("UTF-8", "ISO-8859-1",$mail->Body);

        if($mail->send()){
            return true;
        } else {
            return false;
        }

还要确保您可以定期发送电子邮件,然后确保您尝试发送的文件是可读的。