13

I am sending a mail using PEAR's mail and mail_mime packages and sample code as below:

$sendStart=array();
require_once('Mail.php');
require_once('Mail/mime.php');

$sendStart['mail'] =& Mail::factory('mail');
$sendStart['mime'] = new Mail_mime("\n");

$sendStart['mime']->setHTMLBody($html);
$sendStart['headers']['Subject']=$title;
$sendStart['headers']['X-SMTPAPI']='{"category": ["MailID-XXX"]}';

$body=$sendStart['mime']->get(array(
        'html_charset'=>'UTF-8',
        'text_charset'=>'UTF-8',
        'head_charset'=>'UTF-8'
    ));

//echo ($sendStart['mime']->_htmlbody); exit;
$sendStart['mail']->send('xxx@example.com',$sendStart['mime']->headers($sendStart['headers']),$body);

I am facing a strange issue while sending the mails through this code. I have images inside the email body, sometimes the images not displaying. When I debug the issue, I found that . is missing in the image url. But if I print the mail just before the send line (as I comment out in the code) it prints with the image perfectly.

correct image url: http://www.domain.com/image.png

in the mail : http://www.domaincom/image.png or http://www.domain.com/imagepng ... etc.

part of the HTML code which has the image as below:

<table cellpadding="0" cellspacing="0" border="0" class="image-table image-2" align="center" style="float:none;margin-left:auto;margin-right:auto;text-align:left;">
    <tbody>
      <tr>
        <td class="element" style="text-align: left;height: auto;overflow: hidden;-webkit-text-size-adjust: none;">
          <!--[if gte mso 9]><img alt="Placeholder Image" src="http://www.domain.com/image.png" style="outline: none; text-decoration: none; display: block; clear: none; float: none;    margin-left: auto; margin-right: auto;display:none; mso-hide: none;" align="center" width="394"><![endif]--><![if !mso]><!-- --><img alt="Placeholder Image" src="http://www.domain.com/image.png" style="outline: none;text-decoration: none;display: block;clear: none;float: none;width: 100%;height: auto;max-width: 394px;margin-left: auto;margin-right: auto;*width: 394px;-ms-interpolation-mode: bicubic;" align="center"><!--<![endif]-->
        </td>
      </tr>
    </tbody>
</table>

And the very strange thing is it is displaying properly in outlook but not in other clients as for outlook I have separate code(as per the code).

Does anybody have any idea how to debug the issue or any comment on this issue.

Edited:

This issue is nothing to do with any specific tag (although I explain it using image tag), I experience it in several places such as in styles.

example : line-heigth:1.5; is the original and it changes to line-heigth:15; while sending out.

Basically it just remove the . here & there in the email HTML.

Kenster
  • 23,465
  • 21
  • 80
  • 106
