zend_mail实现发送邮件实例(包括附件)

php代码

 

            $sendTo = $this->getRequest()->getPost('sendTo');
            if($sendTo) {
                $subject = $this->getRequest()->getPost('subject');
                $content = $this->getRequest()->getPost('content');
                $mail = new Zend_Mail("UTF-8");
                $config = array('auth' => 'login', 'username' => $this->_email,
                'password' => $this->_password, 'ssl' => $this->_ssl, 'port' => 465);
                $mailTransport = new Zend_Mail_Transport_Smtp($googleSmtp, $config);
                if($_FILES['attach']['tmp_name'] != '') { 
                    $attach = $mail->createAttachment(file_get_contents($_FILES['attach']['tmp_name'])); 
                    $attach->type = $_FILES['attach']['type']; 
                    $attach->filename = $_FILES['attach']['name']; 
                }
                $mail->setBodyHtml($content);
                $mail->setSubject($subject);
                $mail->setFrom($this->_email, $this->_email);
                $sendToArr = explode(';',$sendTo);
                foreach ($sendToArr as $send){
                       $mail->addTo($send, $send);
                    try {
                        $rs = $mail->send($mailTransport);
                    } catch (Exception $e) {
                        $this->getSession()->addError('send mail failed ,please check the send to email and try again');
                        $this->_redirect("$this->_module/$this->_controller/send-mail");
                    }
                }
                $this->getSession()->addSuccess('send mail succeed at ' . $rs->getDate());
                $this->_redirect("$this->_module/$this->_controller/index");
            }

 

 

html代码:

<form action="<?php echo $baseUrl.'/'.$module.'/'.$controller;?>/send-mail/" method="post" enctype="multipart/form-data">
    <table width="100%">
        <tr><th colspan="2"><h2>Send Mail</h2></th></tr>
        <tr><td width="20%">send to</td>
        <td width="80%"><input type="text" name="sendTo" value="<?php echo $from;?>"/></td></tr>
        <tr><td width="20%">subject</td>
        <td width="80%"><input type="text" name="subject" value="<?php echo $subject;?>"/></td></tr>
        <tr><td width="20%">content</td>
        <td width="80%"><textarea name="content" cols="30" rows="7"></textarea></td></tr>
        <tr><td>Add attach file</td><td><input type="file" name="attach"></input></td></tr>
    </table>
    <input type="submit" value="send"/>
</form>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值