Tengo que hacer un código y recibir un correo electrónico automático del usuario cuando envía el formulario.

El código también consiste en insertar una consulta para poner todos los datos en una tabla de base de datos.

Sin embargo, la consulta Insertar y el correo automático funcionan bien. los datos se insertan y también recibo el correo con valores.

El código va para eso como:

<?php include('db.php'); ?>
<?php

$fname = $_POST['fname'];
$mname = $_POST['mname'];
$lname = $_POST['lname'];

$address = $_POST['address'];
$address2 = $_POST['address2'];
$city = $_POST['city'];
$state = $_POST['state'];
$zipcode = $_POST['zipcode'];
$country = $_POST['country'];

$email = $_POST['email'];
$phone = $_POST['phone'];
$altphone = $_POST['altphone'];

$citizenship = $_POST['citizenship'];
$visa = $_POST['visa'];
$inviteletter = $_POST['inviteletter'];
$housingassist = $_POST['housingassist'];
$im = $_POST['im'];
$status = 'open';

$university = $_POST['university'];
$univstartdate = $_POST['univstartdate'];
$univgraddate = $_POST['univgraddate'];
$univaddress = $_POST['univaddress'];
$univcity = $_POST['univcity'];
$univstate = $_POST['univstate'];
$univzipcode = $_POST['univzipcode'];
$univcountry = $_POST['univcountry'];

$usmle = implode(', ', $_POST['usmle']);
$rotationlength = $_POST['rotationlength'];
$speciality1 = $_POST['speciality1'];
$speciality2 = $_POST['speciality2'];
$speciality3 = $_POST['speciality3'];
$research = $_POST['research'];
$additional = $_POST['additional'];

$startdate = $_POST['startdate'];
$freecall = $_POST['freecall'];

$howus = $_POST['howus'];


$date = date_default_timezone_set('America/New_York');
$date = date('M-d,Y H:i:s');
$date2 = date('M-d,Y');