Janith Chinthana
  • 3,792
  • 2
  • 27
  • 54
  • What are the values of `$html` and `$body`? – Dave Chen Jul 05 '15 at 06:01
  • actually its little bit big html template which I can't put inside the question although I will edit it with relevant image part, @DaveChen please check the edited question – Janith Chinthana Jul 05 '15 at 06:03
  • Interesting... you send different emails for MS-Outlook? How do you know in advance what receiver uses what email client? – arkascha Jul 05 '15 at 06:23
  • @arkascha no, as in the code for outlook I used ` – Janith Chinthana Jul 05 '15 at 06:44
  • So the old thing is still required, even today, to create special solutions for each and every MS product? *sigh* – arkascha Jul 05 '15 at 06:49
  • Do you have any way of logging the actual outbound messages that are sent? Anytime I see "outlook" and "mail message problems" in the same sentence I start smelling an Outlook issue with the way it will process HTML-formatted messages... – David W Jul 07 '15 at 20:16
  • @DavidW yes, I have a log and the html there is perfect, but when I receive it I have this issue. Actually it is not a client based problem, it happens in gmail as well as outlook. in question i mention only a sample code, so it was effected to gmail and other email clients not in outlook – Janith Chinthana Jul 08 '15 at 05:14
  • What library you are using? is it handcrafted? If it is a library can you share the repository url else what are the contents of `Mail/mime.php` and 'Mail.php`? – Ugur Jul 10 '15 at 17:33
  • check the header in the mail is it correct ? – rajatsaurastri Jul 13 '15 at 12:53
  • Are you sending UTF-8? Is your email set as UTF-8? – Machavity Jul 14 '15 at 02:14
  • (Considering dot stuffing) I think the problem is Its not adding extra (.) but removing the (.) in this case. Correct me if am wrong. – Varshaan Jul 14 '15 at 04:27
  • @JanithChinthana I am also facing exactly the same issue. Can you please tell me how you fixed this issue. Please give me an example of how you fixed. – user2086641 Feb 20 '20 at 09:17
  • @JanithChinthana I have created an SO ticket (https://stackoverflow.com/questions/60353635/dot-missingon-domain-name-of-password-reset-email-link-send-by-sendgrid) with my issue. Can you please look into it. – user2086641 Feb 24 '20 at 11:54

3 Answers3

6

I'm pretty sure this is caused by dot-stuffing; as the dot is used as a special indicator in emails. You can read about this in the rfc where it says (emphasis added):

To allow all user composed text to be transmitted transparently, the following procedures are used:

  • Before sending a line of mail text, the SMTP client checks the first character of the line. If it is a period, one additional period is inserted at the beginning of the line.
  • When a line of mail text is received by the SMTP server, it checks the line. If the line is composed of a single period, it is treated as the end of mail indicator. If the first character is a period and there are other characters on the line, the first character is deleted.

It seems the client you're using to compose these emails does not implement the first procedure, while the server it sends the mail to does implement it; leading to dots disappearing.

The fix would be to make your client implement the padding.

Community
  • 1
  • 1
Sjon
  • 4,989
  • 6
  • 28
  • 46
  • I am also facing exactly the same issue. Can you please tell me how to fix this issue programmatically. Please give me an example of how you fixed. – user2086641 Feb 22 '20 at 10:10
2

Use PHPMailer, it will make life a lot easier.

CommonKnowledge
  • 769
  • 1
  • 10
  • 35
0

Here is the example of it -

     // Set up the headers that will be included in the email.
    $recipient = 'someemail@gmail.com';
    $from = 'someemail1@gmail.com';

    $headers = array(
      'To'            => $recipient,
      'From'          => $from,
      'Return-Path'   => $from,
      'Reply-To'      => $replyto, //based on your need
      'Subject'       => $subject,
      'Errors-To'     => '<<a href="mailto:errors@example.com">errors@example.com</a>>',
      'MIME-Version'  => '1.0',
    );

    // Set up parameters for both the HTML and plain text mime parts.
    $textparams = array(
      'charset'       => 'utf-8',
      'content_type'  => 'text/plain',
      'encoding'      => 'quoted/printable',
    );
    $htmlparams = array(
      'charset'       => 'utf-8',
      'content_type'  => 'text/html',
      'encoding'      => 'quoted/printable',
    );

    // Create the email itself. The content is blank for now.
    $email = new Mail_mimePart('', array('content_type' => 'multipart/alternative'));

    // Add the text and HTML versions as parts within the main email.
    $textmime = $email->addSubPart($textbody, $textparams);
    $htmlmime = $email->addSubPart($htmlbody, $htmlparams);

    // Get back the body and headers from the MIME object. Merge the headers with
    // the ones we defined earlier.
    $final = $email->encode();
    $final['headers'] = array_merge($final['headers'], $headers);

    // Perform the actual send.
    $smtp_params = array();
    $smtp_params['host'] = '127.0.0.1';
    $smtp_params['port'] = '25';
    $smtp_params['persist'] = TRUE;

    $mail =& Mail::factory('smtp', $smtp_params);
    $status = $mail->send($recipient, $final['headers'], $final['body']);
rajatsaurastri
  • 653
  • 3
  • 21