Jump to content

Here is my HTML form.

 

<form class="emailForm" name="Email Form" method="post" action="contact.php" enctype="text/plain">
<input name='first' type='first' required class="formDec" id="first" placeholder="First*">
<input name='last' type='last' required class="formDec" id="last" placeholder="Last*">
<input name='email' type='email' required class="formDec" id="email" placeholder="Email*">
<input name='subject' type='subject' required class="formDec" id="subject" placeholder="Event Type (Wedding, party, etc.)">
<input name='message' type='message' required class="formDec messageForm" id="message" placeholder="Message">
<input type="submit" class="submitForm" id="submit" value="Submit">
</form>

Works great on my website.

 

Here is the php code the form takes you to once you click submit:

 

<title>Thanks for contacting UM!</title>
<?php

require_once 'vendor/autoload.php';

	$first = filter_var($_POST['first'], FILTER_SANITIZE_STRING);
	$last = filter_var($_POST['last'], FILTER_SANITIZE_STRING);
	$email = filter_var($_POST['email'], FILTER_SANITIZE_STRING);
	$subject = filter_var($_POST['subject'], FILTER_SANITIZE_STRING);
	$msg = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
	
	$data = "First: " . $first . "Last: " . $last . "<br />" . "Email: " . $email . "<br />" . "Type of Event: " . $subject . "<br />" . "Message: " . $message;
	
	$transport=\Swift_SmtpTransport::newInstance('smtp.gmail.com',465,'ssl')
		->setUsername('uasdf@gmail.com')
		->setPassword('asdfal');
		
	$mailer=\Swift_Mailer::newInstance($transport);
	$message=\Swift_Message::newInstance('Ultra Media Contact Form')
		->setFrom (array('asdfms@gmail.com' => 'Ultra Media Forms'))
		->setTo (array('asdf@gmail.com', 'aasdf@gmail.com' => 'Form Recipients'))
		->setSubject ('Contact Form from Ultra Media received!')
		->setBody ($data, 'text/html');
		
	$mailer->send($message);
		
?>
Thank you for contacting Ultra Media, we will get back to you as quickly as possible.
Please click <a href='index.html'>here</a> to return to our homepage.

Also works great, but, small problem.

 

I receive the email. Great. BUT. Here is what the email looks like after filling out the form legitimately:

 

First: Last: 
Email: 
Type of Event: 
Message:

 

As you can see, I receive the email without any information that the user sent. Whyyy??

Link to comment
https://linustechtips.com/topic/593676-php-email-form-email-not-correct/
Share on other sites

Link to post
Share on other sites

I may be wrong but it looks like you've declared first, last, email, subject, msg and those aren't being declared again under ->setBody you'll have to alter this code bit slightly as it seems you may be using a different mail handler solution for PHP besides PHPMailer like I am using but this is the gist of it.

