Solved

Having problems with my sendmail file

Posted on 2008-06-23
27
225 Views
Last Modified: 2008-07-21
I have a sendmail file I have created, but it doesn't appear to be taking the user's username along with the email to allow verification.  Any thoughts as to why?  Below is a snippet on what is going on.  Not the total code.
$to = $_POST['strEmail'];

$subject = "Confirm registration with Company Name";

$message = '

<!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN>

<html>

<head>

<title>' . TITLE . '</title>

<meta http-equiv=Content-Type content=text/html; charset=iso-8859-1>

<link type=text/css href=http://www.domain.com/styles/email_template.css>

</head>

<body>

        <div class=email_position>

                <div class=email_background>

                        <div class=email_text>

                                <div style=margin-bottom: 10px align=center>

                                <img src=http://www.domain.com/images/logo.png>

                                </div>

                                Welcome to Company Name. 

                                <div style=margin-top: 20px align=center>

                                <a href=http://www.domain.com/confirm.php?Username=$username><img src=http://www.domain.com/images/validate.png width=205px height=56px border=0></a>

                                </div>

                                <div style=margin-top: 20px>

                                If the above image does not work, please click on the following link: <a href=http://www.domain.com/confirm.php?Username=' . $username . '>http://www.domain.com/confirm.php?Username=' . $username . '</a>

                                </div>

                        </div>

                </div>

        </div>

</body>

</html>

';

 

$headers .= 'From: An Email' . "\r\n";

$headers .= 'MIME-Version: 1.0' . "\r\n";

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

Open in new window

0
Comment
Question by:pingeyeg
  • 11
  • 8
  • 7
  • +1
27 Comments
 
LVL 6

Expert Comment

by:ixti
Comment Utility
Please provide form that posts data to this script.
Also  describe a little more clearly, what exactly you want?
And maybe it will be better to post whole code here. Just replace real domain names with example.com domain name.
0
 
LVL 82

Expert Comment

by:hielo
Comment Utility
Try adding the To field to the headers as well:

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

IF it still does not work, try changing the \r\n to simply \n.
0
 
LVL 9

Expert Comment

by:Rurne
Comment Utility
Line 32: 'From: An Email'

Is this a substitution for something else?  As you're not displaying any of the actual mail code, I can only assume this is the problem.
0
 
LVL 82

Expert Comment

by:hielo
Comment Utility
One more thing, you need to intialize your headers variable since you are using the .= operator:
$headers = "";
$headers .= 'From: An Email' . "\r\n";
0
 
LVL 1

Author Comment

by:pingeyeg
Comment Utility
Actually, the problem that I am having is dealing with the double and single quotes.  I am trying to figure out if using double quotes around the form elements is ok to do.  Also does that effect how the variable gets passed?  I have an external css file that tags along with the email being sent, but it won't work without having the double quotes around the elements, but in using the double quotes the email doesn't send.  Kind of confusing.
0
 
LVL 1

Author Comment

by:pingeyeg
Comment Utility
This is the way, to my knowledge, it needs to be, but it doesn't send the email.
if($mysql_insert) {

$to = $_POST['strEmail'];

$subject = "Confirm registration with Company";

$message = '

<!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN>

<html>

<head>

<title>' . TITLE . '</title>

<meta http-equiv=Content-Type content=text/html; charset=iso-8859-1>

<link type="text/css" href="http://www.domain.com/styles/email_template.css">

</head>

<body>

	<div class="email_position">

		<div class="email_background">

			<div class="email_text">

				<div style="margin-bottom: 10px" align="center">

				<img src="http://www.domain.com/images/logo.png">

				</div>

				Welcome to Company. 

				<div style="margin-top: 20px" align="center">

				<a href="http://www.domain.com/confirm.php?Username=$username"><img src="http://www.domain.com/images/validate.png" width="205px" height="56px" border="0"></a>

				</div>

				<div style="margin-top: 20px">

				If the above image does not work, please click on the following link: <a href="http://www.domain.com/confirm.php?Username=' . $username . '">http://www.domain.com/confirm.php?Username=' . $username . '</a>

				</div>

			</div>

		</div>

	</div>

</body>

</html>

';
 

$headers = 'From: Company <info@domain.com>' . "\r\n";

$headers .= 'MIME-Version: 1.0' . "\r\n";

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

if(mail($to, $subject, $message, $headers)) {

	header("location: /login.php?Valid=$valid&Username=$username");

} else {

	echo "There were problems with your registration, please contact Company at <a href='mailto:support@domain.com'>support@domain.com</a>";

}

}

