1

I'm seriously struggling with this one. It might be an easy fix for those of you who know programming, I don't.

My problem is that it won't send an e-mail to my actual inbox. Am I missing something? What is it I don't understand.

PS! If needed the current site build is available at http://beta2.folldalreklame.no

The "site" is available for download at http://www.folldalreklame.no/StackOverflowHelp.zip

Please help.

                            <div class="col-md-8">
                                <h3>Send en melding</h3>
                                <form id="form-contact"  method="post" class="clearfix ts-form ts-form-email" data-php-path="assets/php/email.php">
                                    <div class="row">
                                        <div class="col-md-6 col-sm-6">
                                            <div class="form-group">
                                                <label for="form-contact-name">Navn *</label>
                                                <input type="text" class="form-control" id="form-contact-name" name="name" placeholder="Navn" required>
                                            </div>
                                            <!--end form-group -->
                                        </div>
                                        <!--end col-md-6 col-sm-6 -->
                                        <div class="col-md-6 col-sm-6">
                                            <div class="form-group">
                                                <label for="form-contact-email">E-post *</label>
                                                <input type="email" class="form-control" id="form-contact-email" name="email" placeholder="E-post" required>
                                            </div>
                                            <!--end form-group -->
                                        </div>
                                        <!--end col-md-6 col-sm-6 -->
                                    </div>
                                    <!--end row -->
                                    <div class="form-group">
                                        <label for="form-contact-subject">Emne *</label>
                                        <input type="text" class="form-control" id="form-contact-subject" name="subject" placeholder="Emne" required>
                                    </div>
                                    <!--end form-group -->
                                    <div class="row">
                                        <div class="col-md-12">
                                            <div class="form-group">
                                                <label for="form-contact-message">Melding *</label>
                                                <textarea class="form-control" id="form-contact-message" rows="5" name="message" placeholder="Melding" required></textarea>
                                            </div>
                                            <!--end form-group -->
                                        </div>
                                        <!--end col-md-12 -->
                                    </div>
                                    <!--end row -->
                                    <div class="form-group clearfix">
                                        <button type="submit" class="btn btn-primary float-right ts-btn-arrow" id="form-contact-submit">Send melding</button>
                                    </div>
                                    <!--end form-group -->
                                    <div class="form-contact-status"></div>
                                </form>
                                <!--end form-contact -->
                            </div>
                        </div>
                        <!--end row-->
                    </div>
                </div>
                <!--end container-->
            </section>
            <!--end #contact-->

Here's the email.php-code:

<?php
mb_internal_encoding("UTF-8");

$to = 'post@folldalreklame.no';
$subject = 'Melding via nettside';

$name = "";
$email = "";
$phone = "";
$message = "";

if( isset($_POST['name']) ){
    $name = $_POST['name'];

    $body .= "Name: ";
    $body .= $name;
    $body .= "\n\n";
}
if( isset($_POST['subject']) ){
    $subject = $_POST['subject'];
}
if( isset($_POST['email']) ){
    $email = $_POST['email'];

    $body .= "";
    $body .= "Email: ";
    $body .= $email;
    $body .= "\n\n";
}
if( isset($_POST['phone']) ){
    $phone = $_POST['phone'];

    $body .= "";
    $body .= "Phone: ";
    $body .= $phone;
    $body .= "\n\n";
}
if( isset($_POST['message']) ){
    $message = $_POST['message'];

    $body .= "";
    $body .= "Message: ";
    $body .= $message;
    $body .= "\n\n";
}

$headers = 'From: ' .$email . "\r\n";

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
mb_send_mail($to, $subject, $body, $headers);
    echo '<div class="status-icon valid"><i class="fa fa-check"></i></div>';
}
else{
    echo '<div class="status-icon invalid"><i class="fa fa-times"></i></div>';
}

1 Answers1

0

you need to set up a SMTP mail server with your web host provider once you have that set up it would be easier to use a library like PHPMailer PHPMailer

once you have dowloaded the library use the configuration script to set your SMTP details provided by your web host Example:

<?php
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);

try {
  //Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER;                      //Enable verbose debug output
$mail->isSMTP();                                            //Send using SMTP
$mail->Host       = 'smtp.example.com';                     //Set the SMTP server to send through
$mail->SMTPAuth   = true;                                   //Enable SMTP authentication
$mail->Username   = 'user@example.com';                     //SMTP username
$mail->Password   = 'secret';                               //SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            //Enable implicit TLS encryption
$mail->Port       = 465;                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`

//Recipients
$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net', 'Joe User');     //Add a recipient
$mail->addAddress('ellen@example.com');               //Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

//Attachments
$mail->addAttachment('/var/tmp/file.tar.gz');         //Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    //Optional name

//Content
$mail->isHTML(true);                                  //Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

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

You could also write your own if you so require but I found it easier using the library and adjusting it to my specific needs

What I did is wrapped this script into my own function that takes 3 parameters .

MySendMailfunction(ToEmail , Subject , Content);

everything else is wrapped in that function and does not need to be set as the other details in my class dont need to change

here is also a tutorial on how to set it up PHPMailer Youtube

Trust this helps

JonoJames
  • 1,117
  • 8
  • 22