Creating an email in PHP

Hi,

I'm having trouble formatiing an email using the PHP mailer.  My code is:

//Connect to database

mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());
 
//mysql_select_db('users1') or die ('Cannot select database');
   
// QUERY
$query = 'SELECT password FROM users1 WHERE email = \''.mysql_real_escape_string($_POST['email']).'\' LIMIT 1';
$result = mysql_query($query) or die ('Query failed: ' . mysql_error());
 
 
$line = mysql_fetch_assoc($result);
if (!$line) {
    $error = "Email Not Found in Database";
    require_once("forgetpass.php");
}else{  
 
    include_once('class.phpmailer.php');
   
    $mail    = new PHPMailer();
      
   
    $body = "Your password is ".$line['password'];
      $body  = "Your Login Details are:" "\n\n";
    $body = "Username: ['email'] \n\n";
    $body = "Password: ", .$line['password']" \n\n";
    $body = "If you have any queries or comments please contact you Records Centre Representative" \n\n";
   
    $mail->From     = "online@mydomain.co.uk";
    $mail->FromName = "ImageOnDemand Account";
   
    $mail->Subject = "Your Password Reminder";
   
    $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
   
    $mail->MsgHTML($body);
   
    $mail->AddAddress($_POST['email'], $_POST['email']);
   
    if(!$mail->Send()) {
      $error = "An error occured and no emial was sent";
    require_once("forgetpass.php");
    } else {
      $error = "Email Sent!";
    require_once("forgetpass.php");
    }  
 
}
 
mysql_free_result($result);
mysql_close($link);
 
?>

What i'm trying to do ic create an email both Test and HTML saying:

Thank you for requesting a username and password reminder.

The login details used to access your ImageOnDemand account are::

Username: [email]

Password: [password]

If you have any queries or comments please contact your Records Centre Representative

Kind regards

Online Team
Digital Solutions


___________________


I hope someone can help me...

Regards,
Anthony
d10u4vAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
nplibConnect With a Mentor Commented:
you have to have .= to add more lines

$body .= "Your password is ".$line['password']."<br />";
$body .= "Your Login Details are:<br />";
$body .= "Username: ."$line['email']."<br />";
$body .= "Password: ".$line['password']."<br />";
$body .= "If you have any queries or comments please contact you Records Centre Representative<br />";
0
 
yjwongCommented:
I've not tried, but since you are using HTML mail, you should use HTML line breaks after every line (<br />).
0
 
nplibCommented:
for starters if you do this
 $body = "Your password is ".$line['password'];
      $body  = "Your Login Details are:" "\n\n";
    $body = "Username: ['email'] \n\n";
    $body = "Password: ", .$line['password']" \n\n";
    $body = "If you have any queries or comments please contact you Records Centre Representative" \n\n";
$body will only equal the last line, there is no on concatenation there. plus using \n will not create a new line in the body of the email, or any webpage, that is strictly for creating new lines at the text level, browsers and html engines ignore these characters.
Plus these are lots of errors in your syntax.
You would have to change it to this.
 
$body = "Your password is ".$line['password']."<br />";
      $body  .= "Your Login Details are:<br />";
    $body .= "Username: ."$line['email']."<br />";
    $body .= "Password: ".$line['password']."<br />";
    $body .= "If you have any queries or comments please contact you Records Centre Representative<br />";
 
$mail->MsgHTML($body);   <--- having this line invokes the AltBody() method to use the test only of the msgHTML body, you would have to but the altbody after this line, though I don't think it would work, but you can try it, if it doesn't you will have to do it another way.

$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

Also, you haven't told it how and where to send it,

are you using smtp to send? are you using sendmail? are you using mail()? it has no idea what to do.

I like to use smtp
if you use it, then you need to have $mail->IsSMTP();
$mail->Host = "ip_address of smtp server";
does your smtp server require authentication.

lots of questions.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
yjwongCommented:
I guess the TS's problem is about just formatting the mail. PHPMailer will default a usable/configured mail server to send the mails, so I guess it isn't a problem unless you are using Windows (which requires SMTP). Many Linux server distributions have at least a simple Sendmail program bundled. So basically the solution to make it display right is just adding the "<br />" before the "\n".
0
 
nplibCommented:
@yiwong, even if the linux distro has mail() or sendmail() already complied, you have to tell phpmailer which method to use for emailing. It doesn't just guess.

having the \n in anything that is going to be rendered by an html engine is completely pointless, you would put the \n where you want line breaks in the plain text portion, the altBody variable.
0
 
d10u4vAuthor Commented:
I origionally had this code, which worked fine.  However the emial just had the one line [You password is ---]  I was just trying to format an email with more on it.

 include_once('class.phpmailer.php');
    
    $mail    = new PHPMailer();
    
    $body = "Your password is ".$line['password'];
    
    $mail->From     = "online@mydomain.co.uk";
    $mail->FromName = "First Last";
    
    $mail->Subject = "Your Password Remainder";
    
    $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
    
    $mail->MsgHTML($body);
    
    $mail->AddAddress($_POST['email'], $_POST['email']);
    
  if(!$mail->Send()) {
      $error = "An error occured and no emial was sent";
    require_once("forgetpass.php");
    } else {
      $error = "Email Sent!";
    require_once("forgetpass.php");
    }
 
 
mysql_free_result($result);
mysql_close($link);
 
?>

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.