0

Im having trouble figuring out why wont i get an email when i try to send myself an email (using different email). ill include the forum, php and javascript code.

Below is the php code

$from = '';

$sendTo = 'myemailhere';

$subject = '';


$fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message'); 


$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';


$errorMessage = 'There was an error while submitting the form. Please try again later';


error_reporting(0);

try
{

    if(count($_POST) == 0) throw new \Exception('Form is empty');

    $emailText = "You have a new message from your contact form\n=============================\n";

    foreach ($_POST as $key => $value) {

        if (isset($fields[$key])) {
            $emailText .= "$fields[$key]: $value\n";
        }
    }


    $headers = array('Content-Type: text/plain; charset="UTF-8";',
        'From: ' . $from,
        'Reply-To: ' . $from,
        'Return-Path: ' . $from,
    );


    mail($sendTo, $subject, $emailText, implode("\n", $headers));

    $responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
    $responseArray = array('type' => 'danger', 'message' => $errorMessage);
}

if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    $encoded = json_encode($responseArray);

    header('Content-Type: application/json');

    echo $encoded;
}

else {
    echo $responseArray['message'];
}

below is the javascript code

$(function () {

$('#contact-form').validator();



$('#contact-form').on('submit', function (e) {


    if (!e.isDefaultPrevented()) {
        var url = "contact.php";


        $.ajax({
            type: "POST",
            url: url,
            data: $(this).serialize(),
            success: function (data)
            {

                var messageAlert = 'alert-' + data.type;
                var messageText = data.message;


                var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' + messageText + '</div>';


                if (messageAlert && messageText) {

                    $('#contact-form').find('.messages').html(alertBox);

                    $('#contact-form')[0].reset();
                }
            }
        });
        return false;
    }
})
});

Below is the forum code

<form id="contact-form" method="post" action="contact.php" role="form">

                <div class="messages"></div>

                <div class="controls">

                    <div class="row" style="margin: auto;">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="form_name">Firstname</label>
                                <input id="form_name" type="text" name="name" class="form-control" placeholder="Please enter your firstname *" required="required" data-error="Firstname is required.">
                                <div class="help-block with-errors"></div>
                            </div>
                        </div>
                    </div>
                        <div class="row" style="margin: auto;">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="form_lastname">Lastname</label>
                                    <input id="form_lastname" type="text" name="surname" class="form-control" placeholder="Please enter your lastname *" required="required" data-error="Lastname is required.">
                                    <div class="help-block with-errors"></div>
                                </div>
                            </div>
                        </div>

                    <div class="row" style="margin: auto;">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="form_email">Email</label>
                                <input id="form_email" type="email" name="email" class="form-control" placeholder="Please enter your email *" required="required" data-error="Valid email is required.">
                                <div class="help-block with-errors"></div>
                            </div>
                        </div>
                    </div>
                    <div class="row" style="margin: auto;">
                        <div class="col-md-12">
                            <div class="form-group">
                                <label for="form_message">Message</label>
                                <textarea id="form_message" name="message" class="form-control" placeholder="Message for me *" rows="4" required="required" data-error="Please, leave us a message."></textarea>
                                <div class="help-block with-errors"></div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <input type="submit" class="btn btn-success btn-send float-right" value="Send message">
                        </div>
                    </div>
                    </div>
                </div>

    </form>

I havent dabbled much into php but i know that this should work but im not sure why it doesn't work. Ive tried sending myself emails when on a live server yet nothing seems to work, so im out of ideas on why this does not send an email. any help is really appreciated because i have no idea why it doesnt work.

wzrd
  • 25
  • 3
  • 3
    Possible duplicate of [PHP mail function doesn't complete sending of e-mail](https://stackoverflow.com/questions/24644436/php-mail-function-doesnt-complete-sending-of-e-mail) – Patrick Q Jul 18 '19 at 19:37
  • What debugging have you already done? – Patrick Q Jul 18 '19 at 19:38
  • Try using `\r\n` for headers. I've found that sometimes it makes a difference. – aynber Jul 18 '19 at 19:39
  • doesn't look like there's a "from" in the headers... (don't send those if it's defined in php.ini) Seems like you'd have a pretty specific error for that, though. Most servers use TLS these days, so it could be related to not setting that up as well. – pcalkins Jul 18 '19 at 21:31

1 Answers1

0

Make sure you have a SMTP server available.

I was trying to send email from a local server using : https://github.com/verot/class.upload.php , but I didn't receive anything until I started working on the remote server (live server as you have said). Take a look at phpmailer, this worked for me.

Example:

require_once("phpmailer/class.phpmailer.php");

$mail = new PHPMailer(true);
$mail->CharSet = "UTF-8";
$mail->isSMTP();
$mail->From = 'donotreply@xxx.com';
$mail->FromName = "Your company's name";
$mail->AddAddress("$email","");
$mail->isHTML(true);
$mail->Body = "Hi";
$mail->Send();
acuz3r
  • 43
  • 9