Php form action send to email

PHP Send Email with Attachments

Sending emails with attachments is a common task while developing PHP applications, achieved using PHP’s built-in mail() function. The mail() function allows developers to send emails with text or HTML content and attach one or more files.

Here’s a sample PHP code that demonstrates how to send an email with an attachment in PHP:

[email protected]"; // subject of the email $subject = "Email with Attachment"; // message body $message = "This is a sample email with attachment."; // from $from = "[email protected]"; // boundary $boundary = uniqid(); // header information $headers = "From: $from\r\n"; $headers .= "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: multipart/mixed; boundary=\".$boundary.\"\r\n"; // attachment $attachment = chunk_split(base64_encode(file_get_contents('file.pdf'))); // message with attachment $message = "--".$boundary."\r\n"; $message .= "Content-Type: text/plain; charset=UTF-8\r\n"; $message .= "Content-Transfer-Encoding: base64\r\n\r\n"; $message .= chunk_split(base64_encode($message)); $message .= "--".$boundary."\r\n"; $message .= "Content-Type: application/octet-stream; name=\"file.pdf\"\r\n"; $message .= "Content-Transfer-Encoding: base64\r\n"; $message .= "Content-Disposition: attachment; filename=\"file.pdf\"\r\n\r\n"; $message .= $attachment."\r\n"; $message .= "--".$boundary."--"; // send email if (mail($to, $subject, $message, $headers)) < echo "Email with attachment sent successfully."; >else < echo "Failed to send email with attachment."; >?> 

The recipient’s email address, subject, message body, and header information are defined in the code above. The boundary variable separates the message and attachment in the email.

Читайте также:  Правила хорошего кода css

The attachment is read from a file using file_get_contents() and encoded using base64_encode() . The chunk_split() function splits the attachment into smaller chunks for sending.

Finally, the mail() function sends the email with an attachment. If the email has been sent successfully, the function will return true ; Otherwise, it will return false .

Send HTML Form Data Over Email Using PHP

  1. Add an HTML form to your webpage to collect the desired information from users.
  2. When submitting the HTML form, use PHP code to retrieve the form data and store it in variables.
  3. In the above PHP code, replace the hardcoded values of the variables with the input names of the form data.

Here’s an example HTML code for a form that allows you to submit an email with the recipient’s email address, subject, message, sender’s email address, and an attachment:





In the code above, the form’s action is set to «send-email.php», where the form data will be sent for processing. The enctype attribute is set to multipart/form-data to indicate that the form will contain both text and binary data (i.e., the file attachment). The input fields allow you to enter the recipient email, subject, message, sender email and select a file for attachment. The submit button is labeled «Send».

Below is a code of the «send-email.php» page, which receives data and sends emails using the above HTML form:

In conclusion, sending an email with attachments in PHP is a straightforward process that can be achieved using the mail() function and format the email correctly.

Источник

PHP form to email explained

It is a common requirement to have a form on almost any web site.

In this article, we will create a PHP script that will send an email when a webform is submitted.

There are two parts to the webform:

  1. The HTML form code for the form. The HTML code below displays a standard form in the web browser. If you are new to HTML coding, please see: HTML form tutorial
  2. The PHP script for handling the form submission. The script receives the form submission and sends an email.

HTML code for the email form:

 form method="post" name="myemailform" action="form-to-email.php">  Enter Name: input type="text" name="name">  Enter Email Address: input type="text" name="email">  Enter Message: textarea name="message">textarea>  input type="submit" value="Send Form">  form> 

The form contains the following fields: name, email, and message.

name and email are single-line text input fields whereas ‘message’ is a text area field (multi-line text input).

You can have different types of input fields in a form. Please see the HTML form input examples page for details.

On hitting the submit button, the form will be submitted to “form-to-email.php”. This form is submitted through the POST method

Accessing the form submission data in the PHP script

Once your website visitor has submitted the form, the browser sends the form submission data to the script mentioned in the ‘action’ attribute of the form. (for the current form, the script is form-to-email.php)

Since we have the form submission method mentioned as POST in the form (method=‘post’) we can access the form submission data through the $_POST[] array in the PHP script.

The following code gets the values submitted for the fields: name, email, and message.

 php  $name = $_POST['name'];  $visitor_email = $_POST['email'];  $message = $_POST['message']; ?> 

Composing the email message

Now, we can use the above PHP variables to compose an email message. Here is the code:

 php  $email_from = 'yourname@yourwebsite.com';   $email_subject = "New Form submission";   $email_body = "You have received a new message from the user $name.\n".  "Here is the message:\n $message". ?> 

The ‘From’ address, the subject and the body of the email message are composed in the code above. Note the way the body of the message is composed using the variables.

If a visitor ‘Anthony’ submits the form, the email message will look like this: «You have received a new message from the user Anthony. Here is the message: Hi, Thanks for your great site. I love your site. Thanks and Bye. Anthony.»

Sending the email

The PHP function to send email is mail() .

mail(to,subject,message,headers) 

For more details, see the PHP mail() page.

The headers parameter is to provide additional mail parameters ( like the from address, CC, BCC etc)

