lingo5 Posted June 3, 2011 Share Posted June 3, 2011 Hi again, I have been reading on sending email with PHP and have finally done my first php mail function. I use this to retrieve a user's pasaword and have it emailed automatically: <?php $password = $row_user_data_RS['cliente_password']; ?> <?php $email = $row_user_data_RS['cliente_email']; ?> <?php mail($email, 'Your password is', $password, 'From: '.$email.''); ?> The resulting email contains just the user password. I would lke to know how I can add HTML code to the body of the email so that I can deliver a nicely formated email. Thanks ll. Quote Link to comment Share on other sites More sharing options...
revraz Posted June 3, 2011 Share Posted June 3, 2011 http://php.net/manual/en/function.mail.php Quote Link to comment Share on other sites More sharing options...
lingo5 Posted June 3, 2011 Author Share Posted June 3, 2011 Many thanks revraz!!, reading now Quote Link to comment Share on other sites More sharing options...
lingo5 Posted June 3, 2011 Author Share Posted June 3, 2011 OK, I heve done this now: <?php // The message $message = "Dear client,\n your access details are:\n Usr Name:'$email'\n Password:'$password'"; $email = $row_user_data_RS['cliente_email']; $password = $row_user_data_RS['cliente_password']; // In case any of our lines are larger than 70 characters, we should use wordwrap() $message = wordwrap($message, 70); // Send mail($email,'Your access details', $message); ?> </body> </html> <?php mysql_free_result($user_data_RS); ?> An email is sent ok, but the $email and $password vars are blank. I know I'm doing something wrong here.....but what is it? Quote Link to comment Share on other sites More sharing options...
WebStyles Posted June 3, 2011 Share Posted June 3, 2011 try this: $message = "Dear client,\n your access details are:\n Usr Name:".$email."\n Password:".$password; Quote Link to comment Share on other sites More sharing options...
revraz Posted June 3, 2011 Share Posted June 3, 2011 You are putting your $message variable before you set your $email and $password variables $message = "Dear client,\n your access details are:\n Usr Name:$email \n Password:$password"; <---Move this after the two below $email = $row_user_data_RS['cliente_email']; $password = $row_user_data_RS['cliente_password']; Quote Link to comment Share on other sites More sharing options...
jcbones Posted June 3, 2011 Share Posted June 3, 2011 try this: $message = "Dear client,\n your access details are:\n Usr Name:".$email."\n Password:".$password; Variables are parsed in double quoted strings, no need to do that. Swap your lines around: // The message $email = $row_user_data_RS['cliente_email']; $password = $row_user_data_RS['cliente_password']; $message = "Dear client,\n your access details are:\n Usr Name:'$email'\n Password:'$password'"; Variables are looked at in an ordered fashion. So you must declare and then use, you cannot use then declare. Quote Link to comment Share on other sites More sharing options...
WebStyles Posted June 3, 2011 Share Posted June 3, 2011 lol. don't know how I missed that one. Quote Link to comment Share on other sites More sharing options...
lingo5 Posted June 3, 2011 Author Share Posted June 3, 2011 Thanks all, that worked. I have one more question...how would I send an email to ALL the clients ? Quote Link to comment Share on other sites More sharing options...
WebStyles Posted June 3, 2011 Share Posted June 3, 2011 I'm not sure if you can pass an array with emails to the mail function, but you can always loop it. Quote Link to comment Share on other sites More sharing options...
WebStyles Posted June 3, 2011 Share Posted June 3, 2011 just read the manual, you can add emails by separating them with commas. But you'll probably be giving away all the addresses to each user. test and see. Or just create a loop. http://pt.php.net/manual/en/function.mail.php Quote Link to comment Share on other sites More sharing options...
lingo5 Posted June 3, 2011 Author Share Posted June 3, 2011 Thanks, I mean without having to add email addresses manually. One more question please, how can I redirect on success or failure? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted June 3, 2011 Share Posted June 3, 2011 That would send the same email to all the recipients. I don't think everyone wants to see the same username/password. Ken Quote Link to comment Share on other sites More sharing options...
lingo5 Posted June 3, 2011 Author Share Posted June 3, 2011 Sorry Ken, I didn'texplain myself properly. I mean sending an email to all user on my DB with other information, not the acces details. ie when a new document is uploaded. Quote Link to comment Share on other sites More sharing options...
lingo5 Posted June 3, 2011 Author Share Posted June 3, 2011 OK, here's how I'm trying to redirect after succes or failure if (mail($email, $subject, $message)) { header('Location: gracias2.php'); } else { header('Location: error.php'); } But I get a "headers already sent" error.... Quote Link to comment Share on other sites More sharing options...
lingo5 Posted June 3, 2011 Author Share Posted June 3, 2011 OK, I fixed it by deleting the <head> tag. Thanks all for your help. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted June 3, 2011 Share Posted June 3, 2011 If you have sent any output to the browser before doing the header() function, you will get that error. Ken Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.