php发送邮件email

发布于 2016-08-13  2 次阅读


1.phpmailer,很好用,无论原生还是放到框架下,都没问题

    <?php  
    header("content-type:text/html;charset=utf-8");  
    include("class.phpmailer.php");  
    include("class.smtp.php");  
    $mail = new PHPMailer();   
    $mail->IsSMTP(); // send via SMTP   
    $mail->Host = "smtp.163.com"; // SMTP servers   
    $mail->SMTPAuth = true; // turn on SMTP authentication   
    $mail->Username = "*********"; // SMTP username 注意:普通邮件认证不需要加 @域名   
    $mail->Password = "*****"; // SMTP password   
    $mail->From = "******@163.com"; // 发件人邮箱   
    $mail->FromName = "zph"; // 发件人   
    $mail->CharSet = "utf-8"; // 这里指定字符集!   
    $mail->Encoding = "base64";   
    $mail->AddAddress("******@163.com","toyou"); // 收件人邮箱和姓名   
    $mail->AddReplyTo("******@163.com","wo");   
    //$mail->WordWrap = 50; // set word wrap 换行字数   
    //$mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment 附件   
    //$mail->AddAttachment("/tmp/image.jpg", "new.jpg");   
    $mail->IsHTML(true); // send as HTML   
    // 邮件主题   
    $subject="测试";  
    $mail->Subject = $subject;   
    // 邮件内容   
    $mail->Body = "   
    <p>点击激活</p>   
    <a href='#'>点击激活</a>  
    ";   
    $mail->AltBody ="text/html";   
    if(!$mail->Send())   
    {   
    echo "邮件发送有误 <p>";   
    echo "邮件错误信息: " . $mail->ErrorInfo;   
    exit;   
    }   
    else {   
    echo "邮件发送成功!<br />";   
    }  

2.当使用php原生mail()函数的时候,需要安装sendmail,linux一般已经安装sendmail,可以直接使用mail(),如果是windows需要先安装。

3.当使用ci框架的时候,如下

    public function sendemail()  
    {     
        $config = array('protocol'=>'smtp',  
            'smtp_host' => 'smtp.163.com',  
            'smtp_user' => 'yourname',  
            'smtp_pass' => '*****',  
            'smtp_port' => '25',  
            '_smtp_auth' => TRUE,  
            'wordwrap' => TRUE,  
            'charset' => 'iso-8859-1'  
      
        );  
        $this->load->library('email', $config);  
        $this->email->from('m13323262052@163.com', 'Your Name');  
        $this->email->to('m13323262052@163.com');  
        $this->email->subject('Email Test');  
        $this->email->message('Testing the email class.');  
        $this->email->send();  
    }  

只是我心中不再有火花,就让往事随风去吧。