<?php
/*
fsockopen發送郵件
$to : 目標郵寄地址
$username : 使用者名稱
*/
function sendMail($to, $username=''){
$conf = array(
'from' => 'user@domain.com', //發信人Email位址
'user' => 'user@domain.com', //SMTP認證帳戶
'pwd' => 'xxxxxx', //SMTP認證密碼
'smtp' => 'smtp.domain.com', //SMTP伺服器位址,例如mail.gump.me
);
$subject = $username . ',fscockopen發送郵件測試';
$content = <<<EOD
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "HTTP://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="HTTP://www.w3.org/1999/xhtml">
<head>
<meta HTTP-equiv="Content-Type" content="text/html; charset=gb2312" />
</head>
<body>
<p>
$username ,您好,這是一封測試郵件,由php以fsockopen方式發送!
</p>
</body>
</html>
EOD;
$fp = @fsockopen($conf[smtp], 25, $errno, $errstr, 30);
fputs($fp, "HELO hi"."\r\n"); //和伺服器打招呼(hi),可省略
fputs($fp, "AUTH LOGIN ".base64_encode($conf[user])."\r\n");
fputs($fp, base64_encode($conf[pwd])."\r\n");
fputs($fp, "MAIL FROM:"."<".$conf[from].">"."\r\n");
fputs($fp, "RCPT TO:"."<".$to.">"."\r\n");
fputs($fp, "DATA "."\r\n");
fgets($fp, 512);
$header .= "MIME-Version:1.0\r\n";
$header .= "Content-Type:text/html\r\n";
$header .= "To: ".$to."\r\n";
$header .= "From: ".$conf[from]."<".$conf[from].">\r\n";
$header .= "Subject: ".$subject."\r\n";
$header .= "Date: ".date("r")."\r\n";
fputs($fp, $header."\r\n".$content."\r\n.\r\n");
fputs($fp, "QUIT "."\r\n");
$sign = FALSE;
while(!feof($fp)){
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 354) {
continue;
}else{
$sign = TRUE;
}
}
fclose($fp);
return $sign;
}

//示例
if(sendMail('admin@gump.me', 'Gump')){
echo '發送成功';
}else{
echo '發送失敗';
}
?>

創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()