HTML email via PHP appears to be working.....but it's not???

jcarlin14

New Member
Messages
3
Reaction score
0
Points
0
Hi,

I am attempting to send a HTML email via PHP using coding that i know works. It submits the contents of a form to a DB before emailing the results. Here is the coding...

<?php
$var1 = $_POST['var1'];
$var2 = $_POST['var2'];
$var3 =$_POST['var3'];
$var4 = $_POST['var4'];
$var5 = $_POST['var5'];
$var6 = $_POST['var6'];
$var7 = $_POST['var7'];
$var8 = $_POST['var8'];
$to = "email@email";
$from = "test";
$subject = "you have recieved an email from" .$_POST['var3'];
$headers = "MIME-Version: 1.0rn";
$headers .= "Content-type: text/html; charset=iso-8859-1rn";
$headers .= "From: $from\r\n";

$message="
<html>
<body>
<center>
<b>Here is the email you have sent</b> <br>
</center>
<p>it's been sent</p>
</body>
</html>";

mysql_connect("localhost","username","password") or die ('Error: ' . mysql_error());
mysql_select_db ('dbname');

$query="INSERT INTO resultslog(var1, var2, var3, var4, var5, var6, var7, var8) VALUES ('".$var1."','".$var2"','".$var3."','".$var4."','".$var5."','".$var6."','".$var7."','".$var8."')";
mysql_query($query) or die (mysql_error());
if (mail($to,$subject,$message,$headers) ) {
ECHO "Your message has been sent";
}else{
ECHO "The email has failed";
}
?>

When I submit the form I am presented with the "Your message has been sent" text and the data is submitted to the DB but the email is not being sent? Does anyone have any idea why this might not be happening?

Thanks,

JC
 

ice.adi49

New Member
Messages
10
Reaction score
0
Points
1
probably is because of the headers (you
try:
Code:
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "From: $from \n";
if this fails too, then it could be that you are not allowed to send mails on that server.
Happen to me on a godaddy hosting, when in order to send mail, i had to access a smtp which required authentication.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Please use
PHP:
, [html] or [code] tags (as appropriate) to separate and format code.

[quote="jcarlin14, post: 789641"][php]<?php
$var1 = $_POST['var1'];
$var2 = $_POST['var2'];
[...]
[/QUOTE]
I hope you've renamed these variables out of concern for security. Otherwise, you should always use descriptive variable names so your code is readable (and security through obscurity doesn't work).

PHP:
$query="INSERT INTO resultslog(var1, var2, var3, var4, var5, var6, var7, var8) VALUES ('".$var1."','".$var2"','".$var3."','".$var4."','".$var5."','".$var6."','".$var7."','".$var8."')";
This is vulnerable to SQL injection, which is a very serious security risk. To fix this hole, switch from the outdated mysql driver to PDO and use prepared statements. If you need a PDO tutorial, try "Writing MySQL Scripts with PHP and PDO". The site you save may just be your own.

PHP:
mysql_query($query) or die (mysql_error());

Don't use die when outputting HTML.

Outputting the result of mysql_error to non-admin users discloses too much information.

When I submit the form I am presented with the "Your message has been sent" text and the data is submitted to the DB but the email is not being sent[.]
mail returns true if the message is accepted delivery, not for if it's delivered.

How do you know it's not being delivered? Have you checked your spam folder?
 

jcarlin14

New Member
Messages
3
Reaction score
0
Points
0
Hi,

Thanks for the responses. I have checked Spam folder and also tried sending to different email addresses. I have changed the variable names, just want to get it working before I set everything correctly.

I have also sent non-HTML emails and they are working fine, which is what is confusing me?

Thanks,

JC
 

ricky315

New Member
Messages
1
Reaction score
0
Points
0
I'm sure that there are HTML error that's why you can't send your email. I suggest you use HTML email checkers to find out the problem
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Doubtful. With the Internet Message Format, it doesn't much matter whether message content is properly structured for its content type as long as it's properly escaped so as not to interfere with the MIME format. The sample doesn't even make use of multipart messages, so there's nothing to worry about on that front. Message content has even less of an impact in SMTP. If the HTML were invalid. the e-mail should still be delivered.
 
Top