Solved

Creating an email in PHP

Posted on 2007-12-05
6
5,749 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

705 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

15 Experts available now in Live!

Get 1:1 Help Now