Recently i came across -- as a header of mail() function in php. I tried to google it out but didn't found any result so i am posting this question. While trying to edit my code i found that the id after these two -- should be unique.My php script to send email with attachement.
<?php
$file = "http://wpbrisk.com/download/email/email/doc1.pdf"; // Specify the Url of the attachment here
$filename = "doc1.pdf"; // Specify the name of the file here
$to="hudixt#gmail.com"; // Enter the mail to which you want to sent the attachement
$your_name = "Hudixt"; // Your name
$from_mail = "info#wpbrisk.com"; // Your email
$subject = "This is a mail with attachment."; // Subject
$message = "<div id='hello'>World</div>"; // message
// Get the content of the file
$content = file_get_contents($file);
$content = base64_encode($content);
$uid = md5(uniqid(time()));
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
mail($mailto, $subject, "", $header);
?>
Can you please tell me what this -- means in header and why it should be unique.
These "--ID--" lines are MIME part boundaries (see https://en.wikipedia.org/wiki/MIME). They are described in https://www.rfc-editor.org/rfc/rfc2046. They are used to separate different parts of a message.
According to RFC#2046 those only must be unique "enough". If you want to encapsulate an email into another one, the boundaries must not infer with each other, i.e. they have to be distinct. So it's a good idea to make them as unique "as possible and needed":
This implies that it is crucial that the composing agent be able
to choose and specify a unique boundary parameter value that does
not contain the boundary parameter value of an enclosing multipart
as a prefix.
(https://www.rfc-editor.org/rfc/rfc2046#section-5.1)
Seems like a MIME boundary to me...
Basically, when you have multiple types of content in an e-mail message, the different parts are separated using an ID. Each part is then preceded by '--' followed by the boundary. From the RFC:
Thus, a typical multipart Content-Type header field might look like this:
Content-Type: multipart/mixed;
boundary=gc0p4Jq0M2Yt08jU534c0p
This indicates that the entity consists of several parts, each itself with a structure that is syntactically identical to an RFC 822 message, except that the header area might be completely empty, and that the parts are each preceded by the line
--gc0p4Jq0M2Yt08jU534c0p
See http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html for more info.
To answer your last question, when you have an e-mail with text and attachment, the whole message will have Content-Type: multipart/mixed, the text will be text/plain, the attachment (suppose a word file) will be application/msword. Each new content-type part will be separated by '--' . $boundary. The boundary is chosen to be unique and different from the other message parts so that it doesn't appear inside either of them - when the application interpreting the e-mail contents finds $boundary it must be safe to assume that's an intended boundary and not just a fluke inside the message.
Hope that helps!
Related
I use Mail::IMAPClient
Have some variables
$body = $imap->body_string($msg);
$header = $imap->message_string($msg);
$body contains body of message, but $header contains header and body of message. I have not found a method in IMAPClient, who get only message header.
I need delete body from $header. Split $body and $header bad variant, because body can be very big. Body and header separates the empty string, but I do not know how to use it.
Please have a look at http://gagravarr.org/code/test-imap-headers.pl
It suggests the Mail::IMAPClient package supports/supported the 'keyword' "ALL" to get all headers at once:
my %headers = %{ $imap->parse_headers( $msg, "ALL" ) };
for my $h ( keys %headers ) {
my #hdrs = #{ $headers{$h} };
print "$h (" . scalar #hdrs . " entries)\n";
foreach (#hdrs) { print "\t$_\n"; }
}
I'm trying to send a raw email with image attachments via AmazonSES using PHP. It works great when I send the email to a gmail account but hotmail accounts are receiving empty attached images. In other words, hotmail seems to recognize that there are attachments and these attachments have the correct name that i specified, it's just that they are always empty, sized 0 bytes. Googling is not helping... Thanks in advance!
$amazonSES = new AmazonSES();
// if (empty($attach)==0) {
// $response = $amazonSES->send_email(AWS_SES_FROM_EMAIL,
// array('ToAddresses' => array($to)),
// array('Subject.Data' => $subject,'Body.Text.Data' => $messagein,)
// );
// } else {
$rstring = 'ajfas90lsjhntlen89y34oi598';
$message= "To: ".$to."\n";
$message.= "From: " . AWS_SES_FROM_EMAIL . "\n";
$message.= "Subject: " . $subject . "\n";
$message.= "MIME-Version: 1.0\n";
$message.= 'Content-Type: multipart/mixed; boundary="ARandomString'.$rstring.'"';
$message.= "\n\n";
$message.= "--ARandomString$rstring\n";
$message.= 'Content-Type: text/plain; charset="utf-8"';
$message.= "\n";
$message.= "Content-Transfer-Encoding: 7bit\n";
$message.= "Content-Disposition: inline\n";
$message.= "\n";
$message.= $messagein;
$message.= "\n\n";
$message.= "--ARandomString$rstring\n";
foreach ($attach as $attachment) {
// $message.= "Content-ID: \<77987_SOME_WEIRD_TOKEN_BUT_UNIQUE_SO_SOMETIMES_A_#domain.com_IS_ADDED\>\n";
$message.= "Content-ID: \<". md5(uniqid(rand(), true)) ."#biomechanico.com\>\n";
$message.= 'Content-Type: application/zip; name="shell.zip"';
$message.= "\n";
$message.= "Content-Transfer-Encoding: base64\n";
$message.= 'Content-Disposition: attachment; filename="' . $attachment["name"] . '"';
$message.= "\n" . base64_encode(file_get_contents($attachment["file"])) . "\n";
$message.= "--ARandomString$rstring\n";
}
$response = $amazonSES->send_raw_email(array(
'Data'=> base64_encode($message)),
array('Source'=>AWS_SES_FROM_EMAIL, 'Destinations'=> $to));
You are producing a malformed message.
Consider using a proper library for generating mail messages, instead of stitching them together from raw strings.
Otherwise, here’s what I notice right away.
The final multipart boundary must be terminated by an extra --, i.e. the last line must be:
--ARandomStringajfas90lsjhntlen89y34oi598--
rather than
--ARandomStringajfas90lsjhntlen89y34oi598
In the attachment parts, you do not have a blank line between the Content-Disposition header line and the body.
Message lines must not be longer than 998 characters, but your Base64-encoded attachment data, however long, is always on a single line.
As far as I understand PHP, your syntax for Content-ID in the attachment parts is wrong, in that it produces Content-ID: \<whatever\> but should produce Content-ID: <whatever>
Lines must be terminated by CR LF (\r\n), but you have LF (\n).
A good way to debug message problems is to take the actual full generated message source ($message) and run it through Message Lint. If the above suggestions do not help, please post the generated message source rather than the PHP code.
For the Internet message format, please refer to RFC 5322. For the multipart message syntax, please refer to RFC 2046.
I am creating a page that sends the response from contact form within mail but reply-to is not working (the variable i am using is having value, but is not added within headers)
here's my code:
<?php
//$uname=$_REQUEST['uname'];
if(isset($_REQUEST['name']))
{
$name=$_REQUEST['name'];
}
if(isset($_REQUEST['email']))
{
$email=$_REQUEST['email'];
}
if(isset($_REQUEST['phone']))
{
$phone=$_REQUEST['phone'];
}
if(isset($_REQUEST['message']))
{
$message=$_REQUEST['message'];
}
// TESTING IF VARIABLES HAVE VALUES
echo "$name $email $phone $message";
// RESULT: TRUE TILL HERE
if($name=="" || $email=="" || $phone=="" || $message=="")
{
header("location:../?inst=invalid");
}
else
{
// ---------------- SEND MAIL FORM ----------------
// send e-mail to ...
$to="mail#example.com";
// Your subject
$subject="$name Contacted you via Contact Form";
// From
$headers = "From: ME <no-reply#example.com>\r\n";
$headers .= 'Reply-To:' . $email . "\r\n";
$headers .= "Return-Path: info#example.com\r\n";
$headers .= "X-Mailer: PHP\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
print $message;
// send email
$sentmail = mail($to,$subject,$message,$headers);
//$sentmail = mail($to1,$subject,$message,$header);
}
// if your email succesfully sent
if($sentmail){
echo "Mail Sent.";
}
else {
header("location:../landing/?status=verification-pending");
}
?>
Now when i checked headers in my gmail, the value for $email doesn't appear in header information, Also no message is received. All I get is a blank message or may be $message is not printing anything like the same case i am facing with reply-to.
please help me a little with this. Thanks in advance.
Check that you have php warnings and notices enabled. When you are echoing out $name, $email, etc you are doing a redirect using headers after that. If your php notices etc aren't turned on, your header redirect will fail due to having already echoed something, and you won't know that you had invalid input. This is part of the reason you shouldn't echo things out during logic, but should store and out put values later.
Actually there was a little mistake with the above code. I mistakenly added = instead of == while comparison.
This line:
if($name=="" || $email="" || $phone="" || $message="")
Should read as:
if($name=="" || $email=="" || $phone=="" || $message=="")
Since = is for equation and not a condition for comparison ==
Fixed it in the Question
So I'm using PERL and Email::MIME to get an email from gmail. Here is my code:
use Net::IMAP::Simple::Gmail;
use Email::Mime;
# Creat the object that will read the emails
$server = 'imap.gmail.com';
$imap = Net::IMAP::Simple::Gmail->new($server);
# User and password
$user = 'username#gmail.com';
$password = 'passowrd';
$imap->login($user => $password);
# Select the INBOX and returns the number of messages
$numberOfMessages = $imap->select('INBOX');
# Now let's go through the messages from the top
for ($i = 1; $i <= $numberOfMessages; $i++)
{
$top = $imap->top($i);
print "top = $top\n";
$email = Email::MIME->new( join '', #{ $imap->top($i) } );
$body = $email->body_str;
print "Body = $body\n";
}#end for i
When I run it, I get the following error:
can't get body as a string for multipart/related; boundary="----=_Part_6796768_17893472.1369009276778"; type="text/html" at /Library/Perl/5.8.8/Email/Mime.pm line 341
Email::MIME::body_str('Email::MIME=HASH(0x87afb4)') called at readPhoneEmailFeed.pl line 37
If I replace
$body = $email->body_str;
with
$body = $email->body;
I get the output:
Body =
(i.e. empty string)
What's going on here? is there a way for me to get the raw body of the message (->body_raw doesn't work either)? I'm okay with parsing out the body using regex
Email::MIME is not the best documented package I have ever seen.
The body and body_str methods only work on a single mime part. Mostly that would be a simple text message. For anything more complex use the parts method to get each mime component which is itself an Email::MIME object. The body and body_str methods should work on that. An html formatted message will generally have two MIME parts: text/plain and text/html.
This isn't exactly what you want but should be enough to show you what is going on.
my #parts = $email->parts;
for my $part (#parts) {
print "type: ", $part->content_type, "\n";
print "body: ", $part->body, "\n";
}
I've searched and searched for something similar, but i think i'm not doing it right. So i will ask a question. This is very basic. My problem is as follows:
I have a multi-page form, consisting of 4 pages + 1 preview page. On the preview page, upon submitting i want the entire form data to be sent to 2 different mail adresses. One standard, and the other one, the one that the user has submitted.
So i have:
<?php
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
?>
<?php
$email_from = 'mail#company.com';
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message".
$to = "yourname#yourwebsite.com, $email /n";
$headers = "From: Company";
mail($to,$email_subject,$email_body,$headers);
?>
How should I insert the submitted $email variable in order for it to work?
Remove the \n
$to = 'yourname#yourwebsite.com,' . $visitor_email;
According to the mail() function documentation the $to parameter will take a comma-separated list of addresses as you have attempted, but should have no line break at the end.
Also, your variable is $visitor_email, rather than $email.
$to = "yourname#yourwebsite.com, $visitor_email";
You might also consider adding the visitor's email as a CC or BCC rather than the TO address. In that case, add it as a CC or BCC header (separated by \r\n), but you need to be cautious to be sure that the address is an email address and contains no line break characters which could be used for header injection.
// The From should be an email address
$headers = "From: company#example.com\r\n";
$headers .= "CC: $visitor_email;