Swift mailer attachment

我与影子孤独终老i 提交于 2021-02-07 07:57:10

问题


I have tried email extensions in yii twice.

1.YII-MAIL
2.PHP MAILER

Now i would like to try out swift mailer.I have downloaded the package from here http://swiftmailer.org/download and added it to the extensions folder in YII.

Here i have a form ,with fields for name,email,phone and an attachment. I am saving the file uploaded to a folder called resumes under images folder ,at the same time i am sending a mail with all details along with the uploaded file as an attachment .But on clicking create button i am getting this error

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

Here is the controller action i have tried so far

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


    }

I dont see Swift_message.php in the folder.Can any one out there can look into the problem


回答1:


There is a Swiftmailer Extension for Yii Framework. I've personally used in a couple of projects and its awesome.

How to attach Files:

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


来源:https://stackoverflow.com/questions/20416583/swift-mailer-attachment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!