使用phpmaill發送郵件的例子

來源:互聯網
上載者:User

標籤:

首先,要下載php_mail軟體包

核心代碼:

index.php

<?phpinclude "mail.php";if (!empty($_POST[‘to‘])  && !empty($_POST[‘fromname‘]) && !empty($_POST[‘title‘]) && !empty($_POST[‘content‘])) {    send_mail($_POST[‘to‘],$_POST[‘fromname‘],$_POST[‘title‘],$_POST[‘content‘]);}?><form action="#" method="post">接收人:<input type="text" name="to" /><br>寄件者暱稱:<input type="text" name="fromname" /><br>標題:<input type="text" name="title" /><br>內容:<input type="text" name="content" style="width:400;height:100;" /><br><input type="submit" value="提交" />注:預設是用作者的QQ郵箱發送 請注意改成自己的資料</form>

mail.php

 1 <?php  2 header("content-type:text/html;charset=utf-8");  3 ini_set("magic_quotes_runtime",0);  4 require ‘php_mail/class.phpmailer.php‘;  5 require ‘php_mail/class.smtp.php‘;  6  7  8 function send_mail($to,$fromname,$title,$content){ 9 try { 10 $mail = new PHPMailer(true); 11 $mail->IsSMTP(); 12 $mail->CharSet=‘UTF-8‘; //設定郵件的字元編碼,這很重要,不然中文亂碼 13 $mail->SMTPAuth = true; //開啟認證 14 $mail->Port = 25; //連接埠請保持預設15 $mail->Host = "smtp.qq.com"; //使用QQ郵箱發送16 $mail->Username = "[email protected]"; //這個可以替換成自己的郵箱17 $mail->Password = "nelfobtugzckbdae"; //注意 這裡是寫smtp的授權碼 寫的不是QQ密碼,此授權碼不可用18 //$mail->IsSendmail(); //如果沒有sendmail組件就注釋掉,否則出現“Could not execute: /var/qmail/bin/sendmail ”的錯誤提示 19 $mail->AddReplyTo("[email protected]","mckee");//回複地址 20 $mail->From = "[email protected]"; 21 $mail->FromName = $fromname; 22 $to = $to; 23 $mail->AddAddress($to); 24 $mail->Subject = $title; 25 $mail->Body = $content;26 $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; //當郵件不支援html時備用顯示,可以省略 27 $mail->WordWrap = 80; // 設定每行字串的長度 28 //$mail->AddAttachment("f:/test.png"); //可以添加附件 29 $mail->IsHTML(true); 30 $mail->Send(); 31 echo ‘郵件已發送‘;32 33 } catch (phpmailerException $e) { 34 echo "郵件發送失敗:".$e->errorMessage(); 35 } 36 37 38 return true;39 }40 //環境 PHP5.3親測可用41 ?> 

效果:

 

使用phpmaill發送郵件的例子

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.