Here is the code to send the email:

 php   $to = "yourname@yourwebsite.com";   $headers = "From: $email_from \r\n";   $headers .= "Reply-To: $visitor_email \r\n";   mail($to,$email_subject,$email_body,$headers);   ?> 

Notice that we put your email address in the ‘From’ parameter and the visitor’s email address in the ‘Reply-To’ parameter. The ‘From’ parameter should indicate the origin of the email. If you put the visitor’s email address in the ‘From’ parameter, some email servers might reject the email thinking that you are impersonating someone.

Sending the email to more than one recipients

If you want to send the email to more than one recipients, then you just need to add these in the “$to” variable.

 php  $to = "name1@website-name.com, name2@website-name.com,name3@website-name.com";   mail($to,$email_subject,$email_body,$headers); ?> 

You can use the CC (carbon copy) and BCC (Blind Carbon Copy) parameters as well. The CC and BCC emails are added in the ‘headers’ parameter.

Sample code:

 php $to = "name1@website-name.com, name2@website-name.com,name3@website-name.com";  $headers = "From: $email_from \r\n";  $headers .= "Reply-To: $visitor_email \r\n";  $headers .= "Cc: someone@domain.com \r\n";  $headers .= "Bcc: someoneelse@domain.com \r\n";  mail($to,$email_subject,$email_body,$headers); ?> 

Securing the form against email injection

Spammers are looking for exploitable email forms to send spam emails. They use the form handler script as a ‘relay’. What they do is to submit the form with manipulated form values. To secure our form from such attacks, we need to validate the submitted form data.

All the values that go in the ‘headers’ parameter should be checked to see whether it contains \r or \n. The hackers insert these characters and add their own code to fool the function.

 php function IsInjected($str)   $injections = array('(\n+)',  '(\r+)',  '(\t+)',  '(%0A+)',  '(%0D+)',  '(%08+)',  '(%09+)'  );   $inject = join('|', $injections);  $inject = "/$inject/i";   if(preg_match($inject,$str))    return true;  >  else    return false;  > >  if(IsInjected($visitor_email))   echo "Bad email value!";  exit; > ?> 

In general, any value used in the header should be validated using the code above.

Better, complete validations could be done using the PHP form validation script here.

PHP form to email complete code

The link below contains the complete form, validation and emailing code.

Источник

HTML Form to Email with Attachment using PHP

html to email with attachments

Our goal is to create an HTML form with a file field, when someone clicks the submit button we want to receive the data with attachment in our email.

Before we started to this everyone for the big support my HTML form to PHP mail post. As per my subscriber’s request, I made this new tutorial. to send any attachment via HTML form to the email we can use phpmail function. here is am giving an example of how to do it.

Create form for html to email with attachments

First create an HTML form with enctype=”multipart/form-data” because we are transferring files .

this is just a HTML boilerplate. you can add your class and modify with your website design.

Now we have to create our PHP file which will process the data when the form submit.

Here we are getting the data from the form and making a message string and encoding the uploaded attachment into base64 format and send it to your email address,

When the email hit your inbox it will decode the base64 object and display the attachment

mail.php file

[email protected]’; //if u dont have an email create one on your cpanel $mailto = ‘[email protected]’; //the email which u want to recv this email $content = file_get_contents($fileName); $content = chunk_split(base64_encode($content)); // a random hash will be necessary to send mixed content $separator = md5(time()); // carriage return type (RFC) $eol = «\r\n»; // main header (multipart mandatory) $headers = «From: «.$fromname.» » . $eol; $headers .= «MIME-Version: 1.0» . $eol; $headers .= «Content-Type: multipart/mixed; boundary=\»» . $separator . «\»» . $eol; $headers .= «Content-Transfer-Encoding: 7bit» . $eol; $headers .= «This is a MIME encoded message.» . $eol; // message $body = «—» . $separator . $eol; $body .= «Content-Type: text/plain; charset=\»iso-8859-1\»» . $eol; $body .= «Content-Transfer-Encoding: 8bit» . $eol; $body .= $message . $eol; // attachment $body .= «—» . $separator . $eol; $body .= «Content-Type: application/octet-stream; name=\»» . $filenameee . «\»» . $eol; $body .= «Content-Transfer-Encoding: base64» . $eol; $body .= «Content-Disposition: attachment» . $eol; $body .= $content . $eol; $body .= «—» . $separator . «—«; //SEND Mail if (mail($mailto, $subject, $body, $headers)) < echo "mail send . OK"; // do what you want after sending the email >else

Important Notes :

  • This will not work in your desktop or Localhost
  • You have to host it in a server to make it work.
  • Some free hosting providers not allow you to send emails from your website (eg : 000webhost)
  • Sometimes you have to add a valid from email address to work with this
  • Maximum file size is 25 , if you are attaching bigger files, it may not work. anyway its depend on your server provider.

Check out the video for more details and to know how to implement html to email with attachments

Download Source Code :

If you are still having doubts about how to work with HTML to email with attachments forms you can check out my Youtube video explaining everything in detail.

Источник

Оцените статью