Why is this mail php script not working?

I don't know PHP very well and am trying to get a very simple PHP script to send emails. When they click the "Send" button, I get a "Thank you" message, but do not receive an email address.

<?php $name = $_POST['name']; $email = $_POST['email']; $web = $_POST['web']; $message = $_POST['message']; $formcontent="From: $name \n Website: $web \n Message: $message"; $recipient = " myemail@example.com "; // I do have my email here $subject = "Contact Form"; $mailheader = "From: $email \r\n"; mail($recipient, $subject, $formcontent, $mailheader) or die("Error!"); echo "Thank You!"; ?> 

here is the html form:

 <form action="mail.php" method="post" class="form"> <p class="name"> <label for="name">Name</label> <input type="text" name="name" id="name" /> </p> <p class="email"> <label for="email">E-mail</label> <input type="text" name="email" id="email" /> </p> <p class="web"> <label for="web">Website</label> <input type="text" name="web" id="web" /> </p> <p class="text"> <label for="web">Comments</label> <textarea name="message"></textarea> </p> <p class="submit"> <input type="submit" value="Send" /> </p> </form> 
0
source share
4 answers

I suggest you just use the PHPMAILER class for this, and you yourself can rest.

https://code.google.com/a/apache-extras.org/p/phpmailer/

This will be good support, so you don’t need to start worrying about editing many codes that will drain your time for other work.

Check the files included in the examples folder for SMTP examples, and use SMTP advanced or basic. With advanced SMTP, you can throw and catch your mistakes. This will help you find out where you have errors until all errors are resolved. See Code Example for Basic SMTP Codes.

 require_once('../class.phpmailer.php'); //include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded $mail = new PHPMailer(); $body = file_get_contents('contents.html'); $body = preg_replace('/[\]/','',$body); $mail->IsSMTP(); // telling the class to use SMTP $mail->Host = "mail.yourdomain.com"; // SMTP server $mail->SMTPDebug = 2; // enables SMTP debug information (for testing) // 1 = errors and messages // 2 = messages only $mail->SMTPAuth = true; // enable SMTP authentication $mail->Host = "mail.yourdomain.com"; // sets the SMTP server $mail->Port = 26; // set the SMTP port for the GMAIL server $mail->Username = " yourname@yourdomain "; // SMTP account username $mail->Password = "yourpassword"; // SMTP account password $mail->SetFrom(' name@yourdomain.com ', 'First Last'); $mail->AddReplyTo(" name@yourdomain.com ","First Last"); $mail->Subject = "PHPMailer Test Subject via smtp, basic with authentication"; $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test $mail->MsgHTML($body); $address = " whoto@otherdomain.com "; $mail->AddAddress($address, "John Doe"); $mail->AddAttachment("images/phpmailer.gif"); // attachment $mail->AddAttachment("images/phpmailer_mini.gif"); // attachment if(!$mail->Send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { echo "Message sent!"; } 

When you finish testing and want to stop outputting SMTP buffer messages, find this line that says

 $mail->SMTPDebug = 2; 

and replace it with

 $mail->SMTPDebug = false; $mail->do_debug = 0; 

Then you are good to go. Ask questions if necessary.

+1
source

Hm ... it seems good. Try:

 mail(' youremail@hotmail.com ', 'aSubject', 'aMessage'); 

all arguments are NOT , but the actual string is with single quotes .

Should appear in your regular / junk mail within the next 5 minutes.


If this does not work, then the script is not running, so change mail.php to simple:

 echo 'hi'; 

to make sure the script path is correct. what learns what is wrong, greetings.

+2
source

First of all, you should know that your code is subject to loading of headers by means of POST. You must use filter_var () ( http://php.net/manual/fr/function.filter-var.php ) to ensure that the value of $ email is safe.

Then there are several reasons that could prevent PHP from sending your emails.

The mail () function uses your MTA server (such as sendmail, postfix ...): you need to correctly install / configure it to send letters.

Your ISP may also block port 25 (to prevent spam).

Alternatively, you can use the php pear Mail and Net_SMTP classes to send email through SMTP. Abstract classes handle coding, attachments, etc. Convenient way.

0
source

I'm not a third-party developer, so I don’t know much about php, but this code worked for me, so let him understand.

 <p><span>Name</span><input class="contact" type="text" name="your_name" value="" /></p> <p><span>Email Address</span><input class="contact" type="text" name="your_email" value="" /></p> <p><span>Message</span><textarea class="contact textarea" rows="8" cols="50" name="your_message"></textarea></p> <p style="padding-top: 15px"><span>&nbsp;</span><input class="submit" type="submit" name="contact_submitted" value="Send" /></p> <?php $your_name = $_POST['your_name']; $your_email = $_POST[your_email]; $your_message = $_POST['your_message']; $recipient = " youremailhere@domain.com "; $subject = "New Message About BikeExcel"; mail($recipient , $subject, $your_message, "From " . $your_email); echo "Your Message Has Been Sent"; ?> 

So, instead of putting your email in the mail line, you create a variable that will define your email. It will also make your code very enjoyable, allowing you to look good in your portfolio. Hope this helps !!!!

0
source

Source: https://habr.com/ru/post/1501850/


All Articles