2

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.

  • I checked spam box it is not there too

  • I checked phpinfo SMTP port open and 25

  • I checked phpinfo senmail_path /usr/sbin/sendmail -t -i

  • I call and send e-mail to hosting company they said everyting is okay server side check your sendmail script.

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         = 'tradertarik@gmail.com';

$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>
Itrader
  • 25
  • 6
  • What do the logs say? – Julian Kuchlbauer Jul 19 '16 at 08:44
  • `mail($to, $subject, $message, $headers)` put this inside `if() { }` and check if it returns `FALSE`. – Alok Patel Jul 19 '16 at 08:45
  • Uh, how about checking the result of `mail()`? `var_dump(mail..)` If it reports that you didn't send the file, the issue is probably in your server configuration. – Christian Jul 19 '16 at 08:45
  • Possible duplicate of [PHP mail form doesn't complete sending e-mail](http://stackoverflow.com/questions/24644436/php-mail-form-doesnt-complete-sending-e-mail) – CBroe Jul 19 '16 at 09:26
  • mail($to, $subject, $message, $headers) I have changed as timclutton suggest to mail($to, $subject, $message, implode("\r\n", $headers)); I receive e-mail but without sender name message or topic. What it could be now? – Itrader Jul 19 '16 at 09:29

2 Answers2

1

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));
timclutton
  • 12,682
  • 3
  • 33
  • 43
  • Hi timcluttonI just receive e-mail but without topic message and sender name. Empty e-mail. What it could be wrong? – Itrader Jul 19 '16 at 09:20
  • You'll need to debug it yourself by checking the contents of the variables before calling `mail`. Use something like `var_dump($to, $subject, $message, implode("\r\n", $headers));` to see the contents of all variables. – timclutton Jul 19 '16 at 10:07
0

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! 
        }   
}
al3x2ndru
  • 598
  • 5
  • 8
  • Alex thanks for answer but already I freak out) I am not programmer and highly possible after this isue I will never need to hear about php again. Just I need to find out simple way to fix it. – Itrader Jul 19 '16 at 10:02