本文目錄一覽:
php mail函數到底怎麼配置
樓主用phpmailer吧,我用過,比較簡單。下面是摘的一段phpmailer配置,試試看,有什麼問題再提問。
第一,需要下載PHPMailer文件包phpmailer.
第二,確認你的服務器系統已經支持socket
,通過phpinfo();查看是否支持sockets(socket 是屬於PHP擴展部分),如果顯現為“enabled”,那就是支持了。
第三,把文件解壓到你的web服務器目錄下,調用類就可以了.
首先包含class.phpmailer.php,然後創建對象,設置參數,調用成員函數。具體請見下面的示例代碼:
實例1,做成函數方便調用
?php
require(“phpmailer/class.phpmailer.php”);
function smtp_mail( $sendto_email, $subject, $body, $extra_hdrs, $user_name){
$mail = new PHPMailer();
$mail-IsSMTP(); // send via SMTP
$mail-Host = “200.162.244.66”; // SMTP servers
$mail-SMTPAuth = true; // turn on SMTP authentication
$mail-Username = “yourmail”; // SMTP username 注意:普通郵件認證不需要加 @域名
$mail-Password = “mailPassword”; // SMTP password
$mail-From = “yourmail@yourdomain.com”; // 發件人郵箱
$mail-FromName = “管理員”; // 發件人
$mail-CharSet = “GB2312”; // 這裡指定字符集!
$mail-Encoding = “base64”;
$mail-AddAddress($sendto_email,”username”); // 收件人郵箱和姓名
$mail-AddReplyTo(“yourmail@yourdomain.com”,”yourdomain.com”);
//$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
// 郵件主題
$mail-Subject = $subject;
// 郵件內容
$mail-Body = ”
htmlhead
meta http-equiv=”Content-Language” content=”zh-cn”
meta http-equiv=”Content-Type” content=”text/html; charset=GB2312″
/head
body
I love php。
/body
/html
“;
$mail-AltBody =”text/html”;
if(!$mail-Send())
{
echo “郵件發送有誤 p”;
echo “郵件錯誤信息: ” . $mail-ErrorInfo;
exit;
}
else {
echo “$user_name 郵件發送成功!br /”;
}
}
// 參數說明(發送到, 郵件主題, 郵件內容, 附加信息, 用戶名)
smtp_mail(“yourmail@yourdomain.com”, “歡迎使用phpmailer!”, “NULL”, “yourdomain.com”, “username”);
?
要注意的內容:
1. 郵件的字符集設置,
$mail-CharSet = “GB2312”; //
這裡指定字符集!在這裡我只指定為GB2312因為這樣Outlook能正常顯示郵件主題,我嘗試過設為utf-8但在Outlook下顯示亂碼。
2. 如果是發送html格式的郵件,那麼記得也指定meta … charset=GB2312″
3. 如果你想用它來群發郵件的話,記得修改包含文件函數,如:
require(“phpmailer/class.phpmailer.php”);
改為
require_once(“phpmailer/class.phpmailer.php”);
否則的話會產生類的重定義。
如何通過php發送郵件?php的mail函數不能用!
支持mail的服務器 一般都是linux的 國內的好像不多
自己的電腦安裝mail服務器不能往外發的 呵呵 可以自己測試用
現在很多管理系統都是用fsocketopen方式連接郵件服務器並發送郵件的 可以使用163 126的郵箱 網上有一些模型的 就像是好多管理系統後台讓填入用戶名和密碼 就能群發一樣 如果你不介意的話 給你轉發一個以前我自己改過的可以利用fsocketopen方式群發或者單發email的一共三個文件
MailClass.php 》》》》》》
?php
class Smtp
{
var $host; //主機
var $port; //端口 一般為25
var $user; //SMTP認證的帳號
var $pass; //認證密碼
var $debug = false; //是否顯示和服務器會話信息?
var $conn;
var $result_str; //結果
var $in; //客戶機發送的命令
var $from; //收件人收到郵件顯示的源信箱
var $email; //真實的地址
var $to; //目標信箱
var $subject; //主題
var $body; //內容
var $error;
var $All;
function Smtp($array)
{
$this-host = $array[‘host’];
$this-port = $array[‘port’];
$this-email= $array[‘trueemail’];
$this-from = $array[‘from’];
$this-user = base64_encode($array[‘username’]);
$this-pass = base64_encode($array[‘password’]);
$this-debug = $array[‘debug’];
$this-socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP);
if($this-socket){
$this-result_str = “創建SOCKET:”.socket_strerror(socket_last_error());
$this-debug_show($this-result_str);
}
else
die(“初始化失敗,請檢查您的網絡連接和參數”);
$this-conn = socket_connect($this-socket,$this-host,$this-port);
if($this-conn){
$this-result_str = “創建SOCKET連接:”.socket_strerror(socket_last_error());
$this-debug_show($this-result_str);
}
else
die(“初始化失敗,請檢查您的網絡連接和參數”);
$this-result_str = “服務器應答:font color=#cc0000″.socket_read ($this-socket, 1024).”/font”;
$this-debug_show($this-result_str);
}
function debug_show($str)
{
if($this-debug)
{
echo $str.”p\r\n”;
}
}
function setmail($to,$subject,$body){
$this-to = $to;
$this-subject = $subject;
$this-body = $body;
$All =”Content-type:text/html;charset=gb2312\r\n”; //郵件的編碼方式可以根據自己的需要改
$All .= “From:”.$this-from.”\r\n”;
$All .= “To:”.$this-to.”\r\n”;
$All .= “Subject:”.$this-subject.”\r\n\r\n”;
$All .= $this-body;
$this-All = $All;
}
/**
* 發送郵件部分
* 接收郵箱數組
*/
function send($toarray,$subject,$body)
{
//以下是和服務器會話
$this-in = “EHLO HELO\r\n”;
$this-docommand();
$this-in = “AUTH LOGIN\r\n”;
$this-docommand();
$this-in = $this-user.”\r\n”;
$this-docommand();
$this-in = $this-pass.”\r\n”;
$this-docommand();
foreach( $toarray as $to ) {
$this – setmail($to,$subject,$body);
$this-in = “RSET\r\n”;
$this-docommand();
$this-in = “MAIL FROM:”.$this-email.”\r\n”;
$this-docommand();
$this-in = “RCPT TO:”.$this-to.”\r\n”;
$this-docommand();
$this-in = “DATA\r\n”;
$this-docommand();
$this-in = $this-All.”\r\n.\r\n”;
$this-docommand();
}
$this-in = “QUIT\r\n”;
$this-docommand();
//結束,關閉連接
}
function docommand()
{
socket_write ($this-socket, $this-in, strlen ($this-in));
$this-debug_show(“Client Action:”.$this-in);
$this-result_str = “Server:font color=#cc0000″.socket_read ($this-socket, 1024).”/font”;
$this-debug_show($this-result_str);
}
}
?
MailConfig.inc.php 》》》》》》
?php
$mailconfig[‘host’] = “smtp.126.com”; //主機
$mailconfig[‘port’] = “25”; //端口 一般為25
$mailconfig[‘trueemail’] = “mhz1600@126.com”; //真實的地址
$mailconfig[‘username’] = “mhz1600”; //SMTP認證的帳號
$mailconfig[‘password’] = “*****”; //改成自己的
$mailconfig[‘debug’] = false; //是否顯示和服務器會話信息?
$mailconfig[‘from’] = “test@test.com”; //顯示給用戶的發件人
include_once “MailClass.php”;
set_time_limit(180);
?
SendDemo.php 》》》》》》
?php
include_once “MailConfig.inc.php”;
//簡單的臨時碼驗證 當前時間(到小時)的驗證碼
//if( empty($_GET[‘s’]) || $_GET[‘s’] != md5(date(‘Y-m-d-H’,time())) ) {header(“http/1.1 404″); die(”);}
//發送email
if( isset($_POST[‘sendmail’]) ) {
if( isset($_POST[‘from’]) ) $mailconfig[‘from’] = $_POST[‘from’];
$smtp = new Smtp($mailconfig);
$title = $_POST[‘title’];
//獲取post的email正文
if( get_magic_quotes_gpc() ) $message = $_POST[‘message’];
else $message = addslashes($_POST[‘message’]);
//從email列表/文檔中分離出所有的email地址
$pregstr = “@[a-zA-Z0-9\_][0-9a-zA-Z\.\-\_]+\@[0-aA-Za-z\-\_]+\.[0-9a-zA-Z\.\-\_]+@is”;
$temp = array();
preg_match_all($pregstr,$_POST[’emails’],$temp);
$toarray = $temp[0];
//var_dump($toarray);
$smtp-send($toarray,$title,$message);
die(“操作完成!A href=”.$_SERVER[‘PHP_SELF’].”?s=”.md5(date(‘Y-m-d-H’,time())).”繼續發送其他/a a href=# onclick=window.close()關閉/a”);
}
else {
if( isset($_POST[’emails’]) ) {
if( is_array($_POST[’emails’]) )
$emails = implode(“\t”,$_POST[’emails’]);
else
$emails = $_POST[’emails’];
}
else $emails = “”;
?
html
head
meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″style type=”text/css”
!–
body,td,th {
font-size: 12px;
}
—
/style/head
body
form id=”form1″ name=”form1″ method=”post” action=””
table width=”600″ border=”1″ align=”center” cellpadding=”3″ cellspacing=”0″ bordercolordark=”#FFFFFF” bordercolorlight=”#eeeeee”
tr
td width=”66″發件人:/td
td width=”516″input name=”from” type=”text” value=”?php echo $mailconfig[‘from’]; ?” 可以直接修改mailconfig文件中的email/td
/tr
tr
td郵件標題:/td
tdinput name=”title” type=”text” value=”郵件群發測試標題!” size=”60″/td
/tr
tr
td收件人:br/td
tdtextarea name=”emails” cols=”60″ rows=”5″?php echo $emails; ?/textarea/td
/tr
tr
td郵件正文:br
【html】/td
tdtextarea name=”message” cols=”60″ rows=”10″郵件群發測試!謝謝~!/textarea/td
/tr
tr
td /td
tdinput type=”submit” name=”sendmail” value=” 發送郵件 ” /td
/tr
/table
/form
?
}
?
/body
/html
使用方式 運行senddemo.php就行 確定本地或者服務器開啟了fsocketopen支持 在輸入框可以多種格式的的輸入很多email 程序用正則表達式匹配出所有的email地址 通過服務器循環對話的方式不斷的發送郵件 看看那個demo的流程就明白了
【鄭重聲明:mailclass修改自網上的模型 其他本人原創,版權不究 歡迎分享】
+———————廣告————————-+
那一天:回憶,讓生活更美好
獨享人生中那個特別的日子,記錄從那一天開始的幸福
期待您的加入,歡迎提供寶貴的意見建議
+————————————————–+
+——————–補充——————–+
發送郵件的服務器(smtp)並不是網址 126發送郵件的服務器是 smtp.126.com 網易163的發送郵件服務器是 smtp.163.com 所有郵箱對於這個都有說明的 還有一個就是能夠使用這個功能的好象新註冊的郵箱不太好用 因為網易在2006年10對郵箱進行過調整 在此之前註冊的都沒問題 在這之後註冊的好像開通一些其他的功能並且使用了一段時間才行的
smtp服務器的鏈接可以在命令提示行下測試 就是使用上面的命令:
首先 telnet smtp.126.com 25
因為smtp使用的25端口提供服務的 然後就會看到
220 126.com Anti-spam GT for Coremail System (126com[071018])
輸入 EHLO HELO
服務器返回
250-mail
250-PIPELINING
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN PLAIN
250 8BITMIME
輸入 AUTH LOGIN
服務器返回
334 dXNlcm5hbWU6
然後再輸入通過base64加密的用戶名和密碼 就能通過命令來和服務器對話 包括發送郵件等功能
具體的如果有興趣更多命令自己查一下
這個php的程序就是模擬這個功能來實現的
如果你用telnet直接連不上的話 說明服務器是錯誤的 。。
關於php中的mail函數
發郵件建議使用phpmailer,用自帶的函數需要服務器權限和修改php.ini參數不實際。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/286842.html