PHPMailer allows mime boundary to be visible - smtp

I am using PHPMailer to send email via smtp-relay.gmail.com - see previous post After making an account through G-Suite, my credentials are being accept, but when the email is delivered, I can see the plain text version, as well as, the html version, with some other characters along the way:
------example.com----250cd4bbb8be52d828379181e485c269 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Order Received
...
------example.com----250cd4bbb8be52d828379181e485c269 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit
Followed by the html version, that ends with the following:
------example.com----250cd4bbb8be52d828379181e485c269--
When I was using the vanilla version, it worked just fine without seeing the plain text or the mime boundary data:
$send = mail($recpEmails, $subject, $htmlMessage, $headers);
The variable $htmlMessage still holds the same information as before, but now PHPMailer is sending it via the following line:
$mail->Body = $htmlMessage;
I would not see the plain text or these other lines with the dashes. Why would sending via PHPMailer > smtp-relay.gmail.com change the results?
Is it because I added the following line?
$mail->IsHTML(true);
Is it due to the following line and, if so, then what should I set it to?
$mail->SMTPDebug = 3;
Are there configurations in the G-Suite> email> advanced settings that I need to change?
Here is the updated code from the previous post:
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->SMTPDebug = 3;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Host = "smtp-relay.gmail.com";
$mail->Port = "587";
$mail->Username = "info#example.com";
$mail->Password = "somePassword";
$mail->setFrom("info#example.com");
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $htmlMessage;
$mail->addAddress($recpEmails);
$mail->Send();
Thanks in advance

Turns out that the answer is in the following stackoverflow post
I used to build the mime boundary between the text only and html version via PHP's mail in the following way:
Create the text message - this should be exactly the same as your html to avoid being marked as spam:
$plain_text = ""; // same words that will appear in the html
Start creating the mime boundary:
# -=-=-=- MIME BOUNDARY
$mime_boundary = "----example.com----".md5(time());
# -=-=-=- MAIL HEADERS
Create the subject:
$subject = "example.com\n";
Create the headers, which will include the mime boundary that you began earlier. As well as, adding the plain text message in the header - in between the first mime boundary:
$headers = "From: example.com <cs#example.com>\n";
$headers .= "Reply-To: example.com <cs#example.com>\n";
$headers .= "BCC: example.com <info#example.com>\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/alternative; boundary=\"$mime_boundary\"\n";
$htmlMessage = "--$mime_boundary\n";
$htmlMessage .= "Content-Type: text/plain; charset=us-ascii\n";
$htmlMessage .= "Content-Transfer-Encoding: 7bit\n";
$htmlMessage .= "$plain_text\n";
$htmlMessage .= "--$mime_boundary\n";
$htmlMessage .= "Content-Type: text/html; charset=UTF-8\n";
$htmlMessage .= "Content-Transfer-Encoding: 8bit\n\n";
$htmlMessage .= "<html>\n";
$htmlMessage .= "<body>\n";
$htmlMessage .= "" // has to be the same words as the plain text to avoid being marked as spam
$htmlMessage .= "</body>\n";
$htmlMessage .= "</html>\n";
Complete the mime boundary:
# -=-=-=- FINAL BOUNDARY
$htmlMessage .= "--$mime_boundary--\n\n";
# -=-=-=- SEND MAIL
However, with PHPMailer all of this code is no longer necessary by using the following two lines of code:
$mail->AltBody = $plain_text;
$mail->Body = $htmlMessage;
The following is what I have tested and use:
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->SMTPDebug = 3;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Host = "smtp-relay.gmail.com";
$mail->Port = "587";
$mail->Username = "cs#example.com";
$mail->Password = "somePassword";
$mail->setFrom("cs#example.com");
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->AltBody = $plain_text;
$mail->Body = $htmlMessage;
$mail->addAddress($recpEmails);
$mail->Send();
Almost forgot, but this is how I added BCC:
$mail->addBCC("info#example.com");

Related

Create CSV from MySQL database and attach to PHPMailer email

I am trying to make a php file that will create a CSV file of my database and attach it to a PHPMailer email that will automatically send. The file will be on a webserver on a raspberry pi. I got the file to create the CSV file and send an email separately, but not attach the CSV to email and send correctly. Any help would be greatly appreciated! Here is the code so far:
<?php
require("connect2.php");
$filename = "hoursandpay.csv";
header('HTTP/1.1 200 OK');
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=$filename');
header('Cache-Control: no-cache, no-store, must-revalidate');
header('Pragma: no-cache');
header('Expires: 0');
$output = fopen($filename, 'w');
fputcsv($output, array('ID', 'PIN', 'FNAME', 'LNAME', 'DATE'));
$query = "SELECT * FROM hoursandpay ORDER BY DATE DESC";
$result = mysqli_query($conn, $query);
while($row = mysqli_fetch_assoc($result))
{
fputcsv($output, $row);
}
require_once('PHPMailer_5.2.0/class.phpmailer.php');
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->Username = "XXXXX#gmail.com";
$mail->Password = "XXXXXXXX";
$mail->SetFrom('XXXXXX#gmail.com', 'Test123');
$mail->Subject = "Hours and Pay CSV File";
$mail->MsgHTML('Hi! <br><br> Here is the Hours and Pay datatable.
<br><br> Thanks!');
$mail->AddAddress('XXXXX#gmail.com', 'Test User');
$mail->AddAttachment($filename);
unlink($filename);
$mail->Send();
fclose($output);
?>
You’re using an old version of PHPMailer, and even on rpi you should be running a recent enough version of PHP to allow you to use PHPMailer 6.x.
You’re deleting the file you want to send before the send happens. addAttachment stores the details of the file, but not its content, which is not read until it’s actually sent. So move the unlink call to after your send call.

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.

How can I add smtp support?

First of all, I'm new here and PHP :).
My question is about Smtp support. My code is doing send recover password. But my hosting need smtp support. I didn't add it. I read a lot post but my knowledge basic.
How can I add smtp support?
function sendRecover($to, $title, $url, $from, $username, $salt) {
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: '.$title.' <'.$from.'>' . "\r\n";
$subject = 'Password Recovery - '.$title;
$message = 'A password recover was requested, if you didn\'t make this action please ignore this email. <br /><br />Your Username: <strong>'.$username.'</strong><br />Your Reset Key: <strong>'.$salt.'</strong><br /><br />You can reset your password by accessing the following link: '.$url.'/index.php?a=recover&r=1';
return #mail($to, $subject, $message, $headers);
}
Use PHPMailer
The current "official" version of PHPMailer is available through Github:
https://github.com/Synchro/PHPMailer
For implementation you can refer the links below :
http://www.htmlgoodies.com/beyond/php/article.php/3855686/PHP-Mailer-Script-Step-by-Step.htm
or
http://phpmailer.worxware.com/index.php?pg=examplebsmtp
If you have access to edit the php.ini then you can do something like this:
[mail function]
SMTP = ssl://smtp.gmail.com
smtp_port = 465
username = info#Mmydomainname.com
password = myemailpassword
sendmail_from = info#mydomainname.com

How to insert mail addresses from database to mail sending script

Is there a possible way, to insert data from table to mail sending script? I have made this simple script, but it doesnt work. How to mix these 2 codes?
$result = mysql_query("SELECT * FROM tablename WHERE ID =1" ) or die(mysql_error()); while($row = mysql_fetch_array( $result )) { echo ''. $row['maillist'] .''; }
$to = 'here must be maillist row';
$subject = 'my subject:';
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$message = 'html content with img src tag';
mail($to, $subject, $message, $headers);
My second question is: If i am using bcc, than gmail or other mail services displaying full code of message with all tags, but not displaying image. So, is there a possible way to fix this problem?
My third question is: If i am inserting image to message (watch the code), then the message appears in SPAM, but if i am using only basic text, than its all normal. How to fix it?
I will be grateful for any answers and help!
$q = "SELECT email FROM table WHERE id = '" . $id . "'";
$r = mysql_query($q) or die(mysql_error().'<br />'.$q);
$d = mysql_fetch_assoc($r);
$to = $d['email']:
$subject = 'my subject:';
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$message = 'html content with img src tag';
mail($to, $subject, $message, $headers);
If you wanna send email to several people :
$subject = 'my subject:';
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$message = 'html content with img src tag';
$q = "SELECT email FROM table WHERE id = IN (" . $array_ids . ")";
$r = mysql_query($q) or die(mysql_error().'<br />'.$q);
while($row = mysql_fetch_assoc($r)) {
$to = $row['email']:
mail($to, $subject, $message, $headers);
}
For the SPAM problem, I'd say try to set you header like this
$headers .= 'Content-type: image/jpeg' . "\r\n";