2014-04-04 81 views
2

我準備爲網站創建一個表單頁面,用戶需要填寫很多字段並將其發送到指定的電子郵件。我能夠成功發送電子郵件,但在郵件中找不到附件。使用PHPMailer在郵件中添加附件失敗

$model->attributes=$_POST['ContactForm']; 

if($model->validate()) 
{ 
    require("class.phpmailer.php"); 
    $mail = new PhpMailer; 
    $mail->IsSMTP(); 
    $mail->SMTPSecure = "ssl"; 
    $mail->Host = 'smtp.gmail.com'; 
    $mail->SMTPAuth = true; 
    $mail->SMTPSecure = true; 
    $mail->Username = '[email protected]'; 
    $mail->Port = '465'; 
    $mail->Password = '*****'; 
    $mail->SMTPKeepAlive = true; 
    $mail->Mailer = "smtp"; 
    $mail->IsSMTP(); // telling the class to use SMTP 
    $mail->SMTPAuth = true; 
    $mail->CharSet = 'utf-8'; 
    $mail->SMTPDebug = 0; 
    $mail->SetFrom('[email protected]', 'name'); 
    $mail->Subject = $_POST['ContactForm']['subject']; 
    $mail->AltBody = 'To view the message, please use an HTML compatible'; 
    $mail->MsgHTML($_POST['ContactForm']['body']); 
    $mail->AddAttachment($_POST['ContactForm']['filename']); /**Problem is here */ 
} 

我試着改變$ _POST爲$ _FILES太添加附件。

我的觀點:

<?php $form=$this->beginWidget('CActiveForm', array(
     'htmlOptions' => array('enctype' => 'multipart/form-data') , 
    )); ?> 
<?php echo $form->fileField($model, 'filename');?> 
<?php echo $form->error($model, 'filename');?> 
+0

嘗試'的var_dump()''$ _FILES'和'$ _POST' – Dinistro

+0

如果我傾倒$ _FILES,它給未定義指數誤差「文件名」。但是,我已經在我的模型中定義了它們 – user3496974

+0

但數據在$ _FILES中? – Dinistro

回答

2

由於您使用的是yii。所以讓我們以yii的風格來做。

首先對上傳的文件創建一個對象的

$attachment=CUploadedFile::getInstance($model, 'filename'); 
if($attachment !== null){ 
$path= $attachment->tempName; 
$name=$attachment->name; 
} 

// then do the coding for you mail and change this line 

    $mail->AddAttachment($path,$name); 
+0

謝謝!那太棒了 – user3496974

+0

很高興工作:)享受:) –

相關問題