Solved

Creating an email in PHP

Posted on 2007-12-05
6
5,793 Views
Last Modified: 2013-12-12
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
0
Comment
Question by:d10u4v
[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
  • 3
  • 2
6 Comments
 
LVL 3

Expert Comment

by:yjwong
ID: 20411978
I've not tried, but since you are using HTML mail, you should use HTML line breaks after every line (<br />).
0
 
LVL 17

Expert Comment

by:nplib
ID: 20412310
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
 
LVL 3

Expert Comment

by:yjwong
ID: 20412365
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 17

Expert Comment

by:nplib
ID: 20412494
@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
 

Author Comment

by:d10u4v
ID: 20412654
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
 
LVL 17

Accepted Solution

by:
nplib earned 500 total points
ID: 20412672
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

710 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