Open in new window

0
 
LVL 82

Expert Comment

by:hielo
Comment Utility
You can use double quotes:
<?php

$to = $_REQUEST['strEmail'];

$subject = "Confirm registration with Company Name";

$message = '

<!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN>

<html>

<head>

<title>' . TITLE . '</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<link type="text/css" href="http://www.domain.com/styles/email_template.css">

</head>

<body>

        <div class="email_position">

                <div class="email_background">

                        <div class="email_text">

                                <div style="margin-bottom: 10px" align="center">

                                <img src="http://www.domain.com/images/logo.png">

                                </div>

                                Welcome to Company Name. 

                                <div style="margin-top: 20px" align="center">

                                <a href="http://www.domain.com/confirm.php?Username=$username"><img src="http://www.domain.com/images/validate.png" width="205px" height="56px" border="0"></a>

                                </div>

                                <div style="margin-top: 20px">

                                If the above image does not work, please click on the following link: <a href="http://www.domain.com/confirm.php?Username=' . $username . '">http://www.domain.com/confirm.php?Username=' . $username . '</a>

                                </div>

                        </div>

                </div>

        </div>

</body>

</html>

';

$header ='';

$headers .= 'From: An Email' . "\r\n";

$headers .= 'To: ' . $to . "\r\n";

$headers .= 'MIME-Version: 1.0' . "\r\n";

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

 

?>

Open in new window

0
 
LVL 1

Author Comment

by:pingeyeg
Comment Utility
You would think that would work, but I have yet to receive an email from it.  Either something is wrong with the code or the hosted server is being really slow on the delivery.
0
 
LVL 6

Expert Comment

by:ixti
Comment Utility
Please, provide form that sends data? What is in  $_POST['strEmail'] ? Can you at least post var_dump() of it?
0
 
LVL 82

Expert Comment

by:hielo
Comment Utility
That code is still missing the call to mail:
...
$header ='';
$headers .= 'From: you@yourcompany.com' . "\r\n";
$headers .= 'To: ' . $to . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

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

Author Comment

by:pingeyeg
Comment Utility
hielo:

This is the call to mail function:

if(mail($to, $subject, $message, $headers)) {
      header("location: /login.php?Valid=$valid&Username=$username");
} else {
      echo "There were problems with your registration, please contact us at <a href='mailto:support@domain.com'>support@domain.com</a>";
}
}
0
 
LVL 1

Author Comment

by:pingeyeg
Comment Utility
ixti,

No need for that.  I know the form is posting the information correctly to this page from doing error checking.  The two issues I am having right now is the fact that when the email does send the $username is not being passed.  Instead the variable stays as a string instead of a variable.  The other is actually getting the email sent.  I had it working before, but now it just completely quit.
0
 
LVL 1

Author Comment

by:pingeyeg
Comment Utility
ixti,

Here is the var_dump($_POST);

rray(8) { ["strValid"]=> string(5) "False" ["strUsername"]=> string(8) "pingeyeg" ["strEmail"]=> string(20) "the email" ["x"]=> string(2) "54" ["y"]=> string(1) "4" ["strPassword"]=> string(7) "the password" ["strSecret"]=> string(21) "Mother\'s maiden name" ["strAnswer"]=> string(5) "Faulk" }
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 6

Expert Comment

by:ixti
Comment Utility
Ah now I see what you want to do. Here's fix for variable. And what is the second problem?
<?php

$to = $_REQUEST['strEmail'];

$subject = "Confirm registration with Company Name";

$message = '

<!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN>

<html>

<head>

<title>' . TITLE . '</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<link type="text/css" href="http://www.domain.com/styles/email_template.css">

</head>

