1

This might sound similar like previously asked questions but trust me it's not

I Was trying to send an email that uses an HTML template via PHP mail() function from Localhost and a Hostinger Server but they created different problems.

  1. On localhost the email was being sent as plain text although there were headers

     $headers =
         "MIME-Version: 1.0\r\n" . 
         "Content-Type: text/html; charset=UTF-8";
    

    I have gone through all the similar questions in stackoverflow and tried each and every thing but I couldn't make it work. After some more research on this I found out this

    I assume, that your email client is considering the smtp-server "unsafe", and hence is just going to display all the html as plaintext, rather than rendering it

  2. Therefore I switched over my hosting and tried to do the same but this time I found that the headers are causing the problem. The email is not sent if the header variable is passed in the mail() function. I tried to concatenate the headers which didn't worked. Then I made an array of headers and joined them with php implode which too didn't worked. On a similar question on stackoverflow I found that webmails mess up if html, head, body tags are used as they use xhtml. I removed them and still no success.

I tried error reporting too and it showed module sqlite3 already loaded which I think is not related to mail.

Below is my code

php

<?php
 $email_template = file_get_contents("path/to/my/template");
 $lucky_number = rand(999999, 111111);
 $email_template = str_replace("{{user}}", "User", $email_template);
 $email_template = str_replace("{{lucky_number}}", $lucky_number, $email_template);
 $sender = "from:iusername@host.com"; // I found that if I dont use from, my mail ends up in spam folder
 $receiver = "username@host.com";
 $subject = "Random Subject Name";
 $headers =
    "MIME-Version: 1.0\r\n" . 
    "Content-Type: text/html; charset=UTF-8";

 if(mail($receiver, $subject, $email_template, $sender, $headers))
 {
    echo "Email Sent Successfully";
 }
 else
 {
    echo "Email Sending Failed";
 }

P.S I can't use PHPMailer or other similar libraries

James Z
  • 12,209
  • 10
  • 24
  • 44
Joy Dey
  • 563
  • 1
  • 5
  • 17
  • `I can't use PHPMailer`...why not? It's free, and quite easy to use – ADyson Jun 12 '21 at 07:11
  • Actually I wanted to but its not allowed for this part of my task. Organizational Restrictions :( – Joy Dey Jun 12 '21 at 07:12
  • 1
    Well whatever your organisation is, they seem to like making life difficult and unproductive for their developers...what a strange way to work. I'm sorry for you. – ADyson Jun 12 '21 at 07:15
  • :( Please Can you help me in my query? – Joy Dey Jun 12 '21 at 07:21
  • 1
    Since it behaves differently on different environments it could be related to underlying mailserver settings. https://stackoverflow.com/a/24644450/5947043 is an excellent guide to most things that can go wrong when sending mail with PHP. – ADyson Jun 12 '21 at 07:27
  • 1
    P.S. I just also noticed that you're using the mail() function incorrectly too ... https://www.php.net/manual/en/function.mail.php . What you're putting in the $headers field doesn't match what's allowed in the additional_params argument (as per the manual) and what you're putting in $sender doesn't quite match the header format required in the additional_headers argument. As per the answer below, the sender info should be part of the headers, all passed as one argument to the function. – ADyson Jun 12 '21 at 07:29
  • Actually as I'm rendering an html email template so I added this headers along with the sender info – Joy Dey Jun 12 '21 at 07:41
  • 1
    Yes that's fine but you missed my point - they are not separate variables, they all go in as one string of headers into the mail function. Sender is part of headers. Read the manual or almost any example in a tutorial, it would show you how to do it. The example in the answer below also shows. – ADyson Jun 12 '21 at 07:44

1 Answers1

1

The sender information should be inside the headers

Hence, please change the following lines:

 $headers =
    "MIME-Version: 1.0\r\n" . 
    "Content-Type: text/html; charset=UTF-8";

 if(mail($receiver, $subject, $email_template, $sender, $headers))
 

to

$sender = "iusername@host.com";

$headers = "From: $sender <$sender>\r\nReply-To: $sender\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=utf-8\r\n";

 if(mail($receiver, $subject, $email_template, $headers))
Ken Lee
  • 6,985
  • 3
  • 10
  • 29
  • @Ken this doesn't work. Please read my query again. I have mentioned that without header its working fine. – Joy Dey Jun 12 '21 at 07:28
  • Without header you cannot tell the system that you are sending HTML format mail. Please note that I have slightly amended the codes, please try again. – Ken Lee Jun 12 '21 at 07:28
  • Unfortunately even with the headers the email is rendered in plain text from localhost. – Joy Dey Jun 12 '21 at 07:38
  • 1
    Is your localhost a well configured linux server ?? – Ken Lee Jun 12 '21 at 07:40
  • Yes it is. I just tried out my webmail for checking out the message headers. Strangely I couldn't find the `Content-Type` and the `MIME-Version` headers – Joy Dey Jun 12 '21 at 07:45
  • Please try the Hostinger Server instead and see the result – Ken Lee Jun 12 '21 at 07:50
  • Hostinger isnt allowing me to send the mail with the headers :( – Joy Dey Jun 12 '21 at 07:50
  • Yes I did. It worked now. Can You please provide an explaination? – Joy Dey Jun 12 '21 at 07:55
  • 1
    The 1st line of my answer is the explanation : `The sender information should be inside the headers` – Ken Lee Jun 12 '21 at 07:56
  • And the rendering issue? Is that due to my mentioned reason which I found? `I assume, that your email client is considering the smtp-server "unsafe", and hence is just going to display all the html as plaintext, rather than rendering it` ?? – Joy Dey Jun 12 '21 at 07:59
  • 1
    For the rendering issue in your "localhost", your localhost SMTP may be considered as unsafe because there can be no PTR record for the IP address of your local machine. You need to file an application with your ISP to add your localhost machine's IP a PTR record. (but I think you need to pay them for this) – Ken Lee Jun 12 '21 at 08:05
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/233720/discussion-between-joy-dey-and-ken-lee). – Joy Dey Jun 13 '21 at 16:02