我在YII中有一个文件上传表单。我必须通过邮件发送作为附件上传的文件。邮件正在发送,但附件无效。我正在使用PHP邮件程序http://www.yiiframework.com/extension/phpmailer/
<?php echo $form->fileField($model,'career_resume',array('file','size'=>300,'maxlength'=>300)); ?>
这是我的控制器
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'.' '."$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('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,
));
}
从这里开始我实施了附件http://www.yiiframework.com/forum/index.php/topic/41330-phpmailer/
1.我试图设置文件的权限,但仍然无法正常工作 任何人都可以调查问题
答案 0 :(得分:3)
你可以通过
来做到$path=$_SERVER['DOCUMENT_ROOT'].'/LRI-Original/images/resumes/'.$fileName;
$name=$fileName;
$mail->AddAttachment($path,$name,$encoding ='base64',$type = 'application/octet-stream');
现在它应该,我已经在我的机器上测试了它
答案 1 :(得分:0)
我正在使用php邮件程序,这对我来说非常好,这里是代码
$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('webmastr@example.com', '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@email.com>;");
$mail->AddAddress($t["email"], $t["name"]);
$mail->Send();
这个代码对我来说非常好用于Yii中的php邮件程序
答案 2 :(得分:0)
电子邮件附件不能在yii中工作,但我可以发送电子邮件
$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())
=============================================== =========================