Sending Email in Php

by 9 replies
11
Recently I got a working script to rss processing and sending email.But unfortunately it is sending the plain text version , which makes the email to read very difficult.Below is the screen shot and any one help me to trace this problem..

#programming #email #php #sending
  • You need to make sure you are sending the right headers to the php mail() function call. Here's an example:

    $headers = "From: " . strip_tags($_POST['req-email']) . "\r\n"; $headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n"; $headers .= "CC: example@example.com\r\n"; $headers .= "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
  • This will help you

    <?php
    $to = 'yourmail';
    $from = 'Fromemail';
    $subject = 'the subject';
    $message = 'Test Messgae';
    $headers = 'From:' .$from. "\r\n" .
    'Reply-To: replymail' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

    mail($to, $subject, $message, $headers);
    ?>
  • This is a great solution to code in php such that it will send email to various persons and also will have the ability to send email to more than 100 people.
  • hi it is a good solution to send emails via php and nicely work
  • Hello!
    Another thing to keep in mind:
    On most dedicated or VPN server, there isn't installed the required email seding function which PHP relies on to complete the actual email transfer.

    For ubuntu, install using
    apt-get install sendmail

    On CentOS use
    yum install sendmail

    Mos of the times this is why PHP fails to deliver a message.
    • [1] reply
    • I'm not sure you've solved your issue already, but in case you haven't..

      Assuming you are on shared hosting, which you probably are, you have to combine the two replies, from michael_gourlay and vrtechnologies. But I'm going to share my code anyway, because it's easier for me.

      The headers are responsible for your problem - if it will either be sent in plain text, or HTML formatted. The default is plain text (obviously).

      So, here's the code:
      -------------------
      // Main variables
      $from = 'Optional Name <your@mail.com>';
      $to = 'Optional Name <send@to.com>';
      $subject = 'Message subject';
      $message = 'Your message.';

      // Make the headers
      $headers = "MIME-Version: 1.0\r\n" .
      "Content-type: text/html; charset=utf-8\r\n"
      "From: " . $from . "\r\n" .
      "Reply-To: " . $from . "\r\n" .
      "X-Mailer: PHP/" . phpversion();

      // Send the mail
      $success = @mail($to, $subject, $message, $headers);
      -------------------

      The $success variable will be true if the mail is sent, and false if it has failed. Use that for validation purposes, if you need to.

      In case you want the subject to be in different language than English, I can give you the solution with that too. But if you don't to - the code above is good enough.

      Also, you can go a step further and wrap that code in a function. I extracted the code you see from mine - MailHtml($from, $to, $subject, $message);
      It's the right way to go, if you need to send HTML mails in multiple places in your code.

      I hope this has been helpful.
      • [1] reply
  • Wordpress also have some nice emailing features built-in (if you use wordpress). Check out Function Reference/wp mail « WordPress Codex
    • [1] reply
    • Hi, do you have any experience with coding UTF-8 in subject of the email?

      I've tried many time to show chars like ěščřžýáíé, but with no good responce

      Thanks

Next Topics on Trending Feed

  • 11

    Recently I got a working script to rss processing and sending email.But unfortunately it is sending the plain text version , which makes the email to read very difficult.Below is the screen shot and any one help me to trace this problem..