您的位置:澳门402永利com > 关于计算机 > 两种常用的php发送邮件代码,实例教程

两种常用的php发送邮件代码,实例教程

发布时间:2019-09-29 02:20编辑:关于计算机浏览(60)

    imap连接服务器代码。

     代码如下

    <?php
    //连接 IMAP 服务器链接,IMAP 的端口为 143。
    $mbox = imap_open("{localhost:143}INBOX","user_id","password");
    //连接POP3 服务器链接,POP3 的端口为 110。
    $mbox = imap_open("{localhost/pop3:110}INBOX","user_id","password");
    //连接NNTP 服务器链接,NNTP 的端口为 119。
    $nntp = imap_open("{localhost/nntp:119}comp.test","","");
    ?>

    <?php
    //连接 IMAP 服务器链接,IMAP 的端口为 143。
    $mbox = imap_open("{localhost:143}INBOX","user_id","password");
    //连接POP3 服务器链接,POP3 的端口为 110。
    $mbox = imap_open("{localhost/pop3:110}INBOX","user_id","password");
    //连接NNTP 服务器链接,NNTP 的端口为 119。
    $nntp = imap_open("{localhost/nntp:119}comp.test","","");
    ?>

    复杂一点的imap连接方法。

    <?php
    //连接IMAP服务器
    $mbox = imap_open("{imap.example.org}", "username", "password", OP_HALFOPEN)
          or die("连接失败: " . imap_last_error());
    $list = imap_getmailboxes($mbox, "{imap.example.org}", "*");
    if (is_array($list)) {
        foreach ($list as $key => $val) {
            echo "($key) ";
            echo imap_utf7_decode($val->name) . ",";
            echo "'" . $val->delimiter . "',";
            echo $val->attributes . "<br /> ";
        }
    } else {
        echo "imap_getmailboxes 失败: " . imap_last_error() . " ";
    }
    //关闭imap连接
    imap_close($mbox);
    ?>

    <?php
    //连接IMAP服务器
    $mbox = imap_open("{imap.example.org}", "username", "password", OP_HALFOPEN)
          or die("连接失败: " . imap_last_error());
    $list = imap_getmailboxes($mbox, "{imap.example.org}", "*");
    if (is_array($list)) {
        foreach ($list as $key => $val) {
            echo "($key) ";
            echo imap_utf7_decode($val->name) . ",";
            echo "'" . $val->delimiter . "',";
            echo $val->attributes . "<br />n";
        }
    } else {
        echo "imap_getmailboxes 失败: " . imap_last_error() . "n";
    }
    //关闭imap连接
    imap_close($mbox);
    ?>

     

    转请注明www.111cn.net/phper/php.html

     

     

     代码如下

    <?php
    mail( "163@163.com", "欢迎你", "hello,你好! " );
    ?>

    <?php
    //定义边界线
    $boundary = uniqid( "" );
    //生成邮件头
    $header = "From: $from Content-type: multipart/mixed;
    boundary="$boundary" X-Mailer:PHP X-Priority:3";
    //获取附件文件的MIME类型
    $mimetype = mime_content_type('test.zip')
    //获取附件文件的名字
    $attach = 'test.zip'
    //对附件文件进行编码和切分
    $fp = fopen($attach, "r");
    $content = fread($fp, filesize($attach));
    $content = chunk_split( base64_encode($content) );
    //生成邮件主体
    $body ="
    --$boundary
    Content-type: text/plain; charset=iso-8859-1
    Content-transfer-encoding: 8bit
    $message
    --$boundary
    Content-Type: $mimeType; name=$filename
    Content-Disposition: attachment; filename=$filename
    Content-Transfer-Encoding: base64
    $content
    --$boundary--";
    //发送邮件
    mail( $to, $subject, $body, $header );
    ?>

    <?php
    $mbox=imap_open("{localhost:143}INBOX","myid","mypw");
    $message=imap_body($mbox,1);
    imap_close($mbox);
    echo$message;
    ?>

    本文由澳门402永利com发布于关于计算机,转载请注明出处:两种常用的php发送邮件代码,实例教程

    关键词: