How to add SMTP authentication to this php code?


I need to add to this php code validation smtp of gmail or my own domain, I do not know how to do it since this form I got it on the net and I am beginning to familiarize myself with the php language. Thank you!

/* Set e-mail recipient */
$myemail = "[email protected]";
$subject = "email title";

/* Check all form inputs using check_input function*/
$name = check_input($_POST['name'], "Favor de ingresar su nombre");
$company = check_input($_POST['company']);
$telephone = check_input($_POST['telephone'], "Favor de ingresar su telefono.");
$email = check_input($_POST['email']);
$message = check_input($_POST['message'], "Favor de escribir un mensaje.");

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
show_error("El correo que escribio no es valido.");
/* Let's prepare the message for the e-mail */
$message = "

Este mensaje fue enviado desde su sitio

Nombre: $name
Empresa: $company
Telefono: $telephone
E-mail: $email



/* Send the message using mail() function */
mail($myemail, $subject, $message);

/* Redirect visitor to the thank you page */
header('Location: index2.html');

/* Functions we used */
function check_input($data, $problem='')
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
return $data;

function show_error($myError)

<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>

asked by Jese Reyes 29.06.2016 в 18:45

1 answer


Well, since you can use a library, try using PHPmailer . Here you have an example of how to send mails using the smtp authentication of gmail through this library.

Although to use it with the smtp of gmail you have to previously activate the access to less secure applications, for this: enter link , once there go to Applications and connected sites and activate the access of less secure applications

require '../PHPMailerAutoload.php';
//Crea una nueva instancia PHPMailer
$mail = new PHPMailer;
//Usar SMTP
//Habilita el SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//hostname del servidor de correo
$mail->Host = '';

//Setea numero del puerto - 587 TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Encryptacion a usar ssl o tls
$mail->SMTPSecure = 'tls';
//Autenticacion SMTP
$mail->SMTPAuth = true;
//Username para la autenticación - direccion correo gmail
$mail->Username = "[email protected]";
//Password gmail
$mail->Password = "yourpassword";
//quien envia
$mail->setFrom('[email protected]', 'First Last');
//direccion alternativa
$mail->addReplyTo('[email protected]', 'First Last');
//quien recibe
$mail->addAddress('[email protected]', 'John Doe');
$mail->Subject = 'PHPMailer GMail SMTP test';
//Cuerpo del mensaje
$mail->Body = "Gmail mail test";
//Envia el mensaje, y revisa si existen errores
if (!$mail->send()) {
    echo "Mail Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
answered by 29.06.2016 в 19:00