Itrader
Itrader

Reputation: 21

How can I fix sendmail.php doesn't work?

I have website, I am not pro but already I have finished except sendmail problem. When I click "send now" button message appear on screen as "thanks for your message" but I do not receive any e-mail.

here it is sendmail.php file;

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

$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;

I need help to find out what is the problem. I checked other questions and asnwers somehow it wasn't helpfull for me. Thanks in advance.

P.S. I am not sure anybody is going to need form html code but I think it will be good to add it too.

    <div class="contact-form wow fadeIn" data-wow-duration="1000ms" data-wow-delay="600ms">
      <div class="row">
        <div class="col-sm-6">
          <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>
                </div>
              </div>
              <div class="col-sm-6">
                <div class="form-group">
                  <input type="email" name="email" class="form-control" placeholder="Email Address" required>
                </div>
              </div>
            </div>
            <div class="form-group">
              <input type="text" name="subject" class="form-control" placeholder="Subject" required>
            </div>
            <div class="form-group">
              <textarea name="message" id="message" class="form-control" rows="4" placeholder="Enter your message" required></textarea>
            </div>                        
            <div class="form-group">
              <button type="submit" class="btn-submit">Send Now</button>
            </div>
          </form>   
        </div>

Upvotes: 2

Views: 455

Answers (2)

al3x2ndru
al3x2ndru

Reputation: 648

Mail function from php is tricky to use if the sendmail is not properly configure. Instead of, I did used https://github.com/PHPMailer/PHPMailer (don't freak out, you only need 2 files from there class.phpmailer.php and class.smtp.php).

a example of send_mail function based on PHPMailer

function send_mail($subject, $body, $altbody, $to, $name, $attach = '')
{
$mail = new PHPMailer(true);
$mail->IsHTML(true);
$mail->IsSMTP();
$mail->CharSet = 'text/html; charset=UTF-8;';

$mail->Host       = "some.external.smtp.server";    // SMTP server example
$mail->SMTPDebug  = 0;                      // enables SMTP debug information (for testing)
$mail->SMTPAuth   = true;                   // enable SMTP authentication
$mail->Port       = port_for_external_smtp;                     // set the SMTP port for the GMAIL server
$mail->Username   = "username_for_external_smtp_server";        // SMTP account username example
$mail->Password   = "pass";             // SMTP account password example

try 
    { 
        $mail->setFrom('address', 'name'); 
        $mail->addReplyTo($to, $name); 
        $mail->addAddress($to, $name);
        $mail->Subject = $subject; 
        $mail->Body = $body;
        $mail->isHTML(true);
        $mail->AltBody = $altbody;  // altbody if for text only mail
        if (!($attach == ''))
        {
                $mail->AddAttachment($attach); // attachment 
            }
        $mail->send(); 
        } 
catch (phpmailerException $e) 
    { 
        echo $e->errorMessage(); //Pretty error messages from PHPMailer 
        } 
catch (Exception $e) 
    { 
        echo $e->getMessage(); //Boring error messages from anything else! 
        }   
}

Upvotes: 0

timclutton
timclutton

Reputation: 13024

Your $headers parameter is incorrect; it should be string not array. From the PHP manual:

String to be inserted at the end of the email header.

This is typically used to add extra headers (From, Cc, and Bcc). Multiple extra headers should be separated with a CRLF (\r\n). If outside data are used to compose this header, the data should be sanitized so that no unwanted headers could be injected.

You should be able to fix the problem like so:

mail($to, $subject, $message, implode("\r\n", $headers));

Upvotes: 1

Related Questions