1

I've been trying to send an email using PHP, and I've found this code that suits my needs as it will easily fit into my current website's code. However, I put this entire code into a file and tested it out, and it won't send the email.

Code:

<?php 
if(isset($_POST['submit'])){
    $to = "[email protected]"; // this is your Email address
    $from = $_POST['email']; // this is the sender's Email address
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $subject = "Form submission";
    $subject2 = "Copy of your form submission";
    $message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
    $message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];

    $headers = "From:" . $from;
    $headers2 = "From:" . $to;
    mail($to,$subject,$message,$headers);
    mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
    echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
    // You can also use header('Location: thank_you.php'); to redirect to another page.
    }
?>

<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>

<form action="" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>

</body>
</html> 

I am unable to get this to send an email to "[email protected]". Am I doing something wrong, or did I forgot to fill something out?

8
  • Are you using XAMPP, or something else for your local server? Chances are that you don't have a local mail server (such as sendmail) setup yet. These local environments almost never include a mail server, and so it needs to be setup separately. Commented Sep 18, 2016 at 22:16
  • @BrianSchroeter I enabled POP for my gmail, but still have no idea what I'm doing with these mail servers. Commented Sep 18, 2016 at 22:31
  • I'm happy to point you in the right direction. What software are you using to manage your local server? Common platforms are XAMPP, and WAMP. It could be a number of different things though. :) Commented Sep 18, 2016 at 22:34
  • I don't even think I have a local server yet... (Sorry if I frustrate you haha, new to PHP and mailing stuff) Commented Sep 18, 2016 at 22:42
  • 1
    Ah, okay! I think we're getting to the root of things. If you have HTML files on your local computer, a simple web browser like Chrome can open those. However, PHP is a server side script, or programming language. PHP requires a web server, such as Apache, for it to process the commands within the code. Without a web server, PHP is unable to do anything at all. You can setup a server on your own computer, using free software such as XAMPP. Commented Sep 19, 2016 at 1:28

2 Answers 2

1

As, you are using it locally, and your mail server is not configured:

You can configure Gmail SMTP for that to work like this:

// Pear Mail Library
require_once "Mail.php";

$from = '<[email protected]>';
$to = '<[email protected]>';
$subject = 'Hi!';
$body = "Hi,\n\nHow are you?";

$headers = array(
    'From' => $from,
    'To' => $to,
    'Subject' => $subject
);

$smtp = Mail::factory('smtp', array(
        'host' => 'ssl://smtp.gmail.com',
        'port' => '465',
        'auth' => true,
        'username' => '[email protected]',
        'password' => 'passwordxxx'
    ));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
    echo('<p>' . $mail->getMessage() . '</p>');
} else {
    echo('<p>Message successfully sent!</p>');
}

It also occurs sometimes that either your sender email or recipient email should be an email that is configured on your website's mail server or cPanel. Due to security reasons, most hosting service providers do not allow this thing. Change it to an email address which is configured on your website's mail server, it should work then.

Sign up to request clarification or add additional context in comments.

4 Comments

Well currently I am running everything locally, it's not up and hosted yet. Is there any way I can get this to work without it being up and online?
@joey942 client side? you mean locally hosted? unless you have a local mail server, this wont work
How can you send a mail without a mail server? Yes, there are alternatives like you can configure Gmail SMTP in your PHP!
Locally hosted, yeah, whoops. Alright thanks for the info.
1

the problem is not the code but you aren't able to send e-mail without a "SMTP" server! Link

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.