Вопрос

I am preparing to create a form page for a website that will require many fields for the user to fill out and that will be sent to a specified email. I am able to successfully send the emails but no attachments are found in the mails.

$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 = 'someaddress@gmail.com';
    $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('fromadd@gmail.com', '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 */
}

I have tried changing the $_POST to $_FILES too for adding attachments.

My view:

<?php $form=$this->beginWidget('CActiveForm', array(
         'htmlOptions' => array('enctype' => 'multipart/form-data') ,
     )); ?>
<?php echo $form->fileField($model, 'filename');?>
<?php  echo $form->error($model, 'filename');?>
Это было полезно?

Решение

Since you are using yii. SO lets do it in yii style.

First of create an object for the uploaded file

$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);

Другие советы

You can't catch a file with

$_POST['']

You have to use

$_FILES['']

You should read this link, and just do it step by step

http://www.w3schools.com/php/php_file_upload.asp

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top