How do I send email via PHP from within an HTML file? - php

My website is HTML5. Consequently, my files are .html. I have a contact.html file that I would like to use to send a message from, using PHP. I don't have much experience with PHP (so if anyone could recommend a better alternative, non-.NET way of sending email, please let me know).
My initial thought was to include my PHP code inside my HTML file (whether or not this is possible or even recommended, I don't know). I've done this once before, and I believe I remember having a form tag that somewhere in its attributes specified the .php file that I used to send the email.
Something like <form someattribute="sendmail.php"> ... </form>.
QUESTION: Given what I THINK I should do (above), is this the best approach (specifying the PHP file inside my form tag), or do you recommend a better way to send email from a raw .html file?

You cannot do that only with HTML. If you stick to PHP solution, try
<?php
if(isset($_POST['send'])) //check the submit button was pressed
{
//get variables from POST array. Remember we specified POST method
$to = $_POST['to'];
$subject = $_POST['subject'];
$message = $_POST['message'];
//set up headers
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
//send the email and save the result
$result = mail($to, $subject, $message, $headers);
//was it sent?
if($result)
{
echo "Successfuly sent the email";
}
else
{
echo "An error has occured";
}
}
?>
<hr>
<form method="POST">
To: <input type="text" name="to"> <br>
Subject: <input type="text" name="subject"> <br>
Text: <textarea name="message"></textarea><br>
<input type="submit" value="Send" name="send">
</form>
You do not need to specify where the form points to because it is the same file. Otherwise it would be
<form action="somefile.php" method="POST">
Altough you have to specify the method POST, otherwise all the data will be sent through GET by default
PHP has a mail function that is used to send the email http://php.net/manual/en/function.mail.php
Returns TRUE if the mail was successfully accepted for delivery, FALSE
otherwise.
We check if the email is sent or not and print a corresponding message. Then, regardless of the result, we print out the message form.

You can easily send the mail by posting the data into a php file. Just need to write some codein that php file and in form user action='phpfilename.php'. Thats it.

If you're just trying to send the form info via e-mail it's fairly simple.
<form action="sendmail.php">
just need to make sure you're coding your php file correctly.

http://php.net/manual/en/function.mail.php
mail.html
<form action="mail.php" method="post">
To <input type="text" name="to"/><br/>
Subject <input type="text" name="subject"/><br/>
Message <textarea name="message"></textarea><br/>
<input type="submit" value="Send"/>
</form>
mail.php
<?php
mail($_POST["to"] , $_POST["subject"], $_POST["message"]);
header("Location: mail.html"); //redirect the user
?>

HTML is only client side, and is just markup so it cannot send an email. You should have a form that posts to a PHP page, as you suggest, and that PHP page sends the email.
http://www.w3schools.com/php/php_forms.asp
http://www.w3schools.com/php/php_mail.asp

Related

How do I add a form on HTML and send to my email using PHP

I am attempting to code a form on my HTML website with 5 fields and get them to send to my email using PHP. However, I am not sure why I am not able to successfully do that. I am able to get a positive feedback when I echo but when I add the mailto() part that's when everything gets complicated.
Would somebody be able to explain each step I will need to take in the procedure in creating a simple form using PHP from the very beginning. I would like to be able to use this for reference when coding forms – as well as anybody else that may need a reference.
Here's a basic email program that you can use:
$to = "person#example.com";
$subject = "Hello!";
$body = "Goodbye now. I am bored. Please give me pizza.";
mail($to, $subject, $body);
You just need those three parameters, and you can easily turn them into values from your form using this:
$to = $_POST["email"];
$subject = $_POST["email-subject"];
$body = $_POST["email-body"];
When the inputs in your form are this:
<input type="email" name="email" />
<input type="text" name="email-subject" />
<input type="text" name="email-body" />
You don't even need the mailto() mentioned in your question, just use this syntax:
mail(to, subject, message, headers, parameters);
And that's how you send an email in PHP.

How can I set up a contact form on my website? [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I've done my research on this but can't seem to get it to work.
I'm looking to add a contact form to my website that sends an email directly to me. I've watched videos and used code I've found online but nothing works. I even temporarily disabled my website and uploaded just a blank contact form (code below) and a php file (code below) with my only results being that the echo command at the end of the PHP file DOES show up. The email, though, still does not send.
What am I missing? Thank you!
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Temp</title>
</head>
<body>
<form method="post" action="send.php" name="contact_form">
<p>
<input name="name" type="text" />
</p>
<p>
<input name="email" type="text" />
</p>
<p>
<textarea name="message"></textarea>
</p>
<p>
<input type="submit" name="submit" id="submit" value="Submit" />
</p>
</form>
</body>
</html>
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = "email#myemail.co";
$subject = "Contact Form Submission";
mail ($to, $subject, $message, "From: " . $name);
echo "Your message has been sent. You can expect to hear from us soon.";
?>
I'm sure this is a duplicate, but I just wanted to mention a couple of things to check.
Does your mail() call return true? I would check that. It's possible that your PHP installation is not set up correctly to send mail. There are countless posts on how to check and configure that, which I would suggest reviewing if you haven't already (here's one, for example).
Depending on where you're hosting this, your host's configurations may restrict any outgoing mail that is not from the domain you're hosting. (I've had this problem myself on shared hosts.) Here you're setting the "from" header as the name of the person submitting the form (which would look something like: "From: John Doe"). This may be a problem either on the sending or receiving end of the email (either side rejecting it because it doesn't come from an email address, or a valid email address, etc). Try setting the "from" value to an email address valid on your host (e.g., "myname#mydomain.com"). Then, just include the person's name and email address in the $message of the email.
Hope that helps.

Attach File Through PHP Mail

I am trying to get a custom contact form using PHP mail to have a user attach a photo, that then gets sent to the recipient outlined in the PHP mail code
<input type="file" id="file" name="file">
The form code is as follows;
<form action="register-mail.php" method="POST" enctype="multipart/form-data">
<input type="file" id="file" name="file">
<input type="submit" value="Submit">
</form>
The PHP mail code is as follows;
<?php $file = $_FILES['file'];
$formcontent="Email Text Content";
$recipient = "fake#email.com";
$subject = "Here is a Photo";
$mailheader = 'From: Basic Sign-up <fake#email.com>' . "\r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
die();
?>
I can't seem to get it to attach the file to the email being sent. What am I doing wrong?
That is not how attachment works. Using the mail() for attachments is a little more complex than that. You got to tell mail() which part should handle the file attachment and which part is responsible to display the email body by setting up a MIME Boundary. In other words, the code should be divided into 2 parts:
A section to handle the message being sent in body
A section to handle file uploading
A detailed tutorial is here
PHP EMAIL WITH ATTACHMENT
However, I would suggest you to use a very handy tool called PHPMailer to do the same task. It simplifies the process and lets the class handle all the legwork.
PHPMailer

Contact Form Sends me Empty emails

I have this Contact form that works well. If someone sends me an email I get it. But for whatever reason I keep getting empty emails sent to me. Since no one could access that page im sure its not someone sending me the empty emails. I dont know what the problem is. Any help?
<form method="post" id="contactform" name="contactform" action="comment.php" id="ContactForm" name="ContactForm" method="post">
<fieldset>
<label>Email *</label>
<input class="text" type="text" name="email">
<label>Name *</label>
<input class="text" type="text" name="name" />
</fieldset>
<input class="submit-button" type="submit" name="submit" id="submit" value="Send" />
</form>
and my contact.php
<?php
$email.= "<b>Email : </b>".trim($_POST['company'])."<br/>";
$email.= "<b>Name : </b>".trim($_POST['name'])."<br/>";
//Replace YourEmailAddress#Yourdomain.com with yours, eg:contactus#mywebsite.com
//Both on the next line and on the mail function below.
$headers = "From: email#email.com\r\n";
$headers .= "Content-Type: text/html";
mail(
"Your Name<myname>",
"Header",
$email,
$headers
);
header("www.google.com");
?>
the "header" part in my php form is to redirec the user to a page after sending the form.
Thanks in advance.
You are probably getting visits from bots. Your script will always trigger an E-Mail, even if no POST data is present.
In your contact script, as a basic measure of protection, add something like
if ($_POST["submit"] != "Send")
die();
add further validation (as pointed out in the comments) as needed.
Might be because you don't appear to be validating the form inputs, so it can be submitted blank.
Sometimes I do this to websites (test validation, end up sending blank email), but I usually add a message later to "Validate your input!".
Excuse me if you are indeed doing validation, but that was my gut instinct because I see a lot of people fail to validate even the presence of a required input, let alone the integrity.
Company doesnt exist in your form but you try to parse it.
And maybe 2 form name declaration is not that good but its not your answer.

How can I add an "Email This" button that will NOT use mailto:

I want a button that does not just use a mailto: link. I want it to make either a popup, overlay or new window which has a simple form which allows anyone to email the link of the page they are on to another friend, with the subject and content pre-filled but editable.
Something similar to how you can do it on google maps by clicking "Send"
I thought this would be simple to find and I would be amazed if no-one has made some sort of easy javascript plugin or something.
I would rather not use something like "addthis" or other similar sharing aggreator service.
I have looked around here and scoured google but cannot find anything. If I missed a topic or if there is a good tutorial it would be much appreciated!
What you are asking for is only possible with server side code. Your server will need to have access to a mail server so that it can compose and send on behalf of your user. This has most definitely been prebuilt, but you will need to let us know what server side language you are using before someone can point you to a solution. If you don't understand server side code then you will have to resign yourself to using a mailto: link or a third party service.
UPDATE - Example
In your HTML
<form action="http://mysite.com/sendemail.php" method="post" name="myform" >
<input name="from" type="text" value="me#mysite.com" />
<input name="to" type="text" value="me#mysite.com" />
<input name="subject" type="text" value"Subject" />
<input name="body" type="text" value="Hello World" />
</form>
In your PHP file
$from = $_POST['from'] ;
$to = $_POST['to'];
$subject = $_POST['subject'] ;
$body = $_POST['body'] ;
mail($to, $subject, $body, "From:" . $from);
Because the form is set to method POST you can access each form element by its name and the _POST variable. So $_POST['from'] get the form elements whose name = "from".
For jQuery you can see the sharing plugins on offer here: http://plugins.jquery.com/plugin-tags/share
One of them sounds like what you need (Share Email):
A simple share this page via email
plugin that grabs the page title, url,
meta description and makes a mailto
link with Subject and Body pre-filled.

Categories