Swift 邮件附件 [英] Swift mailer attachment

查看:33
本文介绍了Swift 邮件附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 yii 中尝试过两次电子邮件扩展.

I have tried email extensions in yii twice.

1.YII-MAIL
2.PHP MAILER

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

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.

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

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


    }

我在文件夹中没有看到 Swift_message.php.任何人都可以调查这个问题

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

推荐答案

有一个 Swiftmailer Extension 用于 Yii 框架.我个人在几个项目中使用过,它很棒.

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

如何附加文件:

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

这篇关于Swift 邮件附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