<?php
ini_set("mail.log", "/tmp/mail.log");
ini_set("mail.add_x_header", TRUE);
define("TITLE", "Contact Us | MicroUrb");
include('includes/header.php');
/*
NOTE:
In the form in contact.php, the name text field has the name "name".
If the user submits the form, the $_POST['name'] variable will be automatically created and will contain the text they typed into the field. The $_POST['email'] variable will contain whatever they typed into the email field.
PHP used in this script:
pre_match()
- Perform a regular expression match
- http://ca2.php.net/preg_match
$_POST
- An associative array of variables passed to the current script via the HTTP POST method.
- http://www.php.net/manual/en/reserved.variables.post.php
trim()
- Strip whitespace (or other characters) from the beginning and end of a string
- http://www.php.net/manual/en/function.trim.php
exit
- output a message and terminate the current script
- http://www.php.net/manual/en/function.exit.php
die()
- Equivalent to exit
- http://ca1.php.net/manual/en/function.die.php
wordwrap()
- Wraps a string to a given number of characters
- http://ca1.php.net/manual/en/function.wordwrap.php
mail()
- Send mail
- http://ca1.php.net/manual/en/function.mail.php
*/
?>
<div id="contact">
<hr>
<h1 class="contact">Get in touch with us!</h1>
<?php
// Check for header injections
function has_header_injection($str) {
return preg_match("/[\r\n]/", $str);
}
if (isset($_POST['contact_submit'])) {
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$msg = $_POST['message'];
// Check to see if $name or $email have header injections
if (has_header_injection($name) || has_header_injection($email)) {
die(); // If true, kill the script
}
if (!$name || !$email || !$msg) {
echo '<h4 class="error">All fields required.</h4><a href="contact.php" class="button block">Go back and try again</a>';
exit;
}
// Add the recipient email to a variable
$to = "renaissance.scholar2012@gmail.com";
// Create a subject
$subject = "$name sent you a message via your contact form";
// Construct message
$message = "Name: $name\r\n";
$message .= "Email: $email\r\n";
$message .= "Message:\r\n$msg";
// If the subscribed checkbox was checked
if (isset($_POST['subscribe']) && $_POST['subscribe'] == 'Subscribe') {
// Add a new line to message variable
$message .= "\r\n\r\nPlease add $email to the mailing list.\r\n";
}
$message = wordwrap($message, 72);
// Set mail headers into a variable
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: $name <$email> \r\n";
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: High\r\n";
// Send the email
mail($to, $subject, $message, $headers);
?>
<!-- Show success message afte email has been sent -->
<h5>Thanks for contacting MicroUrb!</h5>
<p>Please allow 24 hours for a response.</p>
<p><a href="index.php" class="button block">« Go to Home Page</a></p>
<?php } else { ?>
<form method="post" action="" id="contact-form">
<label for="name">Your name</label>
<input type="text" id="name" name="name">
<label for="email">Your email</label>
<input type="email" id="email" name="email">
<label for="message">and your message</label>
<textarea id="message" name="message"></textarea>
<input type="checkbox" id="subscribe" name="name" value="Subscribe">
<label for="">Subscribe to newsletter</label>
<input type="submit" class="button next" name="contact_submit" value="Send Message">
</form>
<?php } ?>
<hr>
</div>
<?php include('includes/footer.php'); ?>
I've tried creating a simple mail form. The form itself is on my contact.php page. I would like to test that the code submits perfectly and sends the email.
I knew that there would be a chance that via MAMP or MAMP Pro the sending of the email would not work, but I have found documentation that says you can make it work. I followed this documentation:
https://gist.github.com/zulhfreelancer/4663d11e413c76c6393fc135f72a52ce
and it did not work for me. I have tried reaching out to the author to no avail.
This is what I have tried to do thus far:
I have made sure error reporting is enabled and set to report all errors and I have that code in my index.php file here:
<?php ini_set('error_reporting', E_ALL); ini_set('display_errors', 'On'); define("TITLE", "Home | MicroUrb"); include('includes/header.php'); ?> <div id="philosophy"> <hr> <h1>MicroUrb's Philosophy</h1> <p>Here at MicroUrb we guarantee you organically grown produce, because we grew it ourselves and we are local. We're not pompous, we're proud. We're proud of our work, our quality, our environment and our love for fresh local produce and family.</p> <p>Thank you, from your local urban family farm.</p> <hr> </div><!-- philosophy --> <?php include('includes/footer.php'); ?>
If you look at my code in contact.php, you will see that the mail() function is being called.
I checked MAMP Pro logs and for Postfix I had nothing because Postfix appears to not be running (could this be the problem?) For the Apache logs I had this error:
unknown: warning: /etc/postfix/main.cf, line 696: overriding earlier entry: inet_protocols=all sendmail: warning: /etc/postfix/main.cf, line 676: overriding earlier entry: inet_protocols=ipv4 sendmail: warning: /etc/postfix/main.cf, line 696: overriding earlier entry: inet_protocols=all postdrop: warning: /etc/postfix/main.cf, line 696: overriding earlier entry: inet_protocols=all
but which was caused by following Step 2 of the link I shared above, where it directed me to add this:
myorigin = live.com
myhostname = smtp.live.com
relayhost = smtp.live.com:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_sasl_mechanism_filter = plain
inet_protocols = ipv4
smtp_use_tls = yes
smtp_tls_security_level=encrypt
tls_random_source=dev:/dev/urandom
Line eight above was conflicting with inet_protocols = all, so I deleted line 8 above and the problem went away, but the original problem of not being able to send mail is still there.
I am not using an error suppression operator.
I may need to check to see if mail() returns true or false.
I checked spam folders of the email accounts I tried sending mail to.
I believe I have supplied all mail headers:
// Set mail headers into a variable $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/plain; charset=iso-8859-1\r\n"; $headers .= "From: $name <$email> \r\n"; $headers .= "X-Priority: 1\r\n"; $headers .= "X-MSMail-Priority: High\r\n";
I believe the recipient value is correct:
// Send the email mail($to, $subject, $message, $headers);
I tried multiple email accounts, same issue.
I believe the form method matches the code.
Is Postfix on my MAMP Pro not working the problem?
I enabled PHP custom mail.log.
Should I just call it quits and use a different mailer or different way of developing my contact form in PHP that will be less of a headache?
So I believe my question is not a duplicate of the other posting in question is because as outline above, I have gone through the troubleshooting steps of that very SO posting and it has not solved my problem.
I can only guess my problem is either more specific to getting Postfix working on my MAMP Pro. I am not checking if mail() returns true or false correctly or I just may need to go with PHPMailer or some alternative.