Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Creating an email in PHP

Posted on 2007-12-05
6
Medium Priority
?
5,850 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 1500 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

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

578 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