Reputation:
I have this helper class which I use to send E-Mails through PHP:
<?php
class Mailsend {
public $from;
public $to;
public $subject;
public $message;
public static function mail() {
return new Mailsend();
}
public function from($from) {
$this->from = $from;
return $this;
}
public function fromName($fromName) {
$this->fromName = $fromName;
return $this;
}
public function to($to) {
$this->to = $to;
return $this;
}
public function subject($subject) {
$this->subject = $subject;
return $this;
}
public function message($message) {
$this->message = $message;
return $this;
}
public function send() {
$mailFrom = $this->from;
$mailFromName = $this->fromName;
$mailTo = $this->to;
$mailSubject = $this->subject;
$mailSignature = "\n\n-- \n";
$mailSignature .= "Vaš, Format.ba - Primjenjene Vještine.\n";
$mailSignature .= "Za više informacija posjetite nas na: http://www.format.ba/\n";
$mailBody = $this->message . "\n";
$mailBody .= $mailSignature;
$mailHeader = "From: $mailFromName <$mailFrom>\r\n";
$mailHeader .= "MIME-Version: 1.0\r\n";
$mailHeader .= "Content-type: text/html; charset=UTF-8\r\n";
$mailHeader .= "Reply-To: $mailFromName <$mailFrom>\r\n";
$mailHeader .= "X-Mailer: Mailsend.php\r\n";
$mailHeader .= "X-Sender-IP: {$_SERVER['REMOTE_ADDR']}\r\n";
$mailHeader .= "Bcc: $mailFromName <$mailFrom>\r\n";
$mailParams = "-f$mailFrom";
$mailBody = str_replace(' ', '', $mailBody);
return mail($mailTo, $mailSubject, $mailBody, $mailHeader, $mailParams);
}
public function valid($email) {
if (!preg_match("/^[^@]{1,64}@[^@]{1,255}$/", $email)) {
return false;
}
$email_array = explode("@", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if (!preg_match("/^(([A-Za-z0-9!#$%&'*+\/=?^_`{|}~-][A-Za-z0-9!#$%&'*+\/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$/", $local_array[$i])) {
return false;
}
}
if (!preg_match("/^\[?[0-9\.]+\]?$/", $email_array[1])) {
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2) {
return false;
}
for ($i = 0; $i < sizeof($domain_array); $i++) {
if (!preg_match("/^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$/", $domain_array[$i])) {
return false;
}
}
}
return true;
}
}
It works and I wrote it so it is easy to use, like this for an instance:
$message = "Hello, " . $model->fullname . ",
Welcome to our Website, Domain.tdl. You are now able to use all of our services which we provide here.
Visit: www.domain.tdl - for more information and news about our services!
Greets!";
Mailsend::mail()->
from('[email protected]')->
fromName('Domain.tdl - Our Website')->
to('[email protected]')->
subject('Welcome to Domain.tdl - Our Website')->
message(trim($message))->send();
And as you can see from this exact example there is alot of whitespace which is indented by "empty spaces", not tabs.
And when I send that message to one of my E-Mails, I get this output:
Hello, Zlatan Welcome to our Website, Domain.tdl. You are now able to use all of our services which we provide here. Visit: www.domain.tdl - for more information and news about our services! Greets!
It just somehow strips all the newlines and more-than-two-in-length whitespaces, and merges them all together, so I lose my originally intended E-Mail format.
I have tought that this line in the Mailsend
might cause the problem:
$mailBody = str_replace(' ', '', $mailBody);
But that's not the case, and so far I have not find any reasons why this accours and could not prevent it going on.
Has anyone expirenced something similar? Is it possible that I'm missing something in my own code?
Upvotes: 1
Views: 1328
Reputation: 158060
You are using the following header:
$mailHeader .= "Content-type: text/html; charset=UTF-8\r\n";
but are about to send plain text emails. Change it to
$mailHeader .= "Content-type: text/plain; charset=UTF-8\r\n";
or format your email as html
Upvotes: 2
Reputation: 2935
Use <br />
instead of \r\n
.
Add this header in order to make your content HTML formatted:
Content-type: text/html; charset=UTF-8
When you want to insert tabs, use multiple
instead.
Upvotes: 2
Reputation: 30488
Use this with <br>
$message = "Hello, " . $model->fullname . "<br>,
Welcome to our Website, Domain.tdl. You are now able to use all of our services which we provide here.<br>
Visit: www.domain.tdl - for more information and news about our services!<br>
Greets!";
Upvotes: 4