<body>

        <div class="email_position">

                <div class="email_background">

                        <div class="email_text">

                                <div style="margin-bottom: 10px" align="center">

                                <img src="http://www.domain.com/images/logo.png">

                                </div>

                                Welcome to Company Name. 

                                <div style="margin-top: 20px" align="center">

                                <a href="http://www.domain.com/confirm.php?Username=' . $username . '"><img src="http://www.domain.com/images/validate.png" width="205px" height="56px" border="0"></a>

                                </div>

                                <div style="margin-top: 20px">

                                If the above image does not work, please click on the following link: <a href="http://www.domain.com/confirm.php?Username=' . $username . '">http://www.domain.com/confirm.php?Username=' . $username . '</a>

                                </div>

                        </div>

                </div>

        </div>

</body>

</html>

';

$header ='';

$headers .= 'From: An Email' . "\r\n";

$headers .= 'To: ' . $to . "\r\n";

$headers .= 'MIME-Version: 1.0' . "\r\n";

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

 

?>

Open in new window

0
 
LVL 6

Expert Comment

by:ixti
Comment Utility
Sorry. Replace $to = $_REQUEST['strEmail']; with $to = $_POST['strEmail'];

And according to var_dump(), $_POST['strEmail'] is not valid email :)) so email cannot be sent :))
0
 
LVL 1

Author Comment

by:pingeyeg
Comment Utility
Actually, I replaced the actual email with that string so for security purposes.  May not matter much, but it just makes me feel better.
0
 
LVL 1

Author Comment

by:pingeyeg
Comment Utility
Actually, that doesn't seem to work because I already tried that.  When I remove the "' . $username . "' to just $username it sends the email, but obviously I need the variable to reflect the actual username when sent.
0
 
LVL 82

Expert Comment

by:hielo
Comment Utility
If TITLE is not defined anywhere, change this:
<title>' . TITLE . '</title>

to this:
<title>Testing Email</title>
0
 
LVL 6

Expert Comment

by:ixti
Comment Utility
Just try to replace your $message variable declaration with such:
$username_url_safe = str_replace(' ', '+', $username);

$message = '

<!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN>

<html>

<head>

<title>' . TITLE . '</title>

<meta http-equiv=Content-Type content=text/html; charset=iso-8859-1>

<link type=text/css href=http://www.domain.com/styles/email_template.css>

</head>

<body>

        <div class=email_position>

                <div class=email_background>

                        <div class=email_text>

                                <div style=margin-bottom: 10px align=center>

                                <img src=http://www.domain.com/images/logo.png>

                                </div>

                                Welcome to Company Name. 

                                <div style=margin-top: 20px align=center>

                                <a href="http://www.domain.com/confirm.php?Username=' . $username_url_safe . '"><img src=http://www.domain.com/images/validate.png width=205px height=56px border=0></a>

                                </div>

                                <div style=margin-top: 20px>

                                If the above image does not work, please click on the following link: <a href="http://www.domain.com/confirm.php?Username=' . $username_url_safe . '">http://www.domain.com/confirm.php?Username=' . $username_url_safe . '</a>

                                </div>

                        </div>

                </div>

        </div>

</body>

</html>

';

Open in new window

0
 
LVL 1

Author Comment

by:pingeyeg
Comment Utility
That's defined in an included constants file I have.
0
 
LVL 82

Expert Comment

by:hielo
Comment Utility
earlier on I suggested that you initialize $headers, but I typed $header (missed an "s"). Make sure you initialize it correctly. Also, if it does not work with \r\n, try it just with \n.
0
 
LVL 1

Author Comment

by:pingeyeg
Comment Utility
Still isn't working.
0
 
LVL 82

Expert Comment

by:hielo
Comment Utility
I tested the attached code and it worked for me. Notice that for YOU to receive the email, you will need to provide your email address in the from field. Other than that, the issue may be your smtp server. So if it was working earlier, try rebooting your server.
<?php

define('TITLE',"Testing");

$username="John Smith";

$from = 'me@mycompany.com';//provide a valid email address here

$to = $_REQUEST['strEmail'];//make sure your field has a valid email address

$subject = "Confirm registration with Company Name";

$message = '

<!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN>

<html>

<head>

<title>' . TITLE . '</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<link type="text/css" href="http://www.domain.com/styles/email_template.css">

</head>

