Skip to content Skip to sidebar Skip to footer

Php Email Script With Custom Mail Body

I'm using a PHP email script for a contact form. There are six fields for the form: Name Email Phone number Booking Date Booking Time Comments There's also a hidden honeypot fiel

Solution 1:

I have modified Zoltan's code a little bit. Should work now.

<?php$robotest = $_POST['robotest']; //just testin' for robots$recipient = "info@mydomain.com"; //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

Solution 2:

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";

Solution 3:

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";

Solution 4:

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?

  • You don't have to think that much about email delivery.
  • Statistics. Let's you track Total Sent/Clicks/Opens/Bounces.
  • Often web service-based instead of SMTP. I.e. easier to handle.
  • Cleaner code (at least if you use AlphaMail that separates data from presentation).
  • Scalable and future proof.

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 = newstdClass();

$data->booking = newstdClass();
$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", "from@example.com"))
    ->setReceiver(new EmailContact("Joe Doe", "to@example.org"))
    ->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#>Warmregards,Robot

Post a Comment for "Php Email Script With Custom Mail Body"