Sending Email with PHP

Asked

Viewed 394 times

2

I’m with a website where the server just block sending email from another provider, only allowing sending with the email from the domain itself, I made the change from FROM to an email from my domain and it worked, but the email received appears with the sender that andmail that I changed, I would like to know if it has to change to appear the completed email form, remembering that it has to be only in the view, because the sending has to be with the email of the provider because another is blocked, follows the code that I am using:

<?php

$nome = $_POST['nome'];
$email = $_POST['email'];
$assunto = $_POST['assunto'];
$msg = $_POST['mensagem'];

$to = "[email protected]";

$subject = "Formulário de Contato";

$menssage = "<strong> Nome: </strong> $nome <br/><br/> <strong> E-mail: </strong> $email <br/><br/> <strong> Assunto: </strong> $assunto <br/><br/> <strong> Mensagem: </strong> $msg";

$header = "MIME-Version: 1.0\n";
$header .= "Content-type: text/html; charset=iso-8859-1\n";
$header .= "From: $to\n";

mail($to, $subject, $menssage, $header);

echo "Mensagem enviada com sucesso";

?>
  • Try to print what the $email variable is receiving. To make sure that the information that is coming in your php is correct. echo $email;

  • $email is what comes from the form, it was previously configured for the $to variable to receive this information, but when changing the server configuration started to show authorization error, I had to change $to an e-domain mail, but wanted to continue to appear in the sender the contents of $email

1 answer

2

Try to test like this:

<?php

$nome = $_POST['nome'];
$email = $_POST['email'];
$assunto = $_POST['assunto'];
$msg = $_POST['mensagem'];

$to = "[email protected]";

$subject = "Formulário de Contato";

$menssage = "<strong> Nome: </strong> $nome <br/><br/> <strong> E-mail: </strong> $email <br/><br/> <strong> Assunto: </strong> $assunto <br/><br/> <strong> Mensagem: </strong> $msg";

$emailsender = "[email protected]";

$header = "MIME-Version: 1.0\n";
$header .= "Content-type: text/html; charset=iso-8859-1\n";
$header .= "From: $to\n";

mail($to, $subject, $menssage, $header, "-r".$emailsender);

echo "Mensagem enviada com sucesso";

?>

From special attention to variable $emailsender, on it you should put an existing email on your domain, example: [email protected].

Other comment: In Linux it is mandatory to use the -r parameter (concatenation of "From on the sending line") because the sending is done through Postfix.

Any questions, just talk.

  • The code as it works, I made the change that you said and continued to work, what I would like to change is the following, the way that the sender of the message is $to, in this case Rogerio.santana@hospitalsantatereza,com.br I would like it to be the $email that is the field that comes from the form, at King Host this is blocked I can’t tell you that $to is an email any it necessarily has to be an email from the domain, but I wanted at least the visualization to be the e-form mail, I was wondering if you have how to do this

  • Got it, in case you ever tried to add $headers .= 'From: Suporte <[email protected]>' . "\r\n";

  • Same thing, in the case of From I have to use an email from the domain, but I wanted the email received to be from the person who filled out the form, it looks like this: De: [email protected] [mailto:[email protected]] Posted: Wednesday, January 18, 2017 12:39 To: [email protected] Subject: Hospital Santa Tereza - Contact Form Name: Rogério do Carmo Santana E-mail: [email protected] Subject: Suggestions Message: test

  • I wanted this From: field to be the email of the client who filled out the form, just like we do with Outlook when sending an email using another box changing only the "To:" something like I add a line $header .= implode ( " n",array ( "From: $to", "Reply-To: $email", "Return-Path: $email","MIME-Version: 1.0","X-Priority: 3","Content-Type: text/html; charset=UTF-8" ) ); Which makes answering it already fill the reply with the correct email, but is not yet the ideial

Browser other questions tagged

You are not signed in. Login or sign up in order to post.