Reputation: 21150
I'm using a PHP email script for a contact form. There are six fields for the form:
There's also a hidden honeypot field for robotest. The PHP script is as follows:
<?php
$robotest = $_POST['robotest']; //just testin' for robots
$recipient = "[email protected]"; //recipient
$email = ($_POST['email']); //senders e-mail adress
if((filter_var($email, FILTER_VALIDATE_EMAIL)) && ($robotest == "")) {
$Name = ($_POST['name']); //senders name
$mail_body = !!!----> WHAT DO I PUT HERE <----!!!!
$subject = "Porter Inquiry"; //subject
$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields
mail($recipient, $subject, $mail_body, $header); //mail command :)
} else {
print "You've entered an invalid email address!";
}
?>
You'll notice above, where I put !!!----> WHAT DO I PUT HERE <----!!!
, I'm unsure how to get multiple fields into the mail body. I'd like to include something like:
"Hello,
You have received a new booking with the following details:
Booking Time: ($_POST['time']) Booking Date: ($_POST['date'])
Additional customer comments: ($_POST['comments']);
Please respond to the customer within 30 minutes on the following
phone number: ($_POST['phone'])
Warm regards,
Robot."
I can't find any info on how to successfully achieve this, would greatly appreciate some guidance.
Upvotes: 2
Views: 10612
Reputation: 2764
Disclosure: I'm one of the developers behind AlphaMail
If you want to handle this really easy, I would recommend that you use a Transactional Email Service like:
Why?
If you choose to go with AlphaMail you could use the AlphaMail PHP-client.
Example:
include_once("comfirm.alphamail.client/emailservice.class.php");
$email_service = AlphaMailEmailService::create()
->setServiceUrl("http://api.amail.io/v1")
->setApiToken("YOUR-ACCOUNT-API-TOKEN-HERE");
$data = new stdClass();
$data->booking = new stdClass();
$data->booking->time = $_POST['time'];
$data->booking->date = $_POST['date'];
$data->booking->comment = $_POST['comments'];
$data->booking->phone = $_POST['phone'];
$response = $email_service->queue(EmailMessagePayload::create()
->setProjectId(12345) // Your AlphaMail project (determines template, options, etc)
->setSender(new EmailContact("Sender Company Name", "[email protected]"))
->setReceiver(new EmailContact("Joe Doe", "[email protected]"))
->setBodyObject($data) // Any serializable object
);
Another advantage with AlphaMail is that you can edit your templates directly in the AlphaMail Dashboard, and you can format your emails using the Comlang template language. This would make it possible to create templates like the one below (text version example, but you can easily create an HTML version).
Hello,
You have received a new booking with the following details:
Booking Time: <# payload.booking.time #>
Booking Date: <# payload.booking.date #>
Additional customer comments: <# payload.booking.comment #>
Please respond to the customer within 30 minutes on the following phone number: <# payload.booking.phone #>
Warm regards,
Robot
Upvotes: 0
Reputation: 47657
You put there almost exactly what you've quoted in your question. You can write it as a very long string or use the concatenation operator:
$mail_body = "Hello, \r\n";
$mail_body .= "You have received a new booking with the following details: \r\n";
$mail_body .= "Booking Time: (" . $_POST['time'] .") Booking Date: (". $_POST['date'] .") \r\n";
$mail_body .= "Additional customer comments: (". $_POST['comments'] ."); \r\n";
$mail_body .= "Please respond to the customer within 30 minutes on the following phone number: (". $_POST['phone'] .") \r\n";
$mail_body .= "Warm regards, \r\n";
$mail_body .= "Robot. \r\n";
Upvotes: 2
Reputation: 2086
Try sending an HTML mail
Modify your mail body like this (Of course, you can do more changes in it)
$mailBody = "Hello,<br/><br/>";
$mailBody .= "You have received a new booking with the following details:<br/><br/>";
$mailBody .= "Booking Time: ".$_POST['time']." Booking Date: ".$_POST['date']." <br/><br/><br/>";
$mailBody .= "Additional customer comments: ".$_POST['comments']."<br/><br/>";
$mailBody .= "Please respond to the customer within 30 minutes on the following<br/>";
$mailBody .= "phone number: ".$_POST['phone']."<br/><br/>";
$mailBody .= "Warm regards,<br/><br/>";
$mailBody .= "Robot.";
and $header
like this
$header = "From: ". $Name . " <" . $email . ">\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
Upvotes: 2
Reputation: 3105
I have modified Zoltan's code a little bit. Should work now.
<?php
$robotest = $_POST['robotest']; //just testin' for robots
$recipient = "[email protected]"; //recipient
$email = ($_POST['email']); //senders e-mail adress
if((filter_var($email, FILTER_VALIDATE_EMAIL)) && ($robotest == "")) {
$Name = ($_POST['name']); //senders name
$mail_body = "Hello, \r\n";
$mail_body .= "You have received a new booking with the following details: \r\n";
$mail_body .= "Booking Time: ({$_POST['time']}) Booking Date: ({$_POST['date']}) \r\n";
$mail_body .= "Additional customer comments: ({$_POST['comments']}); \r\n";
$mail_body .= "Please respond to the customer within 30 minutes on the following phone number: ({$_POST['phone']}) \r\n";
$mail_body .= "Warm regards, \r\n";
$mail_body .= "Robot. \r\n";
$subject = "Porter Inquiry"; //subject
$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields
mail($recipient, $subject, $mail_body, $header); //mail command :)
} else {
print "You've entered an invalid email address!";
}
?>
Hope this helps... and yes this will send mail even when the fields are empty(except the recipient field)
Dins
Upvotes: 3