$conn = new mysqli ($servername, $dbusername, $dbpassword, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO applications (fname, mname, lname, address, address2, city, state, zipcode, country, email, phone, altphone, citizenship, visa, inviteletter, housingassist, im, university, univstartdate, univgraddate, univaddress, univcity, univstate, univzipcode, univcountry, usmle, rotationlength, speciality1, speciality2, speciality3, research, additional, startdate, freecall, howus, date, status)

VALUES ('$fname', '$mname', '$lname', '$address', '$address2', '$city', '$state', '$zipcode', '$country', '$email', '$phone', '$altphone', '$citizenship', '$visa', '$inviteletter', '$housingassist', '$im', '$university', '$univstartdate', '$univgraddate', '$univaddress', '$univcity', '$univstate', '$univzipcode', '$univcountry', '$usmle', '$rotationlength', '$speciality1', '$speciality2', '$speciality3', '$research', '$additional', '$startdate', '$freecall', '$howus', '$date2', '$status')";
if ($conn->query($sql) === TRUE) {


$email_from = 'newyorkrotations@gmail.com';//<== update the email address
$email_subject = "Student Application";
$email_body = "First Name : $fname.\n 
               Middle Name : $mname.\n 
               Last Name : $lname.\n 
               Address : $address.\n
               Alt Address : $address2.\n 
               City : $city.\n
               State : $state.\n
               Zipcode : $zipcode.\n
               Country : $country.\n
               Email : $email.\n
               Phone : $phone.\n
               Alt Phone : $altphone.\n
               Citizenship : $citizenship.\n
               Does He Need Visa : $visa.\n
               Does He Need Invite Letter : $inviteletter.\n
               Does He Need Housing Assistance : $housingassist.\n
               He is A : $im.\n
               University : $university.\n
               College Start Date : $univstartdate.\n
               Graduation Date : $univgraddate.\n
               University Address : $univaddress.\n
               University City : $univcity.\n
               University Stqate : $univstate.\n
               University Zipcode : $univzipcode.\n
               University Country : $univcountry.\n
               Did He took Any Steps : $usmle.\n
               Requested Rotation Length : $rotationlength.\n
               Speciality #1 : $speciality1.\n
               Speciality #2 : $speciality2.\n
               Speciality #3 : $speciality3.\n
               Research : $research.\n
               Additional Services : $additional.\n
               Rotation Start Date : $startdate.\n
               Do He need physician's Free call : $freecall.\n
               How did He Reached Us : $howus.\n
               Application Submitted at : $date2.\n";

$to = "newyorkrotations@gmail.com";//<== update the email address
$headers = "From: $email \r\n";
$headers .= "Reply-To: $email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: index.php');

}
else {
    echo "ERROR" . $sql . "<br>" . $conn->error;
}
$conn->close();
?>

Sin embargo, intenté agregar algo de código HTML al código para que el correo que recibo se vea un poco agradable. Pero, en cambio, recibo el código completo tal como está en mi correo. Significa que el HTML se envía al correo como texto sin formato.

El código para eso como:

$email_from = 'newyorkrotations@gmail.com';//<== update the email address
$email_subject = "Student Application";

$message = '<html><body>';
            $message .= '<img src="http://css-tricks.com/examples/WebsiteChangeRequestForm/images/wcrf-header.png" alt="Website Change Request" />';
            $message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
            $message .= "<tr style='background: #eee;'><td><strong>First Name:</strong> </td><td>" . strip_tags($_POST['fname']) . "</td></tr>";
            $message .= "<tr><td><strong>Middle Name:</strong> </td><td>" . strip_tags($_POST['mname']) . "</td></tr>";
            $message .= "<tr><td><strong>Last Name:</strong> </td><td>" . strip_tags($_POST['lname']) . "</td></tr>";
            $message .= "<tr><td><strong>Address:</strong> </td><td>" . strip_tags($_POST['address']) . "</td></tr>";
            $message .= "<tr><td><strong>Alt Address:</strong> </td><td>" . strip_tags($_POST['address2']) . "</td></tr>";
            $message .= "<tr><td><strong>City:</strong> </td><td>" . strip_tags($_POST['city']) . "</td></tr>";
            $message .= "<tr><td><strong>State:</strong> </td><td>" . strip_tags($_POST['state']) . "</td></tr>";
            $message .= "<tr><td><strong>ZipCode:</strong> </td><td>" . strip_tags($_POST['zipcode']) . "</td></tr>";
            $message .= "<tr><td><strong>Country:</strong> </td><td>" . strip_tags($_POST['country']) . "</td></tr>";
            $message .= "<tr><td><strong>Email:</strong> </td><td>" . strip_tags($_POST['email']) . "</td></tr>";
            $message .= "<tr><td><strong>Phone:</strong> </td><td>" . strip_tags($_POST['phone']) . "</td></tr>";
            $message .= "<tr><td><strong>Alt Phone:</strong> </td><td>" . strip_tags($_POST['altphone']) . "</td></tr>";
            $message .= "<tr><td><strong>Citizenship:</strong> </td><td>" . strip_tags($_POST['citizenship']) . "</td></tr>";
            $message .= "<tr><td><strong>Does He Have Visa:</strong> </td><td>" . strip_tags($_POST['visa']) . "</td></tr>";
            $message .= "<tr><td><strong>Does He Need Invite Letter:</strong> </td><td>" . strip_tags($_POST['inviteletter']) . "</td></tr>";
            $message .= "<tr><td><strong>Does He Need Housing Assist:</strong> </td><td>" . strip_tags($_POST['housingassist']) . "</td></tr>";
            $message .= "<tr><td><strong>He Is:</strong> </td><td>" . strip_tags($_POST['im']) . "</td></tr>";
            $message .= "<tr><td><strong>University Name:</strong> </td><td>" . strip_tags($_POST['university']) . "</td></tr>";
            $message .= "<tr><td><strong>University Start Date:</strong> </td><td>" . strip_tags($_POST['univstartdate']) . "</td></tr>";
            $message .= "<tr><td><strong>University Graduation Date:</strong> </td><td>" . strip_tags($_POST['univgraddate']) . "</td></tr>";
            $message .= "<tr><td><strong>University Address:</strong> </td><td>" . strip_tags($_POST['univaddress']) . "</td></tr>";
            $message .= "<tr><td><strong>University City:</strong> </td><td>" . strip_tags($_POST['univcity']) . "</td></tr>";
            $message .= "<tr><td><strong>University State:</strong> </td><td>" . strip_tags($_POST['univstate']) . "</td></tr>";
            $message .= "<tr><td><strong>Universtiy Zipcode:</strong> </td><td>" . strip_tags($_POST['univzipcode']) . "</td></tr>";
            $message .= "<tr><td><strong>University Country:</strong> </td><td>" . strip_tags($_POST['univcountry']) . "</td></tr>";
            $message .= "<tr><td><strong>Steps Taken:</strong> </td><td>" . strip_tags($_POST['usmle']) . "</td></tr>";
            $message .= "<tr><td><strong>Rotation Length:</strong> </td><td>" . strip_tags($_POST['rotationlength']) . "</td></tr>";
            $message .= "<tr><td><strong>Speciality #1:</strong> </td><td>" . strip_tags($_POST['speciality1']) . "</td></tr>";
            $message .= "<tr><td><strong>Speciality #2:</strong> </td><td>" . strip_tags($_POST['speciality2']) . "</td></tr>";
            $message .= "<tr><td><strong>Speciality #3:</strong> </td><td>" . strip_tags($_POST['speciality3']) . "</td></tr>";
            $message .= "<tr><td><strong>Research:</strong> </td><td>" . strip_tags($_POST['research']) . "</td></tr>";
            $message .= "<tr><td><strong>Additional:</strong> </td><td>" . strip_tags($_POST['additional']) . "</td></tr>";
            $message .= "<tr><td><strong>Rotation Start Date:</strong> </td><td>" . strip_tags($_POST['startdate']) . "</td></tr>";
            $message .= "<tr><td><strong>Does He Need A Free Call:</strong> </td><td>" . strip_tags($_POST['freecall']) . "</td></tr>";
            $message .= "<tr><td><strong>How did he find us:</strong> </td><td>" . strip_tags($_POST['howus']) . "</td></tr>";
            $message .= "</table>";
            $message .= "</body></html>";

$to = "newyorkrotations@gmail.com";//<== update the email address
$headers = "From: $email \r\n";
$headers .= "Reply-To: $email \r\n";
//Send the email!
mail($to,$email_subject,$message,$headers);
//done. redirect to thank-you page.
header('Location: index.php');

Simplemente reemplace $ email_body con $ message con un código HTML.

Pero se ejecutó como texto en su lugar.

Se agradece cualquier ayuda.

0
Harish K 20 feb. 2018 a las 11:12

2 respuestas

La mejor respuesta

No ha proporcionado encabezados adecuados, consulte este ejemplo

<?php
$to = 'maryjane@email.com';
$subject = 'Marriage Proposal';
$from = 'peterparker@email.com';

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Create email headers
$headers .= 'From: '.$from."\r\n".
    'Reply-To: '.$from."\r\n" .
    'X-Mailer: PHP/' . phpversion();

// Compose a simple HTML email message
$message = '<html><body>';
$message .= '<h1 style="color:#f40;">Hi Jane!</h1>';
$message .= '<p style="color:#080;font-size:18px;">Will you marry me?</p>';
$message .= '</body></html>';

// Sending email
if(mail($to, $subject, $message, $headers)){
    echo 'Your mail has been sent successfully.';
} else{
    echo 'Unable to send email. Please try again.';
}
?>
0
Danish Kundra 20 feb. 2018 a las 08:17

Tal vez debería agregar el tipo de contenido HTML a sus $ encabezados

$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
0
Klian 20 feb. 2018 a las 08:16