<body>

        <div class="email_position">

                <div class="email_background">

                        <div class="email_text">

                                <div style="margin-bottom: 10px" align="center">

                                <img src="http://www.domain.com/images/logo.png">

                                </div>

                                Welcome to Company Name. 

                                <div style="margin-top: 20px" align="center">

                                <a href="http://www.domain.com/confirm.php?Username=' . $username . '"><img src="http://www.domain.com/images/validate.png" width="205px" height="56px" border="0"></a>

                                </div>

                                <div style="margin-top: 20px">

                                If the above image does not work, please click on the following link: <a href="http://www.domain.com/confirm.php?Username=' . $username . '">http://www.domain.com/confirm.php?Username=' . $username . '</a>

                                </div>

                        </div>

                </div>

        </div>

</body>

</html>

';

$headers ='';

$headers .= 'From:' . $from . "\r\n";

$headers .= 'To: ' . $to . "\r\n";

$headers .= 'MIME-Version: 1.0' . "\r\n";

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

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

?>

Open in new window

0
 
LVL 1

Author Comment

by:pingeyeg
Comment Utility
I have found that what causes the problem is the $username variables at the bottom of the page.  Having the ' . $username . '" and the ' . $username . ' is causing the email not to be sent.  When I take off the single quotes the email sends out, but obviously doesn't send the variable correctly.
0
 
LVL 82

Expert Comment

by:hielo
Comment Utility
Try urlencode for the href of the links and htmlentities for the "viewable" link:
$message = '

<!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN>

<html>

<head>

<title>' . TITLE . '</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<link type="text/css" href="http://www.domain.com/styles/email_template.css">

</head>

<body>

        <div class="email_position">

                <div class="email_background">

                        <div class="email_text">

                                <div style="margin-bottom: 10px" align="center">

                                <img src="http://www.domain.com/images/logo.png">

                                </div>

                                Welcome to Company Name. 

                                <div style="margin-top: 20px" align="center">

                                <a href="http://www.domain.com/confirm.php?Username=' . urlencode($username) . '"><img src="http://www.domain.com/images/validate.png" width="205px" height="56px" border="0"></a>

                                </div>

                                <div style="margin-top: 20px">

                                If the above image does not work, please click on the following link: <a href="http://www.domain.com/confirm.php?Username=' . urlencode($username) . '">http://www.domain.com/confirm.php?Username=' . htmlentities($username) . '</a>

                                </div>

                        </div>

                </div>

        </div>

</body>

</html>

';

Open in new window

0
 
LVL 6

Accepted Solution

by:
ixti earned 500 total points
Comment Utility
Just copy-paste $massage declaration WITHOUT modifications. Or copy-paste $message declaration suggested by hielo in 21850161 post.
$message = '

<!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN>

<html>

<head>

<title>' . TITLE . '</title>

<meta http-equiv=Content-Type content=text/html; charset=iso-8859-1>

<link type=text/css href=http://www.domain.com/styles/email_template.css>

</head>

<body>

        <div class=email_position>

                <div class=email_background>

                        <div class=email_text>

                                <div style=margin-bottom: 10px align=center>

                                <img src=http://www.domain.com/images/logo.png>

                                </div>

                                Welcome to Company Name. 

                                <div style=margin-top: 20px align=center>

                                <a href="http://www.domain.com/confirm.php?Username=' . $username_url_safe . '"><img src=http://www.domain.com/images/validate.png width=205px height=56px border=0></a>

                                </div>

                                <div style=margin-top: 20px>

                                If the above image does not work, please click on the following link: <a href="http://www.domain.com/confirm.php?Username=' . $username_url_safe . '">http://www.domain.com/confirm.php?Username=' . $username_url_safe . '</a>

                                </div>

                        </div>

                </div>

        </div>

</body>

</html>

';

Open in new window

0
 
LVL 6

Expert Comment

by:ixti
Comment Utility
And as a last-resort. Just test to send a mail with simple file like:
<?php
 

$var     = 123;

$message = 'testing variable $var = ' . $var . ' inside bodies';

$from    = "from@example.com"; // Anny e-mail address. Be sure that SMTP accept it.

$to      = "user@example.com"; // Real address to send email to
 

$subject = "Confirm registration with Company Name";
 

$headers = "From: {$from}\n"

         . "To: {$to}\n"

         . "MIME-Version: 1.0\n"

         . "Content-type: text/html; charset=iso-8859-1\n";
 

var_dump(mail($to, $subject, $message, $headers));
 

// If output will be bool(true) - it mean that message was sent correctly

// Else bool(false) - something went wrong. 

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now