#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    7
    Rep Power
    0

    Formatting text emails


    Hello Maximum or anyone else that can help me.

    I have a form with a text field named "text" and I wrote an application using php. The application emails all registered users the text that the user entered into the "text" field. What I want to do is format the email text so that it has a background color of #dbe5c4, width of 500 pixels. Here is the form:

    <form method="post" action="sendemail.php">
    Subject of email <br />
    <input type="text" id="subject" name="subject" size="60" />
    <br />
    <br />
    Body of email:<br />
    <textarea id="text" name="text" rows="8" cols="60"></textarea>
    <br />
    <br />
    <input type="submit" value="submit" name="submit" />
    </form>

    Here is the php script that sends the email:

    if ((!empty($subject))&&(!empty($text))){

    $dbc = mysqli_connect('databaseurl', 'username', 'password', 'database')
    or die('Error connecting to MySQL server.');

    $query = "SELECT * FROM email_list";
    $result = mysqli_query($dbc, $query)
    or die('Error querying database.');

    // $to, $first_name, $last_name are columns in the database

    while ($row = mysqli_fetch_array($result)){
    $to = $row['email'];
    $first_name = $row['first_name'];
    $last_name = $row['last_name'];
    //$msg is the the variable that holds the message that the user puts in the text
    // field. I want to format the message so that it has a background color and
    // width size of 500 pixels. I'm a newbie at PHP so please explain in layman
    // terms. Write out the code clearly for me please.

    $msg = "Dear $first_name $last_name";
    $msg .= "\n\n";
    $msg .= "$text";

    mail($to, $subject, $msg, 'From: ' . $from);
    echo 'Email sent to:' . $to . '<br />';
    }
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,923
    Rep Power
    1045
    Hi,

    you need to wrap your code in [PHP] tags.



    Originally Posted by arkingIT
    Hello Maximum or anyone else that can help me.
    Who is Maximum?



    Originally Posted by arkingIT
    I have a form with a text field named "text" and I wrote an application using php. The application emails all registered users the text that the user entered into the "text" field.
    What prevents a bad guy from using this to send a spam mail to all your users in every second? What do you even need this for?

    You should at least have a time limit and the possibility to "unsubscribe" from this kind of messages. And I hope this is only possible for logged in users?



    Originally Posted by arkingIT
    What I want to do is format the email text so that it has a background color of #dbe5c4, width of 500 pixels.
    Then you need an HTML mail. A plaintext mail has no colors or other styles.

    But I definitely wouldn't use the mail() function for this. Use a mailer library like PHPMailer (download the "PHP4" version, the "PHP5/6" version is mislabeled and completely outdated).
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    7
    Rep Power
    0
    Thank you but I really want to learn PHP and HTML together. I'm sure there's got to be a simple solution and perhaps you don't know the solution but there's got to be someone out there that can instruct a beginning PHP programmer.

    Still yet thank you for your attempt to help me. I need to learn to code I'm a newbie and newbies need baby food so that they can graduate to already designed php code. How am I going to know how to decipher the long code that you or anyone else sends me if I don't know what is going on and how to read it in the first place?

    I'm sure now that you can see my delima
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    7
    Rep Power
    0

    Yak, I'm yaking code


    Why am I yakking code?

    yak, I need help, please someone help me
  8. #5
  9. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,923
    Rep Power
    1045
    What the hell are you talking about? The library I gave you is the simplest solution you'll ever find, because it frees you from manually setting mail headers in the mail() function. Have you even looked at the page? There are lots of examples you can copy and paste:

    http://phpmailer.worxware.com/index.php?pg=examplebmail

    If you find calling a method like AddAddress() or Send() too complicated, I don't know what you expect from us. I mean, there's no magical PHP function send_mail_to_all_my_users() that will do all the work for you. Sure, there's also the possibility to fumble with the mail headers if you don't like libraries. But I doubt that you'll find that easier.

    If you have concrete questions regarding the library, simply ask them. I'm sure we can help you with that. And of course there are many other libraries if you don't like this one. But they all work more or less the same.
    Last edited by Jacques1; November 13th, 2012 at 07:30 PM.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    7
    Rep Power
    0
    "What the hell are you talking about?" That's not a nice way of relating!

    I'll look at the code, ty.
  12. #7
  13. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,301
    Rep Power
    7170
    Thank you but I really want to learn PHP and HTML together. I'm sure there's got to be a simple solution and perhaps you don't know the solution but there's got to be someone out there that can instruct a beginning PHP programmer.
    You can't format an E-Mail without using HTML. A text email can only be sent unformatted.

    Formatting text using HTML isn't hard, but if you don't know how to do that you're going to have to learn it first. For example, if you want to send red text in your email you have to use something like:
    Code:
    <span style="color:red">red text</span>
    A basic HTML tutorial is beyond the scope of this forum, but there are many of them online.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  14. #8
  15. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,923
    Rep Power
    1045
    Originally Posted by arkingIT
    "What the hell are you talking about?" That's not a nice way of relating!
    Not even looking at the page and saying that you rather wait for somebody else to give a "simple solution" I might not know isn't very nice either, right?

    The thing is: I'll happily answer your questions or look for a different solution if you have specific preferences. But if I link you to a script that was explicitly designed to make sending mails easy and you just ignore it, well, what am I supposed to think about that?

    Long story short: If you want to stick with the mail() function, you need to set the mail header as described in the php.net link I gave you. Otherwise you need an external library.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    7
    Rep Power
    0
    I like the idea of red text. How am I suppose to use it in my email message and since $msg is the variable that hold the form info (mail($to, $subject, $msg, 'From: ' . $from); ) I'll need to know how to use red text in that variable. I tried to use the following but it didn't work. The first, third, and forth line works, but I need the second line to work too:

    $msg = "Dear $first_name $last_name";
    $msg .= '<span style="color:red">Dear $first_name $last_name </span>';
    $msg .= "\n\n";
    $msg .= "$text";
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    7
    Rep Power
    0
    Your telling me that I have to use someone else's code to use color my emails? I've never had problems with using color and I'm an artist by nature and that's why I like to use color in my html emails. I'm perplexed because out of all the years I've worked with Dreamweaver, Photoshop, CSS I thought that I'd never be able to not use my own color using a set standard of syntax for e-mails.

    It's just taking me for a surprise that's all. I'm disappointed but what can I expect, I'm new at this stuff.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Location
    Sydney Australia
    Posts
    178
    Rep Power
    83
    Originally Posted by arkingIT
    Your telling me that I have to use someone else's code to use color my emails? I've never had problems with using color and I'm an artist by nature and that's why I like to use color in my html emails. I'm perplexed because out of all the years I've worked with Dreamweaver, Photoshop, CSS I thought that I'd never be able to not use my own color using a set standard of syntax for e-mails.

    It's just taking me for a surprise that's all. I'm disappointed but what can I expect, I'm new at this stuff.
    "Someone else's code" makes your job easier.

    Email is plain old text, no formatting, no colour, nothing, just text, unless you build it as html.
    If you want to do that by hand, it's not hard, just look up mail() in the php manual. There's an example of how to do html email
    http://au1.php.net/manual/en/function.mail.php
    Example #4

    BUT, you need to know how to build the html, with css, to make your mail look right.
  22. #12
  23. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,923
    Rep Power
    1045
    arkingIT, what are you here for? Do you want to solve your problem and get on? Or do you just want to chat a bit?

    If you're here to get help, then, for god's sake, start to read what people are telling you and click on the links you get.

    I told you at least twice that you have two options, the mail() function being one of them. I also gave you a link to php.net with an explanation of how to set the headers in the mail() function to send an HTML mail (it's also in BarryG's reply). But you keep complaining about having to use "someone else's code". No, you do not have to use "someone else's code". You can stick to your mail() function if you want to. But then you have to set additional headers (do you want me to write down the link again?).

    Again, there are two possibilities to send HTML mails:
    1. via the mail() function, but then you have to set several headers
    2. with an external library, which you obviously don't want to use


    Both options require some work on your part. You have to at least copy and paste the examples from the links. Don't expect us to do that for you.

    By the way: You should definitely get accustomed to using "someone else's code". If you're unable to choose the right tool for the job and instead keep reinventing the wheel, you won't get very far. Every website that's more than just some pretty HTML relies on external libraries and frameworks.

IMN logo majestic logo threadwatch logo seochat tools logo