Swift邮件附件

时间:2013-12-06 05:21:16

标签: php email yii swiftmailer

我在yii中尝试了两次电子邮件扩展程序。

1.YII-MAIL
2.PHP MAILER

现在我想试试swift邮件程序。我已经从这里下载了包http://swiftmailer.org/download并将其添加到YII的extensions文件夹中。

这里我有一个表单,其中包含姓名,电子邮件,电话和附件的字段。 我将上传的文件保存到图像文件夹下名为resumes的文件夹中,同时我发送的邮件包含所有详细信息以及上传的文件作为附件。但是点击创建按钮后我收到此错误

 include(Swift_Message.php) [<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory

这是我到目前为止尝试过的控制器动作

public function actionCreate()
{
            $this->layout='static_inner';

    $model=new LriCareer;

    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);

            if(isset($_POST['LriCareer']))
    {
                    $rnd = rand(0,9999);
        $model->attributes=$_POST['LriCareer'];
                    if($uploadedFile=CUploadedFile::getInstance($model,'career_resume'))
                    {
                        $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
                        $model->career_resume = $fileName;
                        if($model->save())
                        {
                            $uploadedFile->saveAs(dirname(Yii::app()->basePath) . '/images/resumes/'.$fileName);
                            $message = new YiiMailMessage;
                            $first_name="hello";
                            $message->setBody($first_name);
                            $message->subject = 'My Subject';
                            $message->addTo('fazeela.ma@longriverinfotech.com');
                            $message->from = Yii::app()->params['adminEmail'];   
                            $uploadedFile = CUploadedFile::getInstanceByName('fileupload'); // get the CUploadedFile
                            $uploadedFileName = $uploadedFile->tempName; // will be something like 'myfile.jpg'
                            $swiftAttachment = Swift_Attachment::fromPath($uploadedFileName);
                            $message->attach($swiftAttachment);
                            }
                               }
                    else
                    {
                         if($model->save())
            $this->redirect(array('view','id'=>$model->career_id));
                    }}

    $this->render('create',array(
        'model'=>$model,
    ));


    }

我在文件夹中看不到Swift_message.php。任何一个人都可以查看问题

1 个答案:

答案 0 :(得分:0)

Yii Framework有一个Swiftmailer Extension。我亲自用过几个项目,很棒。

如何附加文件:

$message = new YiiMailMessage;
$message->setBody($first_name);
$message->subject = 'My Subject';
$message->addTo('my@domain.com');
$message->from = Yii::app()->params['adminEmail'];   
$uploadedFileName = CUploadedFile::getInstance($model,'career_resume'); 
$uploadedFileName = $uploadedFile->tempName; // will be something like 'myfile.jpg'
$swiftAttachment = Swift_Attachment::fromPath($uploadedFileName);
$message->attach($swiftAttachment);