سؤال

Firstly I would like to say I'm just a very newbie in php.

I have this simple php and as it is at the moment, when it sends the info it appears a horrible "Ok" message.

could you help me improving this php with: -better code (is it ok? could it be better?) -I would like to add a fancier answer (like in a box or something similar) in order to advice the user that his message has been sended.

Another thing is I am not sure this is working. I have xampp installed and locally it is not working. I also tried completing the form with all the files uploaded to the server and I´m still waiting for em....the miracle of the mail arriving to my inbox? =S Maybe I did something wrong?

<?php
if(isset($_POST["nombre"]) && isset($_POST["email"]) && isset($_POST["consulta"])){
$mymail = "myemail@gmail.com";
$subject = "Consulta";
$contenido = ":: Nombre: ". $_POST["nombre"]."\n";
$contenido .= ":: E-mail: ". $_POST["email"]."\n";
$contenido .= ":: Escribiò el siguiente mensaje: ". $_POST["consulta"]."\n";

$header = "From:".$_POST["email"]."\n"; 

mail($mymail, $subject, utf8_decode($contenido));*/
echo "&estatus=ok&";
}?>

Thanks in advance for your help! regards,

هل كانت مفيدة؟

المحلول

In terms of understanding PHP and the mail function, this Link is a pretty good resource on it (as is the site for PHP/web coding in general.
You're message to the user is on the last line where it says echo "&status=ok&"; Echo is the function that outputs text to the page. If you know html, you could output any html you would like e.g.

echo "<b>This success message is in BOLD</b>";

You'll want to be careful with quotes though, as you need to Escape them along with some other special characters, e.g.

echo "This \"code\" will\nprint like \\\"this\\\"";

will look like

This "code" will
print like \"this\"

Anyways, that's going off on a tangent. As for your xampp, you have to have your smtp settings configured. Check out this Post.

Let me know if your questions are answered :)

نصائح أخرى

Ok so your code looks fine to me, if you want to change the message you should change the text in the echo statement. like echo "Some fancy html in here";. The second issue about mail not receiving is probably due to the fact that php relies on the local smtp server by default to send mail, which on windows is nonexistent so its not sending it at all if you have xampp probably and on linux depending on many variables it might be sending it and its going in your spam folder or it might not be sending it at all. If you want to use a third party smtp server such as your gmail one just for testing purposes, the best and easiest way is to use some library such as:

http://swiftmailer.org/docs/sending.html

and define the third party smtp details.

The example code:

 require_once 'lib/swift_required.php';

// Create the Transport
$transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25)
  ->setUsername('your username')
  ->setPassword('your password')
  ;

/*
You could alternatively use a different transport such as Sendmail or Mail:

// Sendmail
$transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');

// Mail
$transport = Swift_MailTransport::newInstance();
*/

// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

// Create a message
$message = Swift_Message::newInstance('Wonderful Subject')
  ->setFrom(array('john@doe.com' => 'John Doe'))
  ->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name'))
  ->setBody('Here is the message itself')
  ;

// Send the message
$result = $mailer->send($message);

is pretty straightforward i think, and you can add also:

if($result){ echo "Message has been sent"; }else{ echo "Error!"; }

at the very end. That should do the trick...

btw the settings for gmail are:

$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl');

and use your gmail email and password for username and password accordingly...

This is a big question.

First you want to test locally and also with a simple mail client to check that your mail transport agent will send an email to a local account. No PHP

Then get mail going to a remote email account (gmail) again no php

Then get your PHP working sending locally.

Finally try a remote email posting from php.

Your script needs validation of input parameters from the input form, to avoid spamming, read eg www.w3schools.com for starters.

For examples code, download and look at some mature code such as Drupal or Wordpress and look how they do it. Since sending email ultimately depends on your transport agent and the remote end (SSL, TLS) which might require keys authentication in order to allow receipt of mail for relaying.

There are also many references and examples on Stack Exchange. You need to do a bit of research yourself.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top