Random Spaces in PHP message - html

I am using the following PHP code to send e-mail when the users submits a form.
$to = 'mail#example.com';
$subject = 'Thank you for your mail ' . $mailRefrence . ' - www.example.com';
$headers = "From: " . 'info#example.com' . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$message = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'> <html xmlns='http://www.w3.org/1999/xhtml'> <head> <meta http-equiv='Content-Type' content='text/html;.......";
$message .= $mailReference;
mail($to, $subject, $message, $headers);
Please note that the HTML-code inside the $message-variable is much longer, I've just removed it due to it is way to long to be pasted here. The source code is available here: http://zurb.com/playground/projects/responsive-email-templates/basic.html
However, when the mail is sent to the user, there is random spaces in the text. Sometimes the spaces appear in the text, and sometimes in the code which often ruins the whole design of the mail.
Does anyone have an idea why this happens and how to solve it?

Use trim
http://php.net/manual/en/function.trim.php
this will remove white space

Related

Exclamation Point Randomly In Result of PHP HTML-Email

I'm getting exclamation points in random spots in the result of this PHP email function. I read that it's because my lines are too long or I have to encode the email in Base64 but I do not know how to do that.
This is what I have:
$to = "you#you.you";
$subject = "Pulling Hair Out";
$from = "me#me.me";
$headers = "From:" . $from;
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 64bit\r\n";
mail($to,$subject,$message,$headers);
How do I fix this so there's no random ! in the result? Thanks!
As stated here: Exclamation Point in HTML Email
The issue is that your string is too long. Feed an HTML string longer than 78 characters to the mail function, and you will end up with a ! (bang) in your string.
This is due to line length limits in RFC2822 https://www.rfc-editor.org/rfc/rfc2822#section-2.1.1
Try to use this piece of code:
$to = "you#you.you";
$subject = "Pulling Hair Out";
$from = "me#me.me";
$headers = "From:" . $from;
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 64bit\r\n";
$finalMessage = wordwrap( $message, 75, "\n" );
mail($to,$subject,$finalMessage,$headers);
The problem is that one line should not be longer than 998 characters. (see also https://stackoverflow.com/a/12840338/2136148)
You are right, that is because your email is too long. Try replacing with this line in your mail header.
Content-Transfer-Encoding: quoted-printable
The answers here have the correct information regarding the line length, however none of them provided me with the sufficient code snippet to fix the issue. I looked around and found the best way to do this, here it is;
<?php
// send base64 encoded email to allow large strings that will not get broken up
// ==============================================
$eol = "\r\n";
// a random hash will be necessary to send mixed content
$separator = md5(time());
$headers = "MIME-Version: 1.0".$eol;
$headers .= "From: Me <info#example.com>".$eol;
$headers .= "Content-Type: multipart/alternative; boundary=\"$separator\"".$eol;
$headers .= "--$separator".$eol;
$headers .= "Content-Type: text/html; charset=utf-8".$eol;
$headers .= "Content-Transfer-Encoding: base64".$eol.$eol;
// message body
$body = rtrim(chunk_split(base64_encode($html)));
mail($email, $subject, $body, $headers);
// ==============================================

PHP mail MMS attaching image not working

Hello guys I am working on webapplication for sending multimedia messages upon user input of their phone numbers. I am successfully able to send emails with images in HTML form. Now, I am trying to send my customers MMS via PHP mail function, but the only thing they receive is the link that I send them with the message.
Here is what I have come up with so far.
<?php
$email = '1234567890#somenetwork.domain';
$link = $_COOKIE["coupon"];
$to = $email;
$subject = 'Some Subject';
$message = " Hello, This is Testing Text 8.0
<a href=\"https://encrypted-tbn0.gstatic.com/images? \
q=tbn:ANd9GcS0dA2aipmy9hwAitgD8U5n8l_afNBvxYc3gnOFi7hOGoGAGIHssw\">Your Link</a> ";
$message->addAttachment("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS0dA2aipmy9hwAitgD8U5n8l_afNBvxYc3gnOFi7hOGoGAGIHssw", "image/gif");
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: someone <support#someone.com>' . "\r\n";
mail($email, $subject, $message, $headers);
?>
When sending to a phone as MMS, you have to send the image as an attachment.
I found the following answer to be very helpful for easily sending attachments, even though it references "mail", not MMS:
Send attachments with PHP Mail()?
One of the issues is that you can't fetch that image in that fashion.
I.e. https://encrypted-tbn0.gstatic.com/images?\
q=tbn:ANd9GcS0dA2aipmy9hwAitgD8U5n8l_afNBvxYc3gnOFi7hOGoGAGIHssw
returns an empty file.
Also, since when can you send MMS via PHP's mail() function?
The most reliable way in my experience to send images via SMS/MMS is to send a WAP push msg.

Why php mail has delay when sending message with images

I was building a email functionality for my website and I am using PHP mail function. The issue I am having is that when I try to email a client with a image inside a message it takes forever(20-45 minutes) to get to them and when I just include text it gets to them right away. Is there solution to this. Thanks for any help.
<?php
$email = $_COOKIE["email"];
$link = $_COOKIE["coupon"];
$to = $email;
$subject = 'Your ads';
$message = ' Hello This is Testing Email 3.0 Text & Image Your Coupon Link
<img src="$link" width="300" height="300"/> ';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Ads <ads#advertising.com>' . "\r\n";
mail($email, $subject, $message, $headers);
?>
According to your code, you mail is a top tier Spam grade for almost all anti spamming mailing protection.
They will most likely put your mail in a slow queue because of that, delaying the message because it was considered an annoyance.
Your From header contains 'ads' and 'advertising' (even if I guess that advertising.com isn't your domain.
You also have little to no text, the word test in it and a big link button named "coupon".
You should try to make your email more personal.
This is the most likely problem.
Second one would be the file transfer.
If you're transferring from China to New-York with a 56kb/s connection, the file transfer will take long enough for your recipient to die from old age.
For your second problem, replace
$message = ' Hello This is Testing Email 3.0 Text & Image Your Coupon Link
<img src="$link" width="300" height="300"/> ';
by
$message = ' Hello This is Testing Email 3.0 Text & Image Your Coupon Link
<img src="' . $link . '" width="300" height="300"/> ';

PHP mail() - How to put an html link in an email? [duplicate]

This question already has answers here:
Send HTML in email via PHP
(8 answers)
Closed 9 years ago.
I'm sending an email via PHP's mail() function. In the message I set a link that looks like this:
$message = "<a href='". $link. "'>" .$title. "</a>\n\n";
However, when the email is received, the email body shows the html code instead of the title as a hyperlink. I'm not very familiar with html emails. How could I achieve what I am trying to get?
Try to add an header, so that the mail client doesn't believe it is plain text:
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
See PHP mail function manual:
Example #4 Sending HTML email:
<?php
// multiple recipients
$to = 'aidan#example.com' . ', '; // note the comma
$to .= 'wez#example.com';
// subject
$subject = 'Birthday Reminders for August';
// message
$message = '
<html>
<head>
<title>Birthday Reminders for August</title>
</head>
<body>
<p>Here are the birthdays upcoming in August!</p>
<table>
<tr>
<th>Person</th><th>Day</th><th>Month</th><th>Year</th>
</tr>
<tr>
<td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
</tr>
<tr>
<td>Sally</td><td>17th</td><td>August</td><td>1973</td>
</tr>
</table>
</body>
</html>
';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
?>
Please see the documentation for PHP mail() at http://php.net/manual/en/function.mail.php
You need to specify a Content Type of HTML in your function.
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
Although it's generally recommended to avoid using mail() alone.
You should consider using PHPMailer, for example.
You have to tell the e-mail client that there is an HTML portion to the e-mail. I'd recommend something like Swiftmailer to do the work instead of doing everything yourself.

Changing Font in autogenerated HTML e-mail message

I need to send a lot of numbers over so want to change the font to Courier.
HGow do I do this? My constructs are...
$message = "Contact : ".$_SESSION["Contact"]."\n\r".
...
$mail_sent = #mail( $to, $subject, $message, $headers );
When I try simple tests like bold on/off or spans suggested elsewhere they just appear as the text and are transmitted and appear identically in Outlook and my iPhone. Any ideas?
It seems that you are using php, so the next syntax should work. Just add the next headers to what you already have or replace the one you have with similar content:
$headers .= "Content-Type: text/html; charset=utf-8\r\n";
$headers .= "MIME-Version: 1.0\r\n";
And change your text to something like this:
$message = "Contact : "<span style="font-face:courier;">.$_SESSION["Contact"]<span>."\n\r".
I'm assuming that line is the one with the numbers you want to format. But of course you can modify any line in your message
Bye