Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Having problems with my sendmail file

Posted on 2008-06-23
27
Medium Priority
?
233 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 8
  • 7
  • +1
27 Comments
 
LVL 6

Expert Comment

by:ixti
ID: 21848833
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
ID: 21848841
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
ID: 21848843
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 82

Expert Comment

by:hielo
ID: 21848890
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
ID: 21848904
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
ID: 21848975
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
ID: 21848990
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
ID: 21849096
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
ID: 21849151
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
ID: 21849172
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
ID: 21849187
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
ID: 21849232
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
ID: 21849291
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
 
LVL 6

Expert Comment

by:ixti
ID: 21849300
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
ID: 21849325
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
ID: 21849341
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
ID: 21849359
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
ID: 21849405
If TITLE is not defined anywhere, change this:
<title>' . TITLE . '</title>

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

Expert Comment

by:ixti
ID: 21849414
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
ID: 21849421
That's defined in an included constants file I have.
0
 
LVL 82

Expert Comment

by:hielo
ID: 21849470
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
ID: 21849846
Still isn't working.
0
 
LVL 82

Expert Comment

by:hielo
ID: 21849986
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
ID: 21850097
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
ID: 21850161
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 2000 total points
ID: 21947775
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
ID: 21989352
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

Find the IP of a Linux Server in Command Line

Knowing your server’s IP address(s) can be a useful bit of information to have for various reasons. This tutorial will teach you how to check the IPs of any modern Linux server. To follow along will simply need access to the server via SSH or TTY.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

730 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