My php code for sending email is not being loaded

Asked

Viewed 239 times

-1

I have the following code to send email from my form

<?php
// alterar a variavel abaixo colocando o seu email
$destino = "[email protected]";
// emails para quem será enviado o formulário
$assunto = "Contato pelo Site";

// // variaveis
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$subject = $_POST['subject'];
$message = $_POST['message'];

// // monta o e-mail na variavel $body
$body = "===================================" . "\n";
$body .= "Contato - TESTE" . "\n";
$body .= "===================================" . "\n\n";
$body .= "Nome: " . $name . "\n";
$body .= "Email: " . $email . "\n";
$body .= "Telefone: " . $phone . "\n";
$body .= "Assunto: " . $subject . "\n";
$body .= "Mensagem: " . $message . "\n\n";
$body .= "===================================" . "\n";

// // É necessário indicar que o formato do e-mail é html
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'From: $name <$email>';

////envio
mail("$destino, $assunto, $body, $headers");

////confimaçao
echo '<a href="Index.php">Voltar para formulário de cadastro</a>';
?>

and my form code to call this php function is like this

		 <form action="envio.php" method="post" name="form" class="col-md-9 go-right">
			<div class="form-group">
			<input id="name" name="name" type="text" class="form-control" required>
			<label for="name">Nome</label>
		</div>
		<div class="form-group">
			<input id="email" name="email" type="text" class="form-control" required>
			<label for="email">Email</label>
		</div>
		<div class="form-group">
			<input id="phone" name="phone" type="tel" class="form-control" required>
			<label for="phone">Celular</label>
		</div>	
		<div class="form-group">
			<input id="subject" name="subject" type="text" class="form-control" required>
			<label for="subject">Assunto</label>
		</div>
		<div class="form-group">
			<textarea id="message" name="message" class="form-control" required></textarea>
			<label for="message">Deixe sua mensagem</label>
		</div>	
			<button type="submit" value="submit">
				 <input name="submit" value="Enviar"><br>
			</button>
		</form>

Apparently everything is ok, but when I click 'Send' it loads upload.php as if it were a page, instead of executing the code that is in the file. Of course it gives page not found for sending.php, and the email is not enough, because I believe the code is not running.

Can someone help me? Thank you.

  • Hello Caroline, a php file should be strictly finished with the extension ". php", make sure that the file extension was not created after that. I suggest looking at the terminal.

  • Hello André, I took a look here by the terminal and it is right, is sending.php

  • You have to see if the mail function is active. Open the php.ini, look for the line [mail function] and adjust the SMTP and smtp_port values.

  • 1

    I believe it is active yes, because I configured through a plugin in WP, and when sending email test by this plugin, it arrives normally. But I’ll take a look at the php.ini file here just to have ctz.

  • From what your comment indicates, you are not in trouble, specifically to send email, but rather to interpret a PHP code. When Apache/IIS/Nginx/etc explicitly delivers its source code, it means that PHP is not installed correctly. Obviously this should occur if you access any other page PHP in this same environment. Ideal is to first identify what is happening with your environment and then start debugging your email submission script.

  • Another thing! You are putting the arguments of the call to mail in quotes: mail("$destino, $assunto, $body, $headers");. It’s wrong! You’ve turned what should be multiple arguments into a single argument (string).

  • Hi, I really don’t. All pages are php and all work normally, php is running ok, but I will make the tips you gave me, if not for sure try this alternative.

  • So, another user already informed me, and it’s already changed, and yet, it hasn’t worked.

  • So. From now on, your problem is in the interpretation of the file. Make sure that this file is calling the PHP interpreter: <?php in the first line. In your question is ok, but it’s worth checking out. - I checked your code by simply fixing the arguments of mail and it worked great, although my environment does not actually send the emails. Ahhh! Also try to change the encoding of this file. Usually they are coded in UTF8.

  • 1

    @Augustovasques Vasques checked the php.ini file and is ok

  • Fine, I’ll change the encoding. And thanks for the hints. and is in the first line yes the <php?

  • Nothing yet, anyone has any other idea ? Thank you

Show 7 more comments

1 answer

0

I ran a test using Xampp and the.php upload page was executed, but it showed an error message.

On the sending page, make the following change in line 31

mail($destino, $assunto, $body, $headers);

In your form, you can change the send button to stay as follows:

<button type="submit">Enviar</button>

If instead of running the programming it is printing the commands on the screen, it is because the server is not recognizing the extension . php

As you commented that you are working with Wordpress, I believe this tutorial will help you, it teaches you to create a contact form without using plugins:

https://blog.da2k.com.br/2015/02/22/wordpress-criando-um-formulario-contacto-sem-plugin-parte-1/

https://blog.da2k.com.br/2015/02/24/wordpress-criando-um-formulario-contacto-sem-plugin-parte-2/

https://blog.da2k.com.br/2015/02/28/wordpress-criando-um-formulario-contacto-sem-plugin-parte-3/

  • Hello, I changed the lines you said. And it wasn’t the same >: What can I do for the server to recognize the . php extension of this file? I’m working for Wordpress, if it makes a difference to anything

  • I edited the reply by inserting a tip on how to create a custom contact form with Wordpress

  • I did this tutorial, I put everything initially, but it continued the same way. Ai took some parts of the code, such as checking whether the fields are blank or not, and these details that my form already contains. And the only difference now, is that it reloads the page, instead of appearing page not found. But it does not arrive the email the same way. Have any idea why the server is not running this file the way it should?

  • Nothing yet, do you have any other idea ? Thank you

Browser other questions tagged

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