Reputation: 458
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 = '[email protected]';
$mail->Password = '12356756#';
$mail->SetFrom('[email protected]', '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'.' '."$careername".'<br/>'.
'Email'.' '."$careeremail".'<br/>'.
'Phone'.' '."$careerphone".'<br/>'.
'Post'.' '."$careerpost".'<br/>'.
'Resume'.' '."$fileName".'<br/>'.
'<a href="http://www.longriverinfotech.com/images/resumes/'.$fileName.'">My Twitter</a>');
$mail->AddAddress('[email protected]', '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
Upvotes: 0
Views: 5022
Reputation: 1
email attach not working in yii but i can send email
$attachment = Yii::app()->baseUrl ."/upload/abc.jpg";
$mail = new PHPMailer;
$mail->IsSMTP();
$mail =Yii::app()->Smtpmail;
$mail->SetFrom($from, 'Admin');
$mail->SMTPAuth = true;
$mail->AddAttachment($attachment);
//$mail->AddAttachment($attachment, $name = '', $encoding = 'base64',
$type = 'image/jpeg');
$mail->Subject = $subject;
$mail->MsgHTML($view);
$mail->IsHTML(true);
if(!$mail->Send())
========================================================================
Upvotes: 0
Reputation: 641
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
Upvotes: 3
Reputation: 1058
I am using php mailer which is working very fine for me here is the code
$attachment = Yii::app()->baseUrl ."/uploads/images/".abc.jpg;
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->Host = 'xxxxxx';
$mail->SMTPAuth = true;
$mail->Username = 'xxxx';
$mail->Password = 'xxxx';
$mail->SetFrom('[email protected]', 'Admin');
$mail->AddAttachment($attachment);
$mail->Subject = $subject;
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
$mail->MsgHTML($body_html);
$send_to = $send_to . ("Name" . " <[email protected]>;");
$mail->AddAddress($t["email"], $t["name"]);
$mail->Send();
This code is working very fine for me for php mailer in Yii
Upvotes: 0