Sending multipart text and html email in Fat Free Framework - multipart

Currently I'm using the following code to send my e-mails in Fat Free Framework:
$smtp = new SMTP ( $f3->get('MAILHOST'), $f3->get('MAILPORT'), $f3->get('MAILPROTOCOL'), $f3->get('MAILUSER'), $f3->get('MAILPW') );
$smtp->set('Content-type', 'text/html; charset=UTF-8');
$smtp->set('Errors-to', '<$my_mail_address>');
$smtp->set('To', "<$my_mail_address>");
$smtp->set('From', '"$my_mailer_name" <$my_mail_address>');
$smtp->set('Subject', "$subject");
$smtp->set('Date', date("r"));
$smtp->set('Message-Id',generateMessageID());
$smtp->send(Template::instance()->render('emails/'.$mailTemplate.'.html'));
And it works like a charm. But I would like to add a text version to this e-mail.
Is there a way to do this within the Fat Free Framework smtp plugin?
If so, how should I do this?
And if not, how else should I do this in F3?

Actually it can send a multiplart text + html mail. The SMTP class is just a protocol implementation, so it might feel a little bit bare-bone at this point. You basically need to prepare your mail body with the multipart like this:
$text = 'Hello world.';
$html = 'Hello <b>world</b>';
$smtp = new \SMTP();
$hash=uniqid(NULL,TRUE);
$smtp->set('From', 'info#domain.com');
$smtp->set('To', 'info#domain.com');
$smtp->set('Content-Type', 'multipart/alternative; boundary="'.$hash.'"');
$smtp->set('Subject', 'Multipart test');
$eol="\r\n";
$body = '--'.$hash.$eol;
$body .= 'Content-Type: text/plain; charset=UTF-8'.$eol;
$body .= $text.$eol.$eol;
$body .= '--'.$hash.$eol;
$body .= 'Content-Type: text/html; charset=UTF-8'.$eol.$eol;
$body .= $html.$eol;
$smtp->send($body,true);

Related

Wordpress smtp mail from address is not working

i am using wp_mail_smtp for sending mail in WordPress and its working fine expect one thing i have define the from email : abc#mysite.com and from name: my site and username password for smtp authentication is : someemail#example.com and password of my mail id.
now problem is that when i sending the mail than i receive mail header like
mysite< someemail#example.com > and it should be mysite< abc#mysite.com > i have also set the filter for from name and from mail like below code
add_filter('wp_mail_from', 'new_mail_from');
add_filter('wp_mail_from_name', 'new_mail_from_name');
function new_mail_from($old) {
return 'abc#mysite.com';
}
function new_mail_from_name($old) {
return 'mysite.com';
}
and my mail code is
$headers = 'From: abc#mysite.com ' . "\r\n";
$to = "admin#test.com"
$send_subject = "Mail Subject"
$message .= "Some message";
wp_mail( $to, $send_subject, $message,$headers );
please tell me what i am doing wrong so that i can receive mail header correctly
Thanks

HTTPful attach file and json-body in one request

I need to upload files via Rest and also send some configuration with it.
Here is my example code:
$this->login();
$files = array('file'=>'aTest1.jpg');
$data =
array(
'name'=>'first file',
'description'=>'first file description',
'author'=>'test user'
);
$response = Request::post($this->getRoute('test'))
->addHeader('Authorization', "Bearer " . $this->getToken())
->attach($files)
->body(json_encode($data))
->sendsJson()
->send();
I am able to send the file or able to send the body. But it is not working if I try with both...
Any Hint for me?
Regards
n00n
For those coming to this page via Google. Here's an approach that worked for me.
Don't use attach() and body() together. I found that one will clear out the other. Instead, just use the body() method. Use file_get_contents() to get binary data for your file, then base64_encode() that data and place it into the $data as a parameter.
It should work with JSON. The approach worked for me with application/x-www-form-urlencoded mime type, using $req->body(http_build_query($data));.
$this->login();
$filepath = 'aTest1.jpg';
$data =
array(
'name'=>'first file',
'description'=>'first file description',
'author'=>'test user'
);
$req = Request::post($this->getRoute('test'))
->addHeader('Authorization', "Bearer " . $this->getToken());
if (!empty($filepath) && file_exists($filepath)) {
$filedata = file_get_contents($filepath);
$data['file'] = base64_encode($filedata);
}
$response = $req
->body(json_encode($data))
->sendsJson();
->send();
the body() method erases payload content, so after calling attach(), you must fill payload yourself :
$request = Request::post($this->getRoute('test'))
->addHeader('Authorization', "Bearer " . $this->getToken())
->attach($files);
foreach ($parameters as $key => $value) {
$request->payload[$key] = $value;
}
$response = $request
->sendsJson();
->send();

Create a plain HTTP PUT multipart request with a jpg attached

