What is wrong with this mail header text? - php

The following $header is being sent via PHP's mail($to,$subject,$content,$header) command. The mail arrives and appears to have an attachment. But the mail text is empty as is the file. I think this has something to do with line spacing but I can't see the problem. I have tried putting the contents (between the boundaries) in $contents rather than appending it to $header. It doesn't make a difference. Any thoughts?
From: dnagirl#someplace.com
Reply-To: dnagirl#someplace.com
X-Mailer: PHP 5.3.1
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="7425195ade9d89bc7492cf520bf9f33a"
--7425195ade9d89bc7492cf520bf9f33a
Content-type:text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 7bit
this is a test message.
--7425195ade9d89bc7492cf520bf9f33a
Content-Type: application/pdf; name="test.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="test.pdf"
JVBERi0xLjMKJcfsj6IKNSAwIG9iago8PC9MZW5ndGggNiAwIFIvRmlsdGVyIC9GbGF0ZURlY29k
ZT4+CnN0cmVhbQp4nE2PT0vEMBDFadVdO4p/v8AcUyFjMmma5CqIIF5cctt6WnFBqLD1+4Np1nY3
c3lvfm+GyQ4VaUY11iQ2PTyuHG5/Ibdx9fIvhi3swJMZX24c602PTzENegwUbNAO4xcoCsG5xuWE
.
... the rest of the file
.
MDAwMDA2MDYgMDAwMDAgbiAKMDAwMDAwMDcwNyAwMDAwMCBuIAowMDAwMDAxMDY4IDAwMDAwIG4g
CjAwMDAwMDA2NDcgMDAwMDAgbiAKMDAwMDAwMDY3NyAwMDAwMCBuIAowMDAwMDAxMjg2IDAwMDAw
IG4gCjAwMDAwMDA5MzIgMDAwMDAgbiAKdHJhaWxlcgo8PCAvU2l6ZSAxNCAvUm9vdCAxIDAgUiAv
SW5mbyAyIDAgUgovSUQgWzxEMURDN0E2OUUzN0QzNjI1MDUyMEFFMjU0MTMxNTQwQz48RDFEQzdB
NjlFMzdEMzYyNTA1MjBBRTI1NDEzMTU0MEM+XQo+PgpzdGFydHhyZWYKNDY5MwolJUVPRgo=
--7425195ade9d89bc7492cf520bf9f33a--
$header ends without a line break

I use the code below to send a message with an attachment as well as html and text message parts. I remember fighting with it a long time to get it right, but unfortunately don't remember which parts were my problems. Maybe looking at it will help you out. I've changed some irrelevant variables and hopefully made it easier to read. Feel free to ask if something is weird or unclear and I'll try to explain.
public function sendEmail($htmlString)
{
$random_hash = md5(date('r', time()));
$message = "--mixed-$random_hash\n";
$message .= 'Content-Type: multipart/alternative; boundary="alt-' . $random_hash . '"' . "\n\n";
$message .= 'MIME-Version: 1.0' . "\n";
$message .= '--alt-' . $random_hash . "\n";
$message .= 'Content-Type: text/plain; charset="iso-8859-1"' . "\n";
$message .= 'Content-Transfer-Encoding: 7bit' . "\n\n";
// text message
$message .= strip_tags($htmlString) . "\n\n";
$message .= '--alt-' . $random_hash . "\n";
$message .= 'Content-Type: text/html; charset="iso-8859-1"' . "\n";
$message .= 'Content-Transfer-Encoding: 7bit' . "\n\n";
// html message
$message .= $htmlString . "\n\n";
$message .= '--alt-' . $random_hash . '--' . "\n";
// graph image
if($this->selectedGraph != 0)
{
$graphString = $this->getGraph(); // image attachment
$graphString = chunk_split(base64_encode($graphString));
$linkID = 'graph-' . $userInfo['FirmID'] . $random_hash . '-image';
$message .= '--mixed-' . $random_hash . "\n";
$message .= 'MIME-Version: 1.0' . "\n";
$message .= 'Content-Transfer-Encoding: base64' . "\n";
$message .= 'Content-ID: ' . $linkID . "\n";
$message .= 'Content-Type: image/gif; name="graph.gif"' . "\n";
$message .= 'Content-Disposition: attachment' . "\n\n";
$message .= $graphString;
$message .= '--mixed-' . $random_hash . '--' . "\n";
}
else
{
$message .= '--mixed-' . $random_hash . '--' . "\n";
}
$headers = 'From: ' . $this->from. "\r\nReply-To: " . $this->replyto;
$headers .= "\r\nContent-Type: multipart/related; boundary=\"mixed-" . $random_hash . "\"\r\nMIME-Version: 1.0";
$flags = '-f ' . BOUNCED_EMAIL_ADDRESS;
return mail($userInfo['Email'], $this->subject, $message, $headers, $flags);
}

These things happen when building mime mails from scratch. There are a couple of php modules you can use to generate cool and compatible mime messages. Mail_Mime should do the trick.

Related

Send Email with attachment by using URL in Codeigniter

I would like to send an email with attachment.
I have no problem to send an email by using file path, but I prefer to set my attachment by using URL.
The email can sent successfully to my email without any attachment.
My PDF URL example: http://store/index.php/Store/GI/OrderID.pdf
Hope some can help me. Really Appreciate. Thank You.
Controller::
public function SendInvoice(){
$orderid = $this->uri->segment(3);
$result = $this->order->GetCustOrder($orderid);
unset($data);
$data = array(
$OrderID,
$result[0]->cust_id,
$result[0]->cust_name,
$result[0]->cust_email
);
$this->store_email->SendInvoiceToCust($data); //library
$this->session->set_flashdata('sent_email','Email has been sent to customer.');
redirect('Store/Index');
}
Library::
public function SendInvoiceToCust($data){
$message = "Dear Valued Customer, <br><br>Thank you for Purchased at Store. <br/><br/>Kindly refer the attachment to view your Invoice.";
//$attached_file = base_url(). 'index.php/Store/GI/' .$data[0]. '.pdf';
$form = base_url(). 'index.php/Store/GI/' .$data[0]. '.pdf';
$attachment = chunk_split(base64_encode($form));
$separator = md5(time());
$message .= "--" . $separator . PHP_EOL;
$message .= "Content-Type: application/octet-stream; name=\"\" . $data[0]. '.pdf'\"" . PHP_EOL;
$message .= "Content-Transfer-Encoding: base64" . PHP_EOL;
$message .= "Content-Disposition: attachment" . PHP_EOL . PHP_EOL;
$message .= $attachment . PHP_EOL . PHP_EOL;
$message .= "--" . $separator . "--";
$this->email->from('email#gmail.com', 'Store');
$this->email->to('email2#gmail.com');
$this->email->subject('Store INVOICE [Do not reply]');
$this->email->message($message);
$this->email->attach($attachment);
$this->email->send();
}
I cant comment yet but you should try setting content type and encoding the pdf.
The below is what I am currently using to send out an email with an attachement in codeigniter.
I have updated the response. This is currently how I am taking a pdf form and attaching it.
Its all done within the $message field with different content transfer and types being set.
$sender = "email#gmail.com";
$emails = "email2#gmail.com";
$subject = "Store INVOICE [Do not reply]";
$headers = "From: " . $sender . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$message = "";
$message = "--" . $separator . PHP_EOL;
$message .= "Content-Type: text/html; charset=\"iso-8859-1\"" . PHP_EOL;
$message .= "Content-Transfer-Encoding: 8bit" . PHP_EOL . PHP_EOL;
$message .= ($content = "Dear Valued Customer, <br><br>Thank you for Purchased at Store. <br/><br/>Kindly refer the attachment to view your Invoice.");
$message .= PHP_EOL . PHP_EOL;
$form = base_url(). 'index.php/Store/GI/' .$data[0]. '.pdf';
$attachment = chunk_split(base64_encode($form));
$separator = md5(time());
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: 7bit";
$message .= "--" . $separator . PHP_EOL;
$message .= "Content-Type: application/octet-stream; name=\"" . $data[0]. '.pdf'\"" . PHP_EOL;
$message .= "Content-Transfer-Encoding: base64" . PHP_EOL;
$message .= "Content-Disposition: attachment" . PHP_EOL . PHP_EOL;
$message .= $attachment . PHP_EOL . PHP_EOL;
$message .= "--" . $separator . "--";
$result = mail($emails, $subject, $message, $headers);

HTML/PHP mail() : Content Not Showing in Email with Attachment sent Through PHP

The issue: Emails sent with HTML and attachment do not show content. There is no issue with the attachment. Before I attempted to send it as an HTML email, the message and attachment were fine.
The code: I am using the following code to send emails with attachments:
//Email to Customer or Tech
$file = "../PDF/phptopdf/".$_GET['pdf'];
$lgth = strlen($_GET['pdf']);
$intsta = strpos($_GET['pdf'],'/')+1;
$intend = $lgth - strpos($_GET['pdf'],'.');
$intendname = $lgth - strpos($_GET['pdf'],'_');
$filename = substr($_GET['pdf'],$intsta,-$intend);
$content = file_get_contents($file);
$content = chunk_split(base64_encode($content));
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (RFC)
$eol = "\r\n";
// main header (multipart mandatory)
$headers = "From: ".$_SESSION['userFName']." ".$_SESSION['userLName']." <".$_SESSION['email'].">" . $eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: 8bit" . $eol;
// message
$body = "--" . $separator . $eol;
$body .= "Content-Type: text/html; charset=iso-8859-1" . $eol;
$body .= "Content-Transfer-Encoding: quoted-printable" . $eol;
if ($_POST['sendToTech'] == '1') {
$mailto = $_POST['techEmail'];
$subject = 'New Work Order for '.$compName;
$pos = strpos($_POST['techName'],' ');
$techFName = substr($_POST['techName'],0,$pos);
$message = "<html><body>";
$message .= "<table style='border:1px solid #000;font-size:18px;line-height:20px;'><tr><td style='text-align:center;padding:10px;'><img src='MY LOGO URL' /></tr></td>";
$message .= "<tr><td style='padding:10px;'>";
$message .= "Hello ".$techFName.", <br/><br/>";
$message .= "I have assigned work order #00-".$_GET['idQ']." for ".$compName." to you. <br/>";
$message .= "The work is scheduled for ".date('F jS, Y \a\t g:i A',strtotime($serviceDate))." at ".$address.". <br/>";
$message .= "Click on the attachment to view the work order or <a href='MYURL".$_GET['pdf']."'>Click Here</a>. <br/><br/>";
$message .= "Thank you, <br/>";
$message .= $_SESSION['userFName']."<br/><br/></tr></td></table></body></html>";
$body .= $message . $eol;
// attachment
$body .= "--" . $separator . $eol;
$body .= "Content-Type: application/pdf; name=\"" . $filename . "\"" . $eol;
$body .= "Content-Transfer-Encoding: base64" . $eol;
$body .= "Content-Disposition: attachment" . $eol;
$body .= $content . $eol;
$body .= "--" . $separator . "--";
//SEND Mail
if (mail($mailto, $subject, $body, $headers)) {
echo "<span class='message mg'>Work Order #00-".$idQ." has been emailed to ".$_POST['techName']." at ".$_POST['techEmail']."</span><br/>";
} else {
$msg[] = "Error: " . $mysqli->error;
echo "<span class='message alert'>An error occured while attempting to send Work Order #00-".$idQ." to ".$_POST['techEmail']."</span>";
}
}
When the email arrives it looks like this:
Email in Gmail
But, when I look into the original message I see the content that is not displayed:
Original Message View
Solutions that I have attempted:
Removing "This is a multi-part message in MIME format" (PHP mail function html content not visible in email)
Changing $body .= "Content-Type: text/html; charset=iso-8859-1" . $eol; -- to text/plain (just to see if that would do anything...)
Changing $headers .= "Content-Transfer-Encoding: 8bit" . $eol; -- from 7bit and quoted-printable
Removing <html><body> and closing tags

php mail attachment - error - how to debug

I search and try different methods to produce a php mail with an attachment, which is saveable with the Client (Outlook). It doesn't work porper and I don't know why?
If the script send the mail with this code:
$result = mail($mail_to, $subject, $headers);
I got an email with an inline base64 coded attachment and if use it in this way:
$result = mail($mail_to, $subject, "", $headers);
or
$result = mail($mail_to, $subject, $text, $headers);
I got the PHP Error: "false" - the mail could not send.
So it isn't and Mailserverproblem, I think and from headersview I think, it should work and I get no mail error on the Linux site.
A part from the mail coming with the attachment inline:
MIME-Version: 1.0
From: user#nowhere.xyz
Content-Type: multipart/mixed; boundary="F34A3E8D822EE5A70EEDE9C3C143243C"
--F34A3E8D822EE5A70EEDE9C3C143243C
Content-Type: multipart/alternative; boundary="A0403290562B2A2F19FB62E48C51BE33"
--A0403290562B2A2F19FB62E48C51BE33
Content-Type: text/plain
Content-Transfer-Encoding: 8bit
Hello Mr. Xyz,
Some stupid text as an example.
--A0403290562B2A2F19FB62E48C51BE33--
--F34A3E8D822EE5A70EEDE9C3C143243C
Content-Type: application/CSV; charset="ISO-8859-1"; name="trouble.csv"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="trouble.csv"
...
YWNodW5nIG5pY2h0IGVyZm9yZGVybGljaCAvIG3DtmdsaWNoIjsiIjsiIjsK
--F34A3E8D822EE5A70EEDE9C3C143243C--
The matching PHP code:
$text = "Hello Mr. Xyz," . $nl . "Some stupid text as an example." . $nl;
$content = chunk_split(base64_encode($file));
$headers = "MIME-Version: 1.0" . $nl;
$headers .= "From: " . $mail_from . $nl;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $boundary1 . "\"" . $nl . $nl;
$headers .= "--" . $boundary1 . $nl;
$headers .= "Content-Type: multipart/alternative; boundary=\"" . $boundary2 . "\"" . $nl . $nl;
$headers .= "--" . $boundary2 . $nl;
$headers .= "Content-Type: text/plain" . $nl;
$headers .= "Content-Transfer-Encoding: 8bit" . $nl . $nl;
$headers .= $text . $nl;
$headers .= "--" . $boundary2 . "--" . $nl . $nl;
$headers .= "--" . $boundary1 . $nl . $nl;
$headers .= "Content-Type: application/excel; charset=\"ISO-8859-1\"; name=\"xyz.csv\"" . $nl;
$headers .= "Content-Transfer-Encoding: base64" . $nl;
$headers .= "Content-Disposition: attachment; filename=\"xyz.csv\"" . $nl . $nl;
$headers .= $content . $nl;
$headers .= "--" . $boundary1 . "--" . $nl;
So, where is the bug? How can I debug such a Situation?
Thx in advance...
This one works....
$text = "Hello Mr. Xyz," . $crnl . "Some stupid text as an example." . $crnl;
$content = chunk_split(base64_encode($file));
$headers = "MIME-Version: 1.0" . $crnl;
$headers .= "From: " . $mail_from . $crnl;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $boundary1 . "\"" . $nl . $crnl;
$headers .= "Content-Type: text/plain" . $nl;
$headers .= "--" . $boundary1 . $crnl;
$headers .= 'Content-Length: '.strlen($text) . $crnl;
$headers .= "Content-Transfer-Encoding: 8-bit" . $nl . $crnl;
$headers .= $text;
$headers .= "--" . $boundary1 . $crnl;
$headers .= "Content-Type: application/excel; charset=\"ISO8859-1\"; name=\"xyz.csv\"" . $nl . $crnl;
$headers .= "--" . $boundary1 . $crnl;
$headers .= "Content-Disposition: attachment; filename=\"xyz.csv\"" . $crnl;
$headers .= 'Content-Length: ' . strlen($content) . $crnl;
$headers .= "Content-Transfer-Encoding: base64" . $nl . $crnl;
$headers .= $content;
$headers .= "--" . $boundary1 . "--"
...with a small bug. I have an extra attment, but that's for now ok. We other thing is a longer play with "\n", "\r\n" and a mixture of both on the right places (lines).

Plain text version of email is not working

So I have the script sending a multipart email of plain text and html, some reason the plaintext version is not working, but I can see that it is sending when I show original in gmail. It just shows the html though when I click message garbled?
// headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= 'From: From Name <'. FROM_EMAIL. ">\r\n";
$headers .= 'To: '.$member->member_info['memem']. "\r\n";
$headers .= 'Bcc: receipts#gmail.com' . "\r\n";
$headers .= "Subject: Registration Receipt\r\n";
$headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n";
// Additional headers
$message . "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-type: text/plain;charset=utf-8\r\n\r\n";
$message .= $receipt_txt;//plain text
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-type: text/html;charset=utf-8\r\n\r\n";
$message .= $receipt_html;//html
$message .= "\r\n\r\n--" . $boundary . "--";
mail( $member->member_info['memem'], 'Registration Receipt', $message, $headers );
Crap. I found it. "$message . "\r\n\r\n--" . $boundary . "\r\n";" should be "$message .= "\r\n\r\n--" . $boundary . "\r\n";" I feel dumb :(

Send CSV file attached to the email

I have an issue: i need to create csv file and send it attached to the particular emails.
Honestly i never do that so i'm quite a lammer in this matter. Can anybody tell me with what i have to start or share some links?
I think this what you are looking for, i've used it in the past works perfectly.
Hope it helps.
<?php
$cr = "\n";
$csvdata = "First Name" . ',' . "Last Name" . $cr;
$csvdata .= $txtFName . ',' . $txtLName . $cr;
$thisfile = 'file.csv';
$encoded = chunk_split(base64_encode($csvdata));
// create the email and send it off
$subject = "File you requested from RRWH.com";
$from = "scripts#rrwh.com";
$headers = 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-Type: multipart/mixed;
boundary="----=_NextPart_001_0011_1234ABCD.4321FDAC"' . "\n";
$message = '
This is a multi-part message in MIME format.
------=_NextPart_001_0011_1234ABCD.4321FDAC
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit
Hello
We have attached for you the PHP script that you requested from http://rrwh.com/scripts.php
as a zip file.
Regards
------=_NextPart_001_0011_1234ABCD.4321FDAC
Content-Type: application/octet-stream; name="';
$message .= "$thisfile";
$message .= '"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="';
$message .= "$thisfile";
$message .= '"
';
$message .= "$encoded";
$message .= '
------=_NextPart_001_0011_1234ABCD.4321FDAC--
';
// now send the email
mail($email, $subject, $message, $headers, "-f$from");
?>
Kind regards,
Wesley.

Categories