What's new

Need assistance with PHP

Webfunk

Top Contributor
Hi,

I'm trying to include an image in the $autoreply of my php. When people fill out my html form and click submit, it triggers the php script, and they are sent an email back automatically. That itself works fine, but I want a signature (the image) down the bottom of the email.

Is there a way of doing this? This is my php at the moment...

Code:
<?php 
 $to = "me@email.com.au" ; 
 $from = $_REQUEST['email'] ; 
 $name = $_REQUEST['firstName'] ; 
 $headers = "From: $from"; 
 $subject = "Contact Form"; 
 
 $fields = array(); 
 $fields{"firstName"} = "First Name"; 
 $fields{"lastName"} = "Last Name"; 
 $fields{"email"} = "Email"; 
 $fields{"phone"} = "Phone"; 
 $fields{"subject"} = "Subject"; 
 $fields{"question"} = "Question"; 
 
 $body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ 	$body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } 
 
 $headers2 = "From: Me <me@email.com.au>"; 
 $subject2 = "Thank you for contacting me"; 
 $autoreply = "Thank you for the enquiry.";
 
 if($from == '') {print "You have not entered an email, please go back and try again";} 
 else { 
 if($name == '') {print "You have not entered a name, please go back and try again";} 
 else { 
 $send = mail($to, $subject, $body, $headers); 
 $send2 = mail($from, $subject2, $autoreply, $headers2); 
 if($send) 
 {header( "Location: http://www.website.com.au/index.html" );} 
 else 
 {print "I encountered an error sending your mail, please notify me at me@email.com.au"; } 
 }
}
 ?>
 

Webfunk

Top Contributor
Thanks for the link, but I do not understand about 99% of the stuff on that page.

I put some HTML into a $message, but that did not work. Then I added in..

$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

because it said to, but looks like it requires some other MIME thing installed. I followed to link to install that, and that lead me to something called PEAR, then I had to install php, long story short - that's a lot of bs to go through for this and doesn't seem right.
 

FirstPageResults

Top Contributor
You don't need to install anything for MIME, all your doing is outlining the MIME type so that the server can send in an appropriate format. You also don't need to touch PEAR. But yes obviously PHP must be installed on the webserver for the script to work.

To insert HTML into an email, all you have to do is the following:

Code:
<?php
$to = "somebody@example.com, somebodyelse@example.com";
$subject = "HTML email";

$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>This email contains HTML Tags!</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
</tr>
</table>
</body>
</html>
";

// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";

// More headers
$headers .= 'From: <webmaster@example.com>' . "\r\n";
$headers .= 'Cc: myboss@example.com' . "\r\n";

mail($to,$subject,$message,$headers);
?>

source: http://www.w3schools.com/PHP/func_mail_mail.asp

I was just pointing out that not every mail client supports HTML so best practice is to also send a plain text alternative.
 

Webfunk

Top Contributor
So I'm guessing that gmail has an issue with html? I used the code you provided and it literally shows the code.
 

FirstPageResults

Top Contributor
So I'm guessing that gmail has an issue with html? I used the code you provided and it literally shows the code.



All I did was remove the CC and change the to and from addresses to my own accounts
 

Webfunk

Top Contributor
Code:
<?php 
 $to = "me@email.com.au" ; 
 $from = $_REQUEST['email'] ; 
 $name = $_REQUEST['firstName'] ; 
 $headers = "From: $from"; 
 $subject = "Contact Form - Pure Rentals"; 
 
 $fields = array(); 
 $fields{"firstName"} = "First Name"; 
 $fields{"lastName"} = "Last Name"; 
 $fields{"email"} = "Email"; 
 $fields{"phone"} = "Phone"; 
 $fields{"subject"} = "Subject"; 
 $fields{"question"} = "Question"; 
 
 $body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ 	$body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } 
 
 $headers2 = "From: Pure Rentals <me@email.com.au>"; 
 $subject2 = "Thank you for the enquiry"; 
 $message = "
 <html>
 <head>
 <title>HTML email</title>
 </head>
 <body>
 <p>This email contains HTML</p>
 </body>
 </html>
 ";
 
 $headers = "MIME-Version: 1.0" . "\r\n";
 $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
 
 $send = mail($to, $subject, $body, $headers); 
 $send2 = mail($from, $subject2, $message, $headers2); 
 if($send)
 {header( "Location: http://www.website.com.au/index.html" );} 
 ?>

So is there an issue with my code? Am I missing something?
 

Webfunk

Top Contributor
Check what code? What do you mean by MIME is missing? I'm so confused...

I thought it was just this...

Code:
 $headers = "MIME-Version: 1.0" . "\r\n";
 $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
 

FirstPageResults

Top Contributor
This line sends the HTML email:

Code:
 $send2 = mail($from, $subject2, $message, $headers2);

But the variable $headers2 doesn't contain the MIME type.. only $headers used in the first email does.

Look at the example I posted again:

Code:
// Always set content-type when sending HTML email
[B]$headers[/B] = "MIME-Version: 1.0" . "\r\n";
[B]$headers[/B] .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";

// More headers
[B]$headers[/B] .= 'From: <webmaster@example.com>' . "\r\n";

You need all 3
 

Lemon

Top Contributor
Hi,

You need to run input validation on your variables because as it stands your script is open to abuse.

Lemon
 

neddy

Top Contributor
Just putting a little lightness into this thread - I have absolutely no idea what anyone is talking about! ;)
 

Webfunk

Top Contributor
Hi Lemon,

I have input validation on my html form before the script even gets processed...is that what you mean?
 

FirstPageResults

Top Contributor
Hi Lemon,

I have input validation on my html form before the script even gets processed...is that what you mean?

Are you doing that with Javascript?

That won't be enough to stop a bot from hammering your form. You'll need to do it again with PHP when the form data is processed.
 

Webfunk

Top Contributor
Got it working!

The code ended up being..

$headers2 = 'From: Me<me@email.com.au>' . "\r\n";
$headers2 .= "MIME-Version: 1.0" . "\r\n";
$headers2 .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";

Thanks heaps for the help mate....I really appreciate it :)
 

sp@rky13

Top Contributor
If they have javascript disabled then they will get through. Javascript is good as it works without reloading the page, but will not stop bipassing
 

Community sponsors

Domain Parking Manager

AddMe Reputation Management

Digital Marketing Experts

Catch Expired Domains

Web Hosting

Members online

Forum statistics

Threads
11,100
Messages
92,051
Members
2,394
Latest member
Spacemo
Top