php郵件表單
❶ 用php編寫一個網頁表單並發送到指定郵箱 如何寫代碼
發送郵件,可以使用mail函數,以下是mail的示例,你可以再仿照網上的郵箱編寫個發送界面表單:
<?php
$to = '[email protected]';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
❷ 請問如何用php實現表單提交後以郵件的形式發到郵箱中.
用mail函數或者fsockopen函數來發送郵件。
手冊里的例子你看看吧:
<?php
$to = '[email protected]';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
❸ 這個表單如何用php發送郵件
表單提交數據到contacts.php文件處理 發送郵件信息,使用PHPMailer發送郵件就可以,
PHPMailer使用方法
網頁鏈接
❹ 新手想做一個PHP的表單提交發送到指定郵箱,請高手指教
首先包含一個郵件發送類 require("sendMail.php");
然後你要有一個發送郵件的伺服器(可以到163隨意注冊個,例如注冊的是[email protected],密碼是aa123),然後是發送郵件的代碼,我給你一個我用的例子
$smtpserver = "smtp.163.com"; //你選擇的SMTP伺服器
$smtpserverport =25; //SMTP伺服器埠
$smtpusermail = "[email protected]"; //SMTP伺服器的用戶郵箱
$smtpemailto = "[email protected]"; //收件箱
$smtpuser = "[email protected]"; //SMTP伺服器的用戶帳號
$smtppass = "aa123"; //SMTP伺服器的用戶密碼
$MailBody="姓名:".$_POST['Name'];//郵件內容(如你提交的表單姓名為Name)
$mailsubject=@iconv("UTF-8", "gb2312", "XX網站-問題提交");//如果你頁面為UTF-8,這里還要轉碼一下
$mailbody = $MailBody; //郵件內容
$mailtype = "HTML"; //郵件格式(HTML/TXT),TXT為文本郵件
$smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//true表示使用身份驗證,否則不使用身份驗證.
$smtp->debug = FALSE; //是否顯示發送的調試信息 TRUE發送 FALSE不發送
$smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);
sendMail.php代碼:
class smtp
{
/* Public Variables */
var $smtp_port; //郵件伺服器埠
var $time_out; //超時時間
var $host_name; //主機名稱
var $log_file; //日誌文件
var $relay_host; //
var $debug;
var $auth;
var $user; //用戶名
var $pass; //密碼
var $sock;
/* Constractor */
function smtp($relay_host, $smtp_port,$auth,$user,$pass)
{
// echo $relay_host." ".$smtp_port." ".$auth." ".$user." ".$pass;
$this->debug = FALSE;
$this->smtp_port = $smtp_port;
$this->relay_host = $relay_host;
$this->time_out = 30;//is used in fsockopen()
$this->auth = $auth;//auth
$this->user = $user;
$this->pass = $pass;
$this->host_name = "localhost";//is used in HELO command
$this->log_file = "";
$this->sock = FALSE;
}
/* Main Function */
function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "")
{
$mail_from = $this->get_address($this->strip_comment($from));
$body = ereg_replace("(^|(\r\n))(\.)", "\1.\3", $body);
$header .= "MIME-Version:1.0\r\n";
if($mailtype=="HTML"){
$header .= "Content-Type:text/html\r\n";
}
$header .= "To: ".$to."\r\n";
if ($cc != "") {
$header .= "Cc: ".$cc."\r\n";
}
$header .= "From: $from<".$from.">\r\n";
$header .= "Subject: ".$subject."\r\n";
$header .= $additional_headers;
$header .= "Date: ".date("r")."\r\n";
$header .= "X-Mailer:By Redhat (PHP/".phpversion().")\r\n";
list($msec, $sec) = explode(" ", microtime());
$header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">\r\n";
$TO = explode(",", $this->strip_comment($to));
if ($cc != "") {
$TO = array_merge($TO, explode(",", $this->strip_comment($cc)));
}
if ($bcc != "") {
$TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));
}
$sent = TRUE;
foreach ($TO as $rcpt_to) {
$rcpt_to = $this->get_address($rcpt_to);
if (!$this->smtp_sockopen($rcpt_to)) {
$this->log_write("Error: Cannot send email to ".$rcpt_to."\n");
$sent = FALSE;
continue;
}
if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {
$this->log_write("E-mail has been sent to <".$rcpt_to.">\n");
}
else {
$this->log_write("Error: Cannot send email to <".$rcpt_to.">\n");
$sent = FALSE;
}
fclose($this->sock);
$this->log_write("Disconnected from remote host\n");
}
return $sent;
}
/* Private Functions */
function smtp_send($helo, $from, $to, $header, $body = "")
{
if (!$this->smtp_putcmd("HELO", $helo)) {
return $this->smtp_error("sending HELO command");
}
if($this->auth){
if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) {
return $this->smtp_error("sending HELO command");
}
if (!$this->smtp_putcmd("", base64_encode($this->pass))) {
return $this->smtp_error("sending HELO command");
}
}
if (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">")) {
return $this->smtp_error("sending MAIL FROM command");
}
if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">")) {
return $this->smtp_error("sending RCPT TO command");
}
if (!$this->smtp_putcmd("DATA")) {
return $this->smtp_error("sending DATA command");
}
if (!$this->smtp_message($header, $body)) {
return $this->smtp_error("sending message");
}
if (!$this->smtp_eom()) {
return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]");
}
if (!$this->smtp_putcmd("QUIT")) {
return $this->smtp_error("sending QUIT command");
}
return TRUE;
}
function smtp_sockopen($address)
{
if ($this->relay_host == "") {
return $this->smtp_sockopen_mx($address);
} else {
return $this->smtp_sockopen_relay();
}
}
function smtp_sockopen_relay()
{
$this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."\n");
$this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
if (!($this->sock && $this->smtp_ok())) {
$this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."\n");
$this->log_write("Error: ".$errstr." (".$errno.")\n");
return FALSE;
}
$this->log_write("Connected to relay host ".$this->relay_host."\n");
return TRUE;
}
function smtp_sockopen_mx($address)
{
$domain = ereg_replace("^.+@([^@]+)$", "\1", $address);
if (!@getmxrr($domain, $MXHOSTS)) {
$this->log_write("Error: Cannot resolve MX \"".$domain."\"\n");
return FALSE;
}
foreach ($MXHOSTS as $host) {
$this->log_write("Trying to ".$host.":".$this->smtp_port."\n");
$this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
if (!($this->sock && $this->smtp_ok())) {
$this->log_write("Warning: Cannot connect to mx host ".$host."\n");
$this->log_write("Error: ".$errstr." (".$errno.")\n");
continue;
}
$this->log_write("Connected to mx host ".$host."\n");
return TRUE;
}
$this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")\n");
return FALSE;
}
function smtp_message($header, $body)
{
fputs($this->sock, $header."\r\n".$body);
$this->smtp_debug("> ".str_replace("\r\n", "\n"."> ", $header."\n> ".$body."\n> "));
return TRUE;
}
function smtp_eom()
{
fputs($this->sock, "\r\n.\r\n");
$this->smtp_debug(". [EOM]\n");
return $this->smtp_ok();
}
function smtp_ok()
{
$response = str_replace("\r\n", "", fgets($this->sock, 512));
$this->smtp_debug($response."\n");
if (!ereg("^[23]", $response)) {
fputs($this->sock, "QUIT\r\n");
fgets($this->sock, 512);
$this->log_write("Error: Remote host returned \"".$response."\"\n");
return FALSE;
}
return TRUE;
}
function smtp_putcmd($cmd, $arg = "")
{
if ($arg != "") {
if($cmd=="") $cmd = $arg;
else $cmd = $cmd." ".$arg;
}
fputs($this->sock, $cmd."\r\n");
$this->smtp_debug("> ".$cmd."\n");
return $this->smtp_ok();
}
function smtp_error($string)
{
$this->log_write("Error: Error occurred while ".$string.".\n");
return FALSE;
}
function log_write($message)
{
$this->smtp_debug($message);
if ($this->log_file == "") {
return TRUE;
}
$message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message;
if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) {
$this->smtp_debug("Warning: Cannot open log file \"".$this->log_file."\"\n");
return FALSE;;
}
flock($fp, LOCK_EX);
fputs($fp, $message);
fclose($fp);
return TRUE;
}
function strip_comment($address)
{
$comment = "\([^()]*\)";
while (ereg($comment, $address)) {
$address = ereg_replace($comment, "", $address);
}
return $address;
}
function get_address($address)
{
$address = ereg_replace("([ \t\r\n])+", "", $address);
$address = ereg_replace("^.*<(.+)>.*$", "\1", $address);
return $address;
}
function smtp_debug($message)
{
if ($this->debug) {
//echo $message;
}
}
}
❺ PHP郵件接收到網頁表單信息為亂碼
資料庫 頁面 文件編碼統一
❻ php 如何實現將表單直接發送到制定郵箱
首先你的php必須有email組建
一般常用的是 jmail
<?
class Jmail
{
public $Username; //郵局用戶名
public $Password; //密碼
public $FormName ; //發件人姓名
public $From ; //發件人地址
public $Addrecipient ; //收件人地址
public $Ttile ; //郵件標題
public $Content; //郵件內容
public $Smtp; //郵件伺服器
function Send(){
$Jmail = new com("Jmail.Message"); //實例化一個Jmail對象
$Jmail->SiLent=true; //設置成True的話Jmail不會提示錯誤只會返回True和False
$Jmail->LogGing = false; //是否開啟日誌
$Jmail->CharSet = "GB2312"; //設定字元串編碼
$Jmail->ContentType = "Text/html"; //郵件的格式為HTML格式
$Jmail->MailServerUsername = $this->Username; //發信箱用戶名
$Jmail->MailServerPassword = $this->Password; //發信箱密碼
$Jmail->FromName = $this->FromName; //發件人姓名
$Jmail->From = $this->From; //發件人地址
$Jmail->AddRecipient($this->Addrecipient); //收件人地址
$Jmail->Subject = $this->Title;//Email標題
$Jmail->Body = $this->Content; //Email正文
$JmailError = $Jmail->Send($this->Smtp); //Smtp伺服器
if($JmailError){ //判斷郵件是否發送成功
return true;
}else{
return false;
}
}
}
//這里是調用代碼
$jmail = new Jmail();
$jmail->Username = ""; //這里是smtp的帳號
$jmail->Password = "";//這里是smtp的密碼
$jmail->FromName = "";//這里是郵件顯示的名字
$jmail->From = "";//這里填寫你發送的郵件地址
$jmail->Addrecipient = ""; //這里填寫你目標的郵件地址
$jmail->Title = "這是標題";
$jmail->Content = "";//這里就是你表單裡面面的悲痛
$jmail->Smtp = "";//這里是你郵件的smtp地址
if($jmail->Send()){
echo "成功哦!";
}else{
echo "失敗哦!";
}
?>
❼ 怎樣用php提交表單到發送郵件
$mailbody =$_POST; 你把$_POST賦給了郵件內容,而$_POST本來就是一個數組 所以會顯示是Array 改成:$mailbody =$_POST["name"]."\n".$_POST['add']."\n".$_POST['c']."\n".$_POST['d']; 試試看?
❽ php提交表單發送郵件
<?php
require_once "email.class.php";
//******************** 配置信息 ********************************
$smtpserver = "smtp.126.com";//SMTP伺服器
$smtpserverport =25;//SMTP伺服器埠
$smtpusermail = "[email protected]";//SMTP伺服器的用戶郵箱
$smtpemailto = $_POST['toemail'];//發送給誰
$smtpuser = "new2008oh";//SMTP伺服器的用戶帳號
$smtppass = "您的郵箱密碼";//SMTP伺服器的用戶密碼
$mailtitle = $_POST['title'];//郵件主題
$mailcontent = "<h1>".$_POST['content']."</h1>";//郵件內容
$mailtype = "HTML";//郵件格式(HTML/TXT),TXT為文本郵件
//************************ 配置信息 ****************************
$smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//這裡面的一個true是表示使用身份驗證,否則不使用身份驗證.
$smtp->debug = false;//是否顯示發送的調試信息
$state = $smtp->sendmail($smtpemailto, $smtpusermail, $mailtitle, $mailcontent, $mailtype);
echo "<div style='width:300px; margin:36px auto;'>";
if($state==""){
echo "對不起,郵件發送失敗!請檢查郵箱填寫是否有誤。";
echo "<a href='index.html'>點此返回</a>";
exit();
}
echo "恭喜!郵件發送成功!!";
echo "<a href='index.html'>點此返回</a>";
echo "</div>";
?>