if(isset($_POST['name'], $_POST['email'], $_POST['message'])) {
    
    $fields = [ /* Pulling form input from the HTML form created somewhere else, you can replace name, email, phone, message etc with whatever your HTML form variables are set to */
        'name' => $_POST['name'],
        'email' => $_POST['email'],
        'phone' => $_POST['phone'],
        'message' => $_POST['message']
        ];
			
/* Using PHPMailer here so you'll probably need to change this up a bit for whatever you're using not sure why the line under this is indented so much so indent it similarly to the other $m-> */
			$m = new PHPMailer(true);
            
            $m->isSMTP();
            $m->SMTPAuth = true;
            
            $m->Host = 'smtp.gmail.com';
            $m->Username = 'asdf@gmail.com';
            $m->Password = 'asdf1';
            $m->SMTPSecure = 'ssl';
            $m->Port = 465;
            
            $m->isHTML(true);
            
            $m->Subject = 'Contact form submitted';
			/* This is where you're having trouble I believe here I 'call' the 'name' from my $fields array and do that repeatedly for each field type and then I just set the paragraph element for the message as this is an HTML email. */
            $m->Body = 'From: ' . $fields['name'] . ' (' . $fields['email'] . ') (' . $fields['phone'] . ') <p>' . $fields['message'] . '</p>';
            
            $m->From = 'asdf@gmail.com';
            $m->FromName = 'asdf Mail Handler';

            $m->AddAddress('Jdoe@gmail.com', 'John Doe');
			/* If you're using PHPMailer then the above address is where you are sending the emails to, as far as I know you can have multiple different addresses */

			/* Also this probably won't work on free cloud code editors since they block the ports needed to send email so you'll need to test this locally or on some dedicated/VPS hosting that allows you access to port 465 etc. */

Helpful documentation for a bunch of cool frameworks/languages: http://devdocs.io/

Gaming Rig - Excalibur - CPU: i5 6600k @ 4.1GHz, CPU Cooler: Hyper 212 Evo, Mobo: MSI Gaming M3 RAM: 16GB Corsair @2400MHz, GPU: EVGA 1060, Case: NZXT Phantom Full Tower (Red)

My Virtualization Server - Dell R710: 2x X5570s @ 2.93GHz with 32GB DDR3 RAM [Web Server, OSX, Plex, Reverse Proxy]

I love computers, gaming, coding, and photography! Be sure to quote me so I can respond to your post!

Link to post
Share on other sites

10 hours ago, 1823alex said:

I may be wrong but it looks like you've declared first, last, email, subject, msg and those aren't being declared again under ->setBody you'll have to alter this code bit slightly as it seems you may be using a different mail handler solution for PHP besides PHPMailer like I am using but this is the gist of it.


if(isset($_POST['name'], $_POST['email'], $_POST['message'])) {
    
    $fields = [ /* Pulling form input from the HTML form created somewhere else, you can replace name, email, phone, message etc with whatever your HTML form variables are set to */
        'name' => $_POST['name'],
        'email' => $_POST['email'],
        'phone' => $_POST['phone'],
        'message' => $_POST['message']
        ];
			
/* Using PHPMailer here so you'll probably need to change this up a bit for whatever you're using not sure why the line under this is indented so much so indent it similarly to the other $m-> */
			$m = new PHPMailer(true);
            
            $m->isSMTP();
            $m->SMTPAuth = true;
            
            $m->Host = 'smtp.gmail.com';
            $m->Username = 'asdf@gmail.com';
            $m->Password = 'asdf1';
            $m->SMTPSecure = 'ssl';
            $m->Port = 465;
            
            $m->isHTML(true);
            
            $m->Subject = 'Contact form submitted';
			/* This is where you're having trouble I believe here I 'call' the 'name' from my $fields array and do that repeatedly for each field type and then I just set the paragraph element for the message as this is an HTML email. */
            $m->Body = 'From: ' . $fields['name'] . ' (' . $fields['email'] . ') (' . $fields['phone'] . ') <p>' . $fields['message'] . '</p>';
            
            $m->From = 'asdf@gmail.com';
            $m->FromName = 'asdf Mail Handler';

            $m->AddAddress('Jdoe@gmail.com', 'John Doe');
			/* If you're using PHPMailer then the above address is where you are sending the emails to, as far as I know you can have multiple different addresses */

			/* Also this probably won't work on free cloud code editors since they block the ports needed to send email so you'll need to test this locally or on some dedicated/VPS hosting that allows you access to port 465 etc. */

Helpful documentation for a bunch of cool frameworks/languages: http://devdocs.io/

Thanks for this. But I figured out that it was just an incompatibility issue. I used enctype text/plain in my html form, and it wasn't compatible with method=post.

Link to post
Share on other sites

7 hours ago, jttiglao said:

Thanks for this. But I figured out that it was just an incompatibility issue. I used enctype text/plain in my html form, and it wasn't compatible with method=post.

Ahh, is it working now? If not I'd recommend using something like PHPMail.

Gaming Rig - Excalibur - CPU: i5 6600k @ 4.1GHz, CPU Cooler: Hyper 212 Evo, Mobo: MSI Gaming M3 RAM: 16GB Corsair @2400MHz, GPU: EVGA 1060, Case: NZXT Phantom Full Tower (Red)

My Virtualization Server - Dell R710: 2x X5570s @ 2.93GHz with 32GB DDR3 RAM [Web Server, OSX, Plex, Reverse Proxy]

I love computers, gaming, coding, and photography! Be sure to quote me so I can respond to your post!

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×