Php emailp

For some reason every time that I press the send button it sends the email but it does not post the information.

Please help. Thank you.

Code:

<?php
$name       = @trim(stripslashes($_POST['name'])); 
$from       = @trim(stripslashes($_POST['email'])); 
$subject    = @trim(stripslashes($_POST['subject'])); 
$message    = @trim(stripslashes($_POST['message'])); 
$to   		= '[email protected]';//replace with your email

$headers   = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: {$name} <{$from}>";
$headers[] = "Reply-To: <{$from}>";
$headers[] = "Subject: {$subject}";
$headers[] = "X-Mailer: PHP/".phpversion();

mail($to, $subject, $message, $headers);

die;



 
if (!isset($_POST["submit"])) { ?>
 
  <form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
   To: <input type="text" name="to_email"><br>
   From: <input type="text" name="from_email"><br>
   Subject: <input type="text" name="subject"><br>
   Message: <textarea rows="10" cols="20" name="message"></textarea><br>
   <input type="submit" name="submit" value="Send Email">
  </form>
<?php

 
  if (isset($_POST["to_email"])) {
    $to_email = $_POST["to_email"];
    $from_email = $_POST["from_email"];
    $subject = $_POST["subject"];
    $body = $_POST["message"];
	
    if ( mail($to_email, $subject, $body, $headers)) {
      echo("Email successfully sent to $to_email...");
    } else {
      echo("Email sending failed...");
    }
  }
}
?>

The line here that runs regardless, will always prevent the script from going past that point.

I tried it already but the script still wont post the information.

I don’t need to use the other half of the code except

<?php
$name       = @trim(stripslashes($_POST['name'])); 
$from       = @trim(stripslashes($_POST['email'])); 
$subject    = @trim(stripslashes($_POST['subject'])); 
$message    = @trim(stripslashes($_POST['message'])); 
$to   		= '[email protected]';//replace with your email

$headers   = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: {$name} <{$from}>";
$headers[] = "Reply-To: <{$from}>";
$headers[] = "Subject: {$subject}";
$headers[] = "X-Mailer: PHP/".phpversion();

mail($to, $subject, $message, $headers);

But it just wont post anything

What are you expecting and what does ‘post’ me to you?

I am expecting that the information that is put into the form would be sent to the with the script. I thought the POST would copy the information and have it put into the mail.

For example here is a code:

<?php $to_email = '[email protected]'; $subject = $_POST['subject']; $message = 'testing'; $headers = 'From: G'; mail($to_email,$subject,$message,$headers); ?>

I want to have the subject the text in the html text box.

Is this the form that is sending those values?

Send Now

I’m using this

Send Now

The form code

<form id="main-contact-form" name="contact-form" method="$post" action="sendemail.php">
                <div class="row  wow fadeInUp" data-wow-duration="1000ms" data-wow-delay="300ms">
                  <div class="col-sm-6">
                    <div class="form-group">
                      <input type="text" name="name" class="form-control" placeholder="Name" required="required">
                    </div>
                  </div>
                  <div class="col-sm-6">
                    <div class="form-group">
                      <input type="email" name="email" class="form-control" placeholder="Email Address" required="required">
                    </div>
                  </div>
                </div>
                <div class="form-group">
                  <input type="text" name="subject" class="form-control" placeholder="Subject" required="required">
                </div>
                <div class="form-group">
                  <textarea name="message" id="message" class="form-control" rows="4" placeholder="Enter your message" required="required"></textarea>
                </div>                        
                <div class="form-group">
                  <button type="submit" class="btn-submit">Send Now</button>
                </div>
              </form>

Try using phpmailer from github.

Make this script and save it as sendemail.php

<?php

use PHPMailer\PHPMailer\PHPMailer,
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require 'phpmailer/src/Exception.php';     //path to Exception.php 
require 'phpmailer/src/SMTP.php';  //path to SMTP.php
require 'phpmailer/src/PHPMailer.php'; //path to PHPMailer.php

// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);


if(isset($_POST['send'])){
    $name = $_POST['name'];
    $email = $_POST['email'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];

try {
    //Server settings
    $mail->SMTPDebug = 0;           // Enable verbose debug output 0= off use 1 or 3
    $mail->isSMTP();                        // Send using SMTP
    $mail->Host       = 'smtp.office365.com';     // Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    $mail->Username   = 'xxx';                     // SMTP username 
    $mail->Password   = 'xxx';                               // SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
    $mail->Port       = 587;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('xxx', optional name here');
    $mail->addAddress('xxx');     // Add a recipient
    $mail->addReplyTo($email);
   
// Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = $subject;
    $mail->Body    = $name .  "<br><br>" . $email  . "<br><br>" .  $message;
    $mail->AltBody = $name  . "<br><br>" . $email . "<br><br>" .  $message;

    $mail->send();
    echo 'Message has been sent';
    header("Refresh:0; url=index.php");
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
    
}}

So, there is an inherent issue with this function that has been known for years. It either sends to the junk folder, is rejected by the ISP as spam, or is never sent to begin with. PHPMailer, SwiftMail, or services like MailGun are better than using the mail() function

Sponsor our Newsletter | Privacy Policy | Terms of Service