Email header with custom CC in format: "Name <email>" in php - php

Question:
How do I get an email header to have a CC with the format: "name <email>"? (I have not found much to help with this when its not the TO or FROM address)
Summary of Issue:
An email CC field in the format: "name lastname " produces a header like: "name#emaildomain; lastname#emaildomain; email#emaildomain;" rather then the expected format.
Generating the header
The cc field is populated on a php form with fields for Subject, To, CC, and Body text POSTS to an email handling class. Inside the handler class, the From, BCC, and CC field are written into a header string using an addHeader() method.
function addHeader($type,$value)
{
$this->header .= $type.":".$value.$this->eol;//eol = PHP_EOL
}
When the mail is actually sent it uses a send() method (that I sadly dont feel comfortable detailing too extensively) but the essential point is that it creates the full header string that looks much like this:
From: Company Name CC:Long Last Name, First Name <longlastname#company.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="==Multipart_Boundary_xb964d9146ea853337b28e6b471a8ca00x"
Which is added to a php mail():
$result = mail($this->to, $this->Subject , $this->Body, $this->header);
What I have tried:
I have tried MIME encoding the header but it doesn't seem to help.
From: =?UTF-8?Q?Company=20Name?= Cc: =?UTF-8?Q?Long=20Last=20Name,=20First=20Name?= <longlastname#company.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="==Multipart_Boundary_x4cfee11b9185861f4ba890079305ecb0x"
I also tried replacing the white space characters with alternatives like &nbsp or (space) but it did not solve the issue either.

Related

PHP imap_fetchbody can't get HTML a tag link url

I'm building a email read script, it read emails via imap, when i try to read email body using imap_fetchbody it only show texts, but email body have one link it seems not show,
This is my code
$hostname = '{mail.test.com/notls}INBOX';
$username = 'info#test.com';
$password = 'testopw';
/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Email Server: ' . imap_last_error());
/* grab emails */
$emails = imap_search($inbox,'SEEN FROM "noreply#test.com"');
if($emails) {
/* put the newest emails on top */
rsort($emails);
/* for every email... */
foreach($emails as $email_number) {
/* get information specific to this email */
echo $message = imap_fetchbody($inbox,$email_number,1);
}
}
The email contents is like below
We do our utmost to arrive to you on time. There may, however,
be unexpected circumstances, causing our driver to be delayed.
You can follow the current status of your assignment here.
When i read the email from mail client text here has a link, but when read that using php it only shows a text,
Email screen shot here
someone know what is the reason for this issue or is there need to change some parameters for get the link with url. Thank You
You have to check the multiple sections of the email. You can actually read the source code of an email. It is quite easy to follow and you will identify the different sections.
imap_fetchbody($inbox,$email_number,$section);
http://php.net/manual/en/function.imap-fetchbody.php
(empty) - Entire message
0 - Message header
1 - MULTIPART/ALTERNATIVE
1.1 - TEXT/PLAIN
1.2 - TEXT/HTML
2 - file.ext
The section you mention on the comments it is a PNG file. Some sections can be encoded in base64 as the email it is a text based protocol.
https://www.base64decode.org/
Example of an email:
From: Nathaniel Borenstein <nsb#bellcore.com>
To: Ned Freed <ned#innosoft.com>
Subject: Sample message
MIME-Version: 1.0
Content-type: multipart/mixed; boundary="simple
boundary"
This is the preamble. It is to be ignored, though it
is a handy place for mail composers to include an
explanatory note to non-MIME compliant readers.
--simple boundary
This is implicitly typed plain ASCII text.
It does NOT end with a linebreak.
--simple boundary
Content-type: text/plain; charset=us-ascii
This is explicitly typed plain ASCII text.
It DOES end with a linebreak.
--simple boundary--
This is the epilogue. It is also to be ignored.
https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html

imap_fetchbody can't handle mails sent with phpmailer?

