Solved

Creating an email in PHP

Posted on 2007-12-05
6
5,768 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
  • 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

810 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