I'm creating a HTTP PUT request manualy. I have the following format
POST http://server.com/id/55/push HTTP/1.0
Content-type: multipart/form-data, boundary=AaB03x
Content-Length: 168
--AaB03x
Content-Disposition: form-data; name="image"; filename="small.jpg"
Content-Type: image/jpeg
Content-Transfer-Encoding: binary
<file content>
--AaB03x--
My question is, how should I fill the "file content" area? If I open a jpeg with TexMate or with cat command line application and I paste the ASCII output, the request does not work.
UPDATE
I'm working with a microprocessor and I can't use C or a high level language I need to manually do the raw request. Do I need to separate with spaces every binary byte read from the file?
In case of saving the jpg into a file in the server side, Do I have to convert the binary stream to ASCII?
I read the binary code of a JPG from my hard drive with a simple php conde:
$filename = "pic.jpg";
$handle = fopen($filename, "rb");
$fsize = filesize($filename);
$contents = fread($handle, filesize($filename));
fclose($handle);
//echo $contents;
for($i = 0; $i < $fsize; $i++)
{
// get the current ASCII character representation of the current byte
$asciiCharacter = $contents[$i];
// get the base 10 value of the current characer
$base10value = ord($asciiCharacter);
// now convert that byte from base 10 to base 2 (i.e 01001010...)
$base2representation = base_convert($base10value, 10, 2);
// print the 0s and 1s
echo($base2representation);
}
whit this code I get a stream of 1 and 0. I can send it including the string of 101010101... to where the tag "file content" of my manually http request is but in the server side I can't visualise the JPG... ¿should I convert it to ASCII again?
SOLUTION
Okay the solution was very simple, I just dumped the ASCII code into the tag "file content" of the http request. Despite I'm using a micro controller I opened a socket with PHP and tested out. The solution was to read the ASCII from the file instead of paste directly the ASCII into the code.
Here a working example of the solution:
<?php
//We read the file from the hard drive
$filename = "pic.jpg";
$handle = fopen($filename, "rb");
$fsize = filesize($filename);
$contents = fread($handle, filesize($filename));
fclose($handle);
$mesage = $contents;
//A trick to calculate the length of the HTTP body
$len = strlen('--AaB03x
Content-Disposition: form-data; name="image"; filename="small.jpg"
Content-Type: image/jpeg
Content-Transfer-Encoding: binary
'.$mesage.'
--AaB03x--');
//We create the HTTP request
$out = "POST /temp/test.php HTTP/1.0\r\n";
$out .= "Content-type: multipart/form-data boundary=AaB03x\r\n";
$out .= "Content-Length: $len\r\n\r\n";
$out .= "--AaB03x\r\n";
$out .= "Content-Disposition: form-data; name=\"image\"; filename=\"small.jpg\"\r\n";
$out .= "Content-Type: image/jpeg\r\n";
$out .= "Content-Transfer-Encoding: binary\r\n\r\n";
$out .= "$mesage\r\n";
$out .= "--AaB03x--\r\n\r\n";
//Open the socket
$fp = fsockopen("127.0.0.1", 8888, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
//we send the message thought the opened socket
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
//Visualize the query sent
echo nl2br($out);
?>
In the real implementation I will simple read directly from the memory of the microcontroler just as I did in the php
You are mistyping the last boundary, it should have been:
--AaB03x--
You need to have an OutputStream of your outcoming connection and use this Stream to write ALL BYTES that you have read from the file.
If you used C#. You can check this: Sending Files using HTTP POST in c#
For Java:
Image writing over URLConnection
how to send data with file upload to the server
HttpURLConnection POST, conn.getOutputStream() throwing Exception

sending a mail with Drupal's user_save

I have a website that has to insert users in a Drupal database. I include the Drupal bootstrap file and call the user_save() function.
The paramenters I pass to user_save are: the first one a stdClass with property 'status'=1, and the second parameter is (what's expected to be sent, because the insert works just fine).
The problem I'm having is that the user receives no confirmation email. I think user_save should send the user an email, but it doesn't. Maybe I'm missing something here, so your help is much appreciated!
The second paramater should be an array:
user_save($account, array('status' => 1));
You can always look at http://api.drupal.org/api/drupal/modules%21user%21user.module/function/user_save/6 to understand the user_save function in detail..
Just to make sure, you have checked the settings for sending email on registration?
admin/config/people/accounts
Sometimes there are problem with email on crappy servers/some hosts are limiting the php mail function. Here is a simple php mail call you could try if you're not sure if the mail-function runs without problem on your server.
<?php
$to = 'youremail#yourhost.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: youremail#yourhost.com' . "\r\n" . 'Reply-To: youremail#yourhost.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>

Wordpress Php auto email to comment author

I'd love to be able to automatically send a response to the person who comments on a post on my site. Their email is required so I feel as though I should be able to grab that and use php to send an email back to that email address...
I know the basics for a php email go as follows... So I just need help grabbing the authors email and putting it into the mailTo variable
<?php
$subject = 'My subject';
$message = "The Message I'd like to send back to the commenter";
$mailTo = get_comment_author_email_link
mail($mailTo, $subject, $message);
?>
Thanks!
I think what you need is to hook to the comment post action with your defined own function as such:
<?php
function sendMail($id){
$subject = 'My subject';
$message = "The Message I'd like to send back to the commenter";
$comment=get_comment($id);
$mailTo = $comment->comment_author_email ;
mail($mailTo, $subject, $message);
}
add_action('comment_post', 'sendMail');
?>
you can use this , but dont forget the comment of webarto :
http://wordpress.org/extend/plugins/wp-comment-auto-responder/

Resources