Tuesday, March 8, 2022

[FIXED] Could not access file PHP Mailer

Issue

I have a form for file upload in YII.I have to send the file uploaded as attachment through the mail. The mail is sending ,but the attachment is not working.I am using PHP mailer http://www.yiiframework.com/extension/phpmailer/

  <?php echo $form->fileField($model,'career_resume',array('file','size'=>300,'maxlength'=>300)); ?>

Here is my controller

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);
                            chmod($_SERVER['DOCUMENT_ROOT'].'/LRI-Original/images/resumes/'.$fileName, 0755);
                            $careername=$_POST['LriCareer']['career_name'];
                            $careeremail=$_POST['LriCareer']['career_email'];
                            $careerphone=$_POST['LriCareer']['career_phone'];
                            $careerpost=$_POST['LriCareer']['career_post'];
                            if($careerpost==1)
                            {
                                $careerpost='Product Manager-Healthcare';
                            }
                            else if($careerpost==2)
                            {
                                $careerpost='Technical Writer';
                            }
                            else if($careerpost==3)
                            {
                                $careerpost='Business Analyst';
                            }
                            else if($careerpost==4)
                            {
                                $careerpost='Quality Assurance Analyst (QA)';
                            }
                            else if($careerpost==5)
                            {
                                $careerpost='Support Engineers';
                            }

//                              $this->redirect('lriCareer/send');
                            Yii::import('application.extensions.phpmailer.JPhpMailer');
                            $mail = new JPhpMailer;
                            $mail->IsSMTP();
                            $mail->Host = 'smtp.googlemail.com:465';
                            $mail->SMTPSecure = "ssl";
                            $mail->SMTPAuth = true;
                            $mail->Username = '565676576@gmail.com';
                            $mail->Password = '12356756#';
                            $mail->SetFrom('565676576@gmail.com', 'Lri Career');
                            $mail->Subject = 'LongRiver Career';
                            $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
                            $mail->AddAttachment((Yii::app()->basePath) . '/images/resumes/'.$fileName);
//                                $mail->setAttachment(dirname(Yii::app()->basePath) . '/images/resumes/'.$fileName);
//                                $mail->AddAttachment($path,$name,$encoding ='base64',$type = 'application/octet-stream');
                            $mail->MsgHTML('Name'.'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'."$careername".'<br/>'.
                                    'Email'.'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'."$careeremail".'<br/>'.
                                    'Phone'.'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'."$careerphone".'<br/>'.
                                    'Post'.'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'."$careerpost".'<br/>'.
                                    'Resume'.'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'."$fileName".'<br/>'.

'<a href="http://www.longriverinfotech.com/images/resumes/'.$fileName.'">My Twitter</a>');
                            $mail->AddAddress('pachukutti@pandarakalan.com', 'rakshasi');
                            $mail->Send();

                            $this->render('send');
                        }

                    }
                    else
                    {
                         if($model->save())
            $this->redirect(array('view','id'=>$model->career_id));
                    }
    }

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


    }

From here i have implemented the attachment http://www.yiiframework.com/forum/index.php/topic/41330-phpmailer/

1.I have tried to set the permission to the file but still its not working Can any one out there look into the problem please


Solution

you can do it by

$path=$_SERVER['DOCUMENT_ROOT'].'/LRI-Original/images/resumes/'.$fileName;
$name=$fileName;
$mail->AddAttachment($path,$name,$encoding ='base64',$type = 'application/octet-stream');

now it should ,i have tested it on my machine



Answered By - Fazeela Abu Zohra

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.