0

I've been working on this for some time and am quite new to php. I'm having trouble getting this to send. A second set of eyes on this code would be most helpful:

<?php
    if(isset($_POST['submit'])){
        $to = "myEmail"; // this is your Email address
        $from = $_POST['emailAddress']; // this is the sender's Email address
        $fullName = $_POST['fullName'];
        $subject = "Form submission";
        $message = $fullName . " wrote the following:" . "\n\n" . $_POST['comment'];
        $message2 = "Here is a copy of your message " . $fullName . "\n\n" . $_POST['comment'];

        $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 " . $fullName . ", we will contact you shortly.";
        // You can also use header('Location: thank_you.php'); to redirect to another page.
    }
?>


<form method="post" action="contact.php">
    <div class="form-group">

        <label for="fullName">Name</label>
        <input type="text" class="form-control" id="fullName" name="fullName" placeholder="Jane Doe">

        <label for="emailAddress">Email</label>
        <input type="email" class="form-control" id="emailAddress" name="emailAddress" placeholder="jane.doe@example.com">

        <label for="comment">Comment</label>
        <textarea class="form-control" rows="3" name="comment" placeholder="Comment"></textarea>

        <button name="submit" type="submit" class="btn">Submit</button>

    </div>
</form>

Many thanks!

Guillaume
  • 1,032
  • 8
  • 16
  • What errors are you getting? Is the code getting inside the conditional? Can you run a one line mail() call with hardcoded values successfully? – Matt Mar 28 '17 at 15:38
  • 1
    On a local server the PHP mail function will not work, but you could use PhpMailer (https://github.com/PHPMailer/PHPMailer). – Martijn Bots Mar 28 '17 at 15:40
  • I don't see anything wrong in code. what error you are getting? also as @mkaatman moentioned, can you send email just with plain mail() function on a different page? – serdar.sanri Mar 28 '17 at 15:40
  • @MartijnBots `mail function will not work`?? why is that. It may not send email because of ISP restrictions ( most ISPs block smtp 25 port ) but It still can send mail locally – serdar.sanri Mar 28 '17 at 15:42
  • 1
    `$subject2` seems to be undefined? – Dave Forber Mar 28 '17 at 15:42
  • Also if you are on windows machine, you may check your php.ini SMTP settings. – serdar.sanri Mar 28 '17 at 15:43
  • @guyfawkes Only if you have set up a mail server. – Martijn Bots Mar 28 '17 at 15:44
  • yes, but that is only valid for a `windows` machine. when you say locally, it includes unix systems which sends email locally with mail() function out-of-box with apache and php installation. – serdar.sanri Mar 28 '17 at 15:48
  • If you use `mail()` function from a network with a dynamic IP address(your home, your work, etc...), the mail server that you are trying to send the email to (e.g: google, outlook,... ) will block you and prevent delivering your email to his clients for spamming protection purposes, because they are using a spam protection service that lists your IP . see this for more information[https://www.spamhaus.org](https://www.spamhaus.org/faq/section/Spamhaus%20PBL#183) .... you can use a mail server for testing on your local machine like [Mercury](http://www.pmail.com/) – Accountant م Mar 28 '17 at 16:06

2 Answers2

1

I would suggest to use PHPMailer. It is an open-source project available on GitHub : PHPMailer - GitHub

This class permits you to exploit the SMTP services of the most famous mailing platform to get a fast system to send mail using PHP.
It's really simple to set up a code with this class, starting from an HTML form :

<!DOCTYPE html>
<html>
<body>
<form method="post">
<input type="email" name="from" placeholder="From">
<input type="email" name="to" placeholder="To">
<input type="text" name="subject" placeholder="Subject">
<textearea name="content"></textarea>
</form>
</body>
</html>

<?php
require_once('PHPMailer_5.2.4\class.phpmailer.php');

if(isset($_POST["submit"])){
$username = 'YOUR_GMAIL_ACCOUNT';
$password = 'YOUR_ACCOUNT_PASSWORD';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
//$mail->addAttachment($_FILES["image"]["name"]); 
$mail->IsHTML(true);
$mail->Username = $username;
$mail->Password = $password;
$mail->SetFrom($_POST["from"]);
$mail->Subject = $_POST["subject"];
$mail->Body = $_POST["content"];
$mail->AddAddress($to);

if(!$mail->Send())
{
    echo "Mailer error : " . $mail->ErrorInfo . "<br>";
}
}
?>

As you can see in the PHP code, I'm using Gmail SMTP service to send this mail. Note that if you want to use other services you have to change the SMTP server.
Furthermore, you need to login into your email service to get an access to the SMTP service, and you need really often to enable the possibility of accessing your mailing account by third part applications too.
In some cases, the SMTP server won't accept TLS or SSL encryption.


Is it possible to add attachment adding enctype="multipart/data" attribute to your form tag and getting the uploaded file through the $_FILES array.
I hope this is going to help you!
DamiToma
  • 921
  • 3
  • 9
  • 27
0

In your code parameters of second mail function are not completed you didn't define value of subject2 I think your first message send in the right way but the second will not

Osama
  • 2,912
  • 1
  • 12
  • 15