Jump to content

Recommended Posts

Posted (edited)

Olá, boa tarde!

Tenho um script que envia os emails do formulário de uma página de contactos.html (/MailHandler.php), no entanto não está a chegar a informação porque o servidor onde está alojado o site requer autenticação de envio SMTP.

A empresa de hospedagem mandou um exemplo de codigo em .php (abaixo)

Dúvida: é possível adaptar o MailHandler.php de forma a o formulário enviar com autenticação smtp?

Código de exemplo para formulário fornecido pela empresa de alojamento:

__________

<?php

if(isset($_POST["submit"])){

$nome_completo = $_POST["nome_completo"];
$email = $_POST["email"];
$telefone = $_POST["telefone"];
$fax = $_POST["fax"];
$comentario = $_POST["comentario"];

require_once "Mail.php";

$from = "de@deminio.pt";
$to = "para@dominio.pt";
$subject = "Assunto do email";

$body = "Nome: ".$nome_completo;
$body.= "\n";
$body.= "Telefone: ".$telefone;
$body.= "\n";
$body.= "Fax:".$fax;
$body.= "\n";
$body.= "\n";
$body.= nl2br($comentario);


$smtpinfo["host"] = "localhost";
$smtpinfo["port"] = "25";
$smtpinfo["auth"] = true;
$smtpinfo["username"] = "dominio@dominio.com";
$smtpinfo["password"] = "password";

$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);

$mail_object = Mail::factory("smtp", $smtpinfo);

$mail = $mail_object->send($to, $headers, $body);

if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
}else{
echo '<p><b>O seu comentario foi enviado com sucesso.</b></p>';
}


}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Formulario de contacto</title>
<style>
form ul
{
font-size:100%;
list-style-type:none;
margin:0;
padding:0;
width:100%;
}
#form_container
{
background:#fff;

margin:0 auto;
text-align:left;
width:640px;
}
form li
{
display:block;
margin:0;
padding:4px 5px 2px 9px;
position:relative;
}
</style>
</head>
<body>

<div id="form_container">
<h1>
<a>Formulário de contacto</a>
</h1>
<form method="post" action="">
<ul>

<li>
<label >Nome Completo </label>
<div>
<input name="nome_completo" maxlength="255" type="text"/>
</div>
</li>

<li>
<label>Email </label>
<div>
<input name="email" maxlength="255" type="text"/>
</div>
</li>

<li>
<label>Telefone </label>
<div>
<input name="telefone" maxlength="255" type="text"/>
</div>
</li>

<li>
<label>Fax </label>
<div>
<input name="fax" maxlength="255" type="text"/>
</div>
</li>

<li>
<label>Comentário </label>
<div>
<textarea name="comentario" maxlength="255"><textarea>
</div>
</li>


<li>
<input value="Enviar" name="submit" type="submit" />
</li>

</ul>
</form>
</div>
</body>
</html>

___________

Código do arquivo MailHandler.php

____________

<?php
$owner_email = $_POST["owner_email"];
$headers = 'From:' . $_POST["email"];
$subject = 'A message from your site visitor ' . $_POST["name"];
$messageBody = "";

if($_POST['name']!='nope'){
$messageBody .= '<p>Visitor: ' . $_POST["name"] . '</p>' . "\n";
$messageBody .= '<br>' . "\n";
}
if($_POST['email']!='nope'){
$messageBody .= '<p>Email Address: ' . $_POST['email'] . '</p>' . "\n";
$messageBody .= '<br>' . "\n";
}else{
$headers = '';
}
if($_POST['state']!='nope'){
$messageBody .= '<p>State: ' . $_POST['state'] . '</p>' . "\n";
$messageBody .= '<br>' . "\n";
}
if($_POST['phone']!='nope'){
$messageBody .= '<p>Phone Number: ' . $_POST['phone'] . '</p>' . "\n";
$messageBody .= '<br>' . "\n";
}
if($_POST['fax']!='nope'){
$messageBody .= '<p>Fax Number: ' . $_POST['fax'] . '</p>' . "\n";
$messageBody .= '<br>' . "\n";
}
if($_POST['message']!='nope'){
$messageBody .= '<p>Message: ' . $_POST['message'] . '</p>' . "\n";
}

if($_POST["stripHTML"] == 'true'){
$messageBody = strip_tags($messageBody);
}

try{
if(!mail($owner_email, $subject, $messageBody, $headers)){
throw new Exception('mail failed');
}else{
echo 'mail sent';
}
}catch(Exception $e){
echo $e->getMessage() ."\n";
}
?>

___________

Desde já obrigado a todos! 👍

Edited by scorch
Adicionado Geshi.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.