I am sending some emails with phpmailer with the regular example on their website.
the email is sent, and it arrives perfectly to the sent address.
after the mail is sent, I am saving the email to my sent items folder with imap_append, using the info of this answer Sent mails with phpmailer don't go to "Sent" IMAP folder
and in third place, I am creating a custom made webmail which reads my imap folders.
the problem is that I can't get the html body of the email sent correctly.
When I open the mail on sent items on thunderbird, or my phone, the body is shown perfectly.
but when I try to get the html body with php_imap functions I can't make it!!.
I created an isolated file to fetch the body of the msg like this:
$body = imap_fetchbody($conn,3392,1,FT_UID);
that returns this:
--b1_9db4e4310d1b141cbec79dd7de22f70b Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit á é í ñ Ñ REMOVED CONTENT --------------------- klfasjdkl adj skldj klfdj akfdsj lkfdasjfkl dasjf --b1_9db4e4310d1b141cbec79dd7de22f70b Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit á é í ñ Ñ
REMOVED CONTENT
---------------------
klfasjdkl adj skldj klfdj akfdsj lkfdasjfkl dasjf --b1_9db4e4310d1b141cbec79dd7de22f70b--
If I try
$body = imap_fetchbody($conn,3392,1.2,FT_UID);
don't get anything
If I try
$body = imap_fetchbody($conn,3392,2,FT_UID);
don't get anything, it seems that PHPMailer creating the emailbody in a way that imap_fetchbody can't handle?
thank you

Remove headers from Email Body

I am having trouble with email synchronization application, i am developing an conversation module for my project so i had extracted email body and attachments from the Email Account but problem is i also got some header text in email body like
"--PHP-alt-9ce0b24f8d52c4d37de61cb315107795d140f8b7 Content-Type: text/plain --PHP-alt-9ce0b24f8d52c4d37de61cb315107795d140f8b7 "
and i want to remove it
actual output is
--PHP-alt-9ce0b24f8d52c4d37de61cb315107795d140f8b7 Content-Type: text/plain --PHP-alt-9ce0b24f8d52c4d37de61cb315107795d140f8b7 Content-Type: multipart/related; boundary="PHP-related-9ce0b24f8d52c4d37de61cb315107795d140f8b7" --PHP-related-9ce0b24f8d52c4d37de61cb315107795d140f8b7 Content-Type: text/html Dear Milind
Thank you for choosing XXXXX for your limo hire.
Please accept this e-mail as confirmation the following booking has been
confirmed and requires you to print, sign and return the attached
contract with us, either by e-mail, fax or post, within 3 days.
To view contract for booking please click here
We would like to draw your attention to our agreed Terms and
Conditions concerning the booking and ask that you ensure you comply,
which can be found on our website www.xxxxx.co.uk
If this has been received in error, please contact our Office
immediately on XXXXXXX or XXXXXX.
Regards
Company name
TEL:
and i want something like
Dear Milind
Thank you for choosing XXXXX for your limo hire.
Please accept this e-mail as confirmation the following booking has been
confirmed and requires you to print, sign and return the attached
contract with us, either by e-mail, fax or post, within 3 days.
To view contract for booking please click here
We would like to draw your attention to our agreed Terms and
Conditions concerning the booking and ask that you ensure you comply,
which can be found on our website www.xxxxx.co.uk
If this has been received in error, please contact our Office
immediately on XXXXXXX or XXXXXX.
Regards
Company name
TEL:
i have tried substr() but its not that helpful
any regular expression may be?
here is code for email sending
lets assume body string = $email_message where all email format is written in "string"
PS: please note that some emails contains attachments
$subject = 'A sample email - Dual Format plus attachment plus inline';
// Create a boundary string. It needs to be unique (not in the text) so ...
// We are going to use the sha1 algorithm to generate a 40 character string:
$sep = sha1(date('r', time()));
// Define the headers we want passed.
$headers = "From: StarLimosine <sender#gmail.com> \r\n X-Mailer: Custom PHP Script";
//$headers.="\r\nCc:sender#gmail.com";
$headers.="\r\nBcc:sender#gmail.com , sender#gmail.in";
// Add in our primary content boundary, and mime type specification:
$headers .=
"\r\nContent-Type: text/html; boundary=\"PHP-mixed-{$sep}\"";
// Prepare our attachment file - Read it in, encode it and split it
//$attached = chunk_split(base64_encode(file_get_contents('attachment.zip')));
// Also now prepare our inline image - Also read, encode, split:
$inline = chunk_split(base64_encode(file_get_contents('images/Logo.png')));
// Now the body of the message.
$body =<<<EOBODY
--PHP-mixed-{$sep}
Content-Type: multipart/alternative; boundary="PHP-alt-{$sep}"
--PHP-alt-{$sep}
Content-Type: text/plain
--PHP-alt-{$sep}
Content-Type: multipart/related; boundary="PHP-related-{$sep}"
--PHP-related-{$sep}
Content-Type: text/html
$email_message
<strong>
Star Limousines<br/>
TEL: 0800 9 556 556 <br/>
or 01435 813494</strong><br/>
<span style="font-size:10px">Registerd Office Pick Hill Farm, Horam East Sussex TN21 0JR</span>
--PHP-mixed-{$sep}--
EOBODY;
$body=$email_message;
mail($to,$subject,$body);
At any point of time, the headers are in the first two lines right? If that's the case, you can go with this:
substr($email, strpos($email, PHP_EOL, strpos($email, PHP_EOL) + 1));
add this only when you POST data when using GET remove this.
$headers .=
"\r\nContent-Type: text/html; boundary=\"PHP-mixed-{$sep}\"";
you will get response without headers

why i receive the body content mail duplicated when i use php://stdin?

I have a postfix server redirecting all incomming mail into a php script
but the body contents of the mail appears duplicated, looks:
--000e0ce03cf6d06b0c04c67595d4
Content-Type: text/plain; charset=ISO-8859-1
why
show
this
duplicated?
--000e0ce03cf6d06b0c04c67595d4
Content-Type: text/html; charset=ISO-8859-1
why<div>show</div><div>this</div><div>duplicated?</div>
--000e0ce03cf6d06b0c04c67595d4--
i have sended this mail using gmail with this body content:
why
show
this
duplicated?
why appears the mail two times? (and one in html :S)
how can i get only one?
thanks!
Assuming your code returns the same sort of separator, one method might be to do the following:
// Get separator
$mail_pieces = explode("\n", $mail);
$separator = $mail_pieces[0];
// Separate email
$mail_pieces = explode($separator, $mail);
$text_mail = $mail[0];
$html_mail = $mail[1];

=?UTF-8?B??= in Emails sent via php mail problem

I have a website, and in the "Contact" section I have a form which users may fill in to contact me.
The form is a simple form which action is a php page.
The php code:
$to = "email#domain.com";
$name=$_POST['name']; // sender name
$email=$_POST['email']; // sender email
$tel= $_POST['tel']; // sender tel
$subject=$_POST['subject']; // subject CHOSEN FROM DROPLIST, ALL TESTED
$text=$_POST['text']; // Message from sender
$text.="\n\nTel:".$tel; // Added to message to show me the telephone nr to the sender at bottom of message
$headers="MIME-Version: 1.0"."\n";
$headers.="Content-type: text/plain; charset=UTF-8"."\n";
$headers.="From: $name <$email>"."\n";
mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=', $text, $headers, '-fno-reply#domain.com');
Could somebody please tell me why this works most of the time, but sometimes I receive email whith no text and the subject line showing
=?UTF-8?B??=
I use outlook express, and I have read this System.Net.Mail and =?utf-8?B?XXXXX.... Headers
but it didn't help.
The problem is not in Outlook, because when I log in to the actual mailprogram where I fetch the POP3 emails from, the email looks the same.
When I right click in Outlook and chose "message source" then there is no "From" information.
Ex, a good message should look like this:
Subject: =?UTF-8?B?w5Z2cmlndA==?=
MIME-Version: 1.0
Content-type: text/plain; charset=UTF-8
From: John Doe
However, the ones with problem looks like this:
Subject: =?UTF-8?B??=
MIME-Version: 1.0
Content-type: text/plain; charset=UTF-8
From:
As if the information has been lost somewhere.
You should know also that I have a VPS, which I manage myself.
I use postfix as an emailserver, if thats got anything to do with it.
But then again, why does it work sometimes?
Also another thing that I have noticed is that sometimes special characters are not shown correctly (by both Outlook and the webmail).
For instance, the name "Björkman" in swedish is shown like Björkman, but again, only sometimes.
I hope anybody knows something about this problem, because it is very hard to track down for me atleast.
If you need more input let me know.
Thanks
When looking at PHP mail(), I see another line breaking: shouldn't you use \r\n as line endings?
And that Björkman example looks very much like your mail is not recognized as UTF-8 encoded.

Categories