Los novatos quieren enviar un formulario PHP y enviarlo a una dirección de correo electrónico designada. Por favor dame tu consejo.
Luego debe tener un servidor para enviar correos electrónicos (puede registrarse en 163 a voluntad, por ejemplo, el servidor registrado es aa@163.com , la contraseña es aa123), y luego el código para enviar el correo electrónico. Déjame darte un ejemplo que he usado.
$ servidor SMTP = " SMTP . 163 . com "; // servidor SMTP de su elección p >
$ smtpserverport = 25 // puerto del servidor SMTP p >
$ smtpusermail = " aa @ 163 . com "; // Buzón de usuario del servidor SMTP
$ smtpemailto = " bb @ qq.com//Bandeja de entrada
$ smtpuser = " aa @ 163 .com " // Cuenta de usuario para el servidor SMTP
$ smtppass = " aa 123 " // Contraseña de usuario para el servidor SMTP
$MailBody = "Nombre:". $_POST[' Nombre']; //Contenido del correo electrónico (si el formulario que envió es Nombre)
$TERM asunto = @iconv("UTF-8","GB 2312","XX Sitio web - Envío de preguntas") ; // Si su página es UTF 8, necesita transcodificar aquí.
$mailbody = $MailBody //Contenido del correo electrónico
$mailtype = "HTML//Formato de correo electrónico (HTML) /TXT), TXT es un correo electrónico de texto.
$smtp = new smtp($smtpserver, $smtpserverport, true, $smtpuser, $SMTP pass); // verdadero significa que se utiliza la autenticación; de lo contrario, no se utiliza la autenticación.
$ SMTP-gt;debug = FALSE//Si mostrar información de depuración, enviar verdadero, enviar falso, no enviar.
$ SMTP->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $tipo de correo);
Código SendMail.php:
smtp clase
{
/*Variables públicas*/
var $ smtp_port//Puerto del servidor de correo
var $time_ out//Tiempo de espera
var $ host_name//Nombre de host
var $ log_file//Archivo de registro
var $relay_host/ /
var $debug
var $auth
var $usuario; //nombre de usuario
var $contraseña//contraseña p>
var $sock
/*Constructor*/
Función smtp($relay_host, $smtp_port, $auth, $user, $pass) p>
{
// echo $relay_host. " ".$smtp_port. " ".$autenticación. " ".$usuario.
" ".$ pass
$ this- gt; debug = FALSE
$ this- gt = $ smtp _ port
$ this- gt;relayhost = $relayhost;
$this-gt;time_out=30 //Usado en fsockopen()
$this-gt;auth= $ auth//auth p>
$ this-gt;usuario = $ usuario
$ this-gt;pass = $ pass
$ this-gt;host _ nombre = "localhost// Usado en el comando HELO
$ this-gt; log _ file =
$ this-gt = FALSE
}
/*Función principal*/
Función sendmail($to, $from, $subject = " ", $body = " ", $mailtype, $cc = " " , $bcc = " ", $additional_headers = " ")
{
$ mail_from = $this-gt;get_address($this-gt;strip_comment ($ de));
$ cuerpo = ereg_replace("(^|(\r\n))(\.)", "\1.\3", $ cuerpo);
$header = "Versión MIME: 1.0\r\n";
if($mailtype=="HTML"){
$header. Tipo de contenido: texto/html \r\n"; p>
}
$header.= "Para: ".$to."\r\n";
if($cc!= "") {
$header. = "cc:"\r\n";
}
$header. = "De: $De lt;. $desde. " gt\ r \ n ";
$encabezado. = "Tema:". $asunto. "\r\n";
$encabezado. = $additional_headers
$encabezado. = "Fecha:". fecha("r"). "\r\n";
$encabezado. = "X-Mailer: Por Redhat (PHP/".phpversion().")\r\n";
list($msec, $sec) = explotar(",microtime());
$header. = "ID del mensaje: lt;. fecha(" YmdHis ", $sec). "." ($mseg*1000000). "." $correo_de.
" gt\ r \ n ";
$TO = explotar(",",$this- gt;strip_comment($to));
if ($cc! = " ") {
$TO = array_merge($TO,explode(",",$this-gt;strip_comment($cc)));
}
if ($bcc != "") {
$TO = array_merge($TO,explode(",",$this-gt;strip_comment($bcc )));
}
$ enviado = VERDADERO
foreach ($TO como $rcpt_to) {
$ rcpt _ to = $this-gt;get_address( $rcpt_to);
if (!$this-gt;smtp_sockopen($rcpt_to)) {
$this-gt; log_write("Error: No se puede enviar el correo electrónico a ". $rcpt_to." \n ");
$ enviado = FALSO
Continuar;
}
if($this-gt ;SMTP_send($this-gt;hostname,$mailsender,$recipient,$title,$text){
$ this- gt; log_write("El correo electrónico se ha enviado al teniente $rcpt_to. " gt\ n ");
}
De lo contrario {
$ this- gt;log_write("Error: No se puede enviar correo electrónico al teniente $rcpt_to .
" gt\ n ");
$ enviado = FALSE
}
fclose($ this- gt; calcetines);
$ this- gt;log_write("Desconectado del host remoto\n ");
}
return $ enviado
}
/*Función privada*/
Función smtp_send($helo, $from, $to, $header, $body = " ")
{
if (!$this->smtp_putcmd("HELO",$helo)) {
return $this->smtp_error("Enviar comando HELO");
}
if($this-gt;auth){
if(!$this-gt;smtp_putcmd("AUTH LOGIN", base64_encode($this-gt;user) )){
return $this-gt;smtp_error("Enviar comando HELO");
}
if (!$this-gt;smtp_putcmd( ",base64_encode($this -gt;pass))){
return $this-gt;smtp_error("Enviar comando HELO");
}
}
if(!$this->smtp_putcmd("MAIL","FROM:lt;.$from.">)) {
return $this- gt; ");
}
if (! $this- gt; smtp_putcmd("RCPT", "Para: lt. $to . " >)) {
return $ this- gt; smtp_error("Enviar RCPT al comando ");
}
if (! $ this- gt; smtp_putcmd("DATOS")) {
return $ this- gt; smtp_error("Enviar comando de datos");
}
if (! $ this- gt; smtp_message($header, $ body)) {
return $ this- gt; smtp_error("Enviar correo");
}
if(! $ this- gt; smtp_eom()) {
return $ this- gt; smtp_error("Enviar ltCR gt ltLF gt;
ltCR gt ltLF gt[EOM]";
}
if (! $this- gt; smtp_putcmd("QUIT")) {
return $this - gt; smtp_error("Enviar comando de salida");
}
Devuelve VERDADERO
}
Función smtp_sockopen($dirección)
{
if($this-gt;relay_host == "") {
return $this-gt;SMTP_sock open_MX($ dirección);
}else {
return $ this- gt; SMTP _ sock open _ Relay()
}
}
Función smtp_sockopen_relay()
{
$this-gt;log_write("Intentando". $this-gt;relay_host.":" .$this-gt; smtp_port."\n");
$this-gt;sock = @fsockopen($this-gt;relay_host,$this-gt;smtp_port,$errno,$errstr ,$this-gt; time_out);
if (!($this-gt;socks amp$this-gt;smtp_ok())) {
$this ->log_write("Error: No se puede conectarse a Relay_host". $this->relay_host."\n");
$this->log_write("Error:". $errstr." ("$errno")\n") ;
Devuelve FALSO
}
$ this- gt;log_write("Conectar al host de retransmisión ".$this-gt; Relay_host."\n" );
Devuelve VERDADERO
}
Función smtp_sockopen_mx($dirección)
{
$dominio = ereg_replace("^. @([^@] )$", "\1", $dirección);
Si (! @getmxrr($dominio, $MXHOSTS)) {
$this->log_write("Error: No se puede resolver MX\"). $dominio. \ " \ n ");
Devuelve FALSO
}
foreach ($MXHOSTS como $host) {
$ esto- gt;log_write("Probando ".$host.":".$this- gt;smtp_port.
" \ n ");
$this-gt;sock = @fsockopen($host,$this-gt;smtp_port,$errno,$errstr,$this-gt;time_out);< /p >
if (!($this-gt;socks amp$this-gt;smtp_ok())) {
$this-gt;log_write("Advertencia: No se puede conectar al host mx " .$host."\n");
$this->log_write("Error: ".$errstr." ("$errno")\n");
Continuar;
}
$ this- gt; log_write("Conectar al host mx".$host."\n");
Devuelve VERDADERO
}
$this->log_write("Error: No se puede conectar a ningún host mx (".implode(",",$MXHOSTS).") \n") ;
Devuelve FALSO
}
Función smtp_message($header, $body)
{
fputs ($this-gt;calcetines,$cabeza.\r\n.$cuerpo);
$this-gt;smtp_debug(" >.str_replace("\r\n ", " \n " " >, $encabezado. " \n gt". $cuerpo. " \n gt"));
Devuelve VERDADERO
}
Función smtp_eom. ()
{
fputs($ this- gt; calcetín, " \ r \ n . " \ r \ n ");
$this- gt;smtp_debug(".[EOM]\n");
return $this-gt;SMTP_ok();
}
Función smtp_ok()
{
$response = str_replace("\r\n ", "", fgets($ this- gt; sock, 512));
$ this->SMTP_debug($respuesta."\n");
Si (! ereg("^[23]", $respuesta)) {
fputs($ this- gt; sock, "QUIT\r\n");
fgets($ this - gt;socks, 512);
$ this-gt;log_write("Error: El host remoto devolvió \"".$response."\"\n");
Devuelve FALSO
}
Devuelve VERDADERO
}
Función smtp_putcmd($cmd, $arg = " ")
{
si ($arg!= "") {
si($ cmd == " ")$ cmd = $ arg
si no $cmd = $cmd. " ".$ arg
}
fputs($ this- gt; calcetines, cmd.
"\r\n");
$this-gt;smtp_debug(">.$cmd."\n");
return $this-gt;SMTP_ok () ;
}
Función smtp_error($string)
{
$ this- gt; log_write("Error: se produce un error en ".$cadena.".\ n ");
Devuelve FALSO
}
Función log_write($mensaje)
{
$this-gt;SMTP_debug($message);
if ($this-gt;log_file == "") {
Devuelve VERDADERO p>
}
$mensaje = fecha("M d H:i:s"). Obtener usuario actual(): ".$ mensaje
if (!@ file_exists($this-gt;log_file) ||!($FP = @fopen($this-gt;log_file,"a"))) {
$ this-gt;smtp_debug("Advertencia: No se puede abrir el archivo de registro \ " ". $ this-gt; log_file.\ " \n "
Devuelve FALSO; $fp, LOCK _ EX);
fputs($fp, $ mensaje
fclose($ FP);
Devuelve VERDADERO
}
Función bar_comment ($dirección)
{
$ comentario = " \([^()]*\)"; p>
mientras (ereg($comentario, $dirección)) {
$dirección = ereg_replace($comentario, "",$ dirección);
}
Devuelve $dirección;
}
Función get_address($dirección)
{
$ dirección = ereg _ reemplazar ("([\ t \ r \ n]) ","",$ dirección);
$dirección = ereg_replace ("^.* lt(.) gt;. *$ ", " \1 ", $ dirección);
Devuelve $dirección;
}
Función smtp_debug($mensaje)
{
if($this-gt;debug){
//echo $mensaje;
}
}
}