Welcome, Guest
  • Author Topic: PHP mail question  (Read 3327 times)

    ayok

    • Seasoned Programmer
    • ***
    • Posts: 107
      • View Profile
    PHP mail question
    « on: 02/10/07, 21:22 »
    I'm trying to make a php mail which shows an image on the mail body. I can show it with html format, but I have to type <br> for every line of the messages.. that's quite annoying, because it would be kind of newsletter.

    Could anybody tell me how I can get it work?

    thx
    ayok

    bpat1434

    • Mods
    • Senior Programmer
    • *****
    • Posts: 419
    • Never Miss An Opportunity To Be Great.
      • MSN Messenger - bpat1434@hotmail.com
      • AOL Instant Messenger - bpat1434
      • Yahoo Instant Messenger - bpat1434
      • View Profile
      • bPatterson
    Re: PHP mail question
    « Reply #1 on: 02/13/07, 21:26 »
    Unfortunately the only way to show an image inside an email message is with HTML.  That said, you can write the email in plain text and use the php function nl2br(inputString) to change all "new Line" characters into "<br />" tags.

    E.g.
    <?php

    $username 
    $_POST['username'];

    $message "Hello {$username}, and welcome to my website ;)

    <img src=\"http://www.mysite.com/images/welcome_image.jpg\" />

    Hopefully you saw that image ;)"
    ;

    $message nl2br($message);

    mail($to$subject$message$headers$parameters);

    ?>


    It's a shrunk down example, but its' basically what would have to be done.

    ~Brett

    bpat1434

    • Mods
    • Senior Programmer
    • *****
    • Posts: 419
    • Never Miss An Opportunity To Be Great.
      • MSN Messenger - bpat1434@hotmail.com
      • AOL Instant Messenger - bpat1434
      • Yahoo Instant Messenger - bpat1434
      • View Profile
      • bPatterson
    Re: PHP mail question
    « Reply #2 on: 02/13/07, 21:28 »
    Missing the edit button....

    You can also send the plain-text version along with it so that plain-text sees your original message, and HTML viewers get the nice image and such.

    ayok

    • Seasoned Programmer
    • ***
    • Posts: 107
      • View Profile
    Re: PHP mail question
    « Reply #3 on: 02/14/07, 09:28 »
    Ah.. bpat! you're the master... hehe.. I've been asking questions in any php forums without answer.

    $message = nl2br($message) is what i need. Thx...

    But I still have a question... First I show you my code for the send.php.
    First the html code for the form which will be sent to emails
    Code: [Select]
    <form method="POST" action="<?php echo($PHP_SELF); ?>">
     
    <h3>News </h3>
      <p>Select Mailing List:<br>
      <select size="1" name="list_choice">
        <option selected>emails.txt</option>
       
      </select></p>
      <p>Title:<br>
      <input type="text" name="subject_choice" size="50"></p>
      <p>Message:<br>
      <textarea rows="6" name="message_choice" cols="42"></textarea></p>
      <p><input type="submit" value="Send Mail" name="sendmail"><input type="reset" value="Reset" name="B2"></p>
    </form>

    And then below it, the code for the email's format
    Code: [Select]
    $header = "From: $from_mail; \n";
    if ($sendmail == "Send Mail"){

    print ("<H3>Sending news to emails...</H3><P>");

    $file_handle = fopen($list_choice, "r") or die("Couldn't open mailing list..");

    while (!feof($file_handle)) {
    set_time_limit(0);
    $address = fgets($file_handle, 1024);

    mail("$address", stripslashes($subject_choice),
    stripslashes($message_choice), $header)

    I add $header .= "Content-type: text/html; charset=iso-8859-1\r\n"; and $message_choice = nl2br($message_choice);
    Now I don't need to add <br> for every new line. However, I still need to type <img src=\"http://www.mysite.com/images/welcome_image.jpg\" /> in the message field everytime I want to send an email.

    Is there anyway to set it in the $message_choice, so that I don't need to insert it for every new message?

    Thank you,
    ayok

    Jorge Solis

    • Global Moderator
    • Systems Administrator
    • *****
    • Posts: 14616
      • View Profile
    Re: PHP mail question
    « Reply #4 on: 02/14/07, 13:24 »
    bpat1434 was Brett Patterson in other times ;)
    Welcome again !

    Jorge

    ayok

    • Seasoned Programmer
    • ***
    • Posts: 107
      • View Profile
    Re: PHP mail question
    « Reply #5 on: 02/14/07, 13:30 »
    I see..
    Thank you Brett Patterson..
    I already solve my problem.. but there is still another problem..
    Why I cannot send the emails, since i change it to html, to HOTMAIL? I already check the bulk/junk mail..

    Thanks again,
    ayok

    Musicman

    • Global Moderator
    • Systems Administrator
    • *****
    • Posts: 2685
      • View Profile
      • Email
    Re: PHP mail question
    « Reply #6 on: 02/15/07, 10:16 »
    Hi,

    there is one things to successfully sending mails - obeying standards .... and just putting a html body without MIME mimikry is not standard, although it is often seen

    Musicman

    ayok

    • Seasoned Programmer
    • ***
    • Posts: 107
      • View Profile
    Re: PHP mail question
    « Reply #7 on: 02/16/07, 05:18 »
    Hi Musicman,
    I added this code on the header,
    Code: [Select]
    $header = "From: $from_mail; \n";
    $header .= "MIME-Version: 1.0\n";
    $header .= "Content-type: text/html; charset=iso-8859-1\n";
    $header .= "X-Priority: 1\n";
    $header .= "X-MSMail-Priority: High\n";
    $header .= "X-Mailer: DPLN Belanda Newsletter";

    But I still cannot send to hotmail.. do i put it wrong?

    Thx
    ayok

    Musicman

    • Global Moderator
    • Systems Administrator
    • *****
    • Posts: 2685
      • View Profile
      • Email
    Re: PHP mail question
    « Reply #8 on: 02/16/07, 15:38 »
    Hi,

    here is mail sent with normal mail client.....

    Code: [Select]
    Date: Fri, 16 Feb 2007 12:48:50 +0500
    From: "whoever" <whoever@gmail.com>
    To: "whoelse" <whoelse@gmail.com>
    Content-Type: multipart/mixed; boundary="===============1791483408=="

    --===============1791483408==
    Content-Type: multipart/alternative;
    boundary="----=_Part_2043_16005042.1171612130746"

    ------=_Part_2043_16005042.1171612130746
    Content-Type: text/plain; charset=ISO-8859-1; format=flowed
    Content-Transfer-Encoding: 7bit
    Content-Disposition: inline

    This is a solution:

    http://weblogs.macromedia.com/flashjavascript/


    If you're using Flash 8, it has ExternalInterface Class which is another
    solution for such requirements.

    --
    whoever

    ------=_Part_2043_16005042.1171612130746
    Content-Type: text/html; charset=ISO-8859-1
    Content-Transfer-Encoding: 7bit
    Content-Disposition: inline

    This is a solution:<br><br><a href="http://weblogs.macromedia.com/flashjavascript/">http://weblogs.macromedia.com/flashjavascript/</a>
    <br><br><br>If you&#39;re using Flash 8, it has ExternalInterface Class which is another solution for such requirements.
    <br><br>--<br>whoever<br><br><br>

    ------=_Part_2043_16005042.1171612130746--


    --===============1791483408==
    .... more stuff here, like embedded images

    --===============1791483408==--

    Note the use of MIME headers (multipart/alternative for text + html, multipart/mixed or multipart/related
    for other stuff inside the same message) and those funny boundaries.
    If there is no images / attachments, you would still see the multipart/alternative and the outer boundary

    Musicman

    « Last Edit: 02/16/07, 17:20 by Jorge Solis »