here is the problem, couldn't find much googling, hope somebody here got the answer for this.
My PHP file is sending emails as feedback to me, and it takes 5 arguments,
whenever I send long arguments to my PHP file, it trims the end of argument 5, which is the longest one, how can I fix that?
To be more clear argument 5 is pretty much the email body.
Here is the PHP code:
<?php
include('Mail.php');
$arg1 = $argv[1]; //appName and version
$arg2 = $argv[2]; //ErrorMessage
$arg3 = $argv[3]; //ErrorData
$arg4 = $argv[4]; //ErrorSource
$arg5 = $argv[5]; //ErrorStackTrace
$arg1 = $_GET['arg1'];
$arg2 = $_GET['arg2'];
$arg3 = $_GET['arg3'];
$arg4 = $_GET['arg4'];
$arg5 = $_GET['arg5'];
$subject = $arg1 ;
$errorMessage = $arg2;
$ErrorData = $arg3;
$ErrorSource = $arg4;
$ErrorStackTrace = $arg5;
$recipients = "myemail";
$from = "errorreport#user.com" ;
$headers = array (
'From' => $from,
'To' => $recipients,
'Subject' => $subject,
);
$body = "ErrorMessage: "."\n".$errorMessage."\n"."ErrorData: "."\n".$ErrorData."\n"."ErrorSource: "."\n".$ErrorSource."\n"."ErrorStackTrace: "."\n".$ErrorStackTrace;
$mail_object =& Mail::factory('smtp',
array(
'host' => 'prwebmail',
'auth' => true,
'username' => 'user',
'password' => 'pass', ));
$mail_object->send($recipients, $headers, $body);
?>
I can see your code is referencing $_GET variables, please change your form to use a method of POST
If you are able to, shy away from GET method forms when sending large data.
find:
<form method="get">
replace with:
<form method="post">
then change:
$arg1 = $_GET['arg1'];
to:
$arg1 = $_POST['arg1'];
etc...
Related
How can I set a proper FROM field for the PHP Mandrill library?
In Java, I can use:
from = "Some Description <noreply#domain.com>";
If I try the same in PHP, I get an error:
Validation error: {"message":{"from_email":"The username portion of the email address is invalid (the portion before the #: Some Description
Only emails with FROMs like this get through:
$from = "noreply#domain.com";
In case it matters, here's how I send an email:
$from = "Some Description <noreply#domain.com>";
$message = array("subject" => $aSubject, "from_email" => $from, "html" => $aBody, "to" => $to);
$response = $mandrill->messages->send($message, $async = false, $ip_pool = null, $send_at = null);
If you read the documentation https://mandrillapp.com/api/docs/messages.html it has two parameters in request
from_email and from_name when we pass
$from = "Name <email>",
It is not accepted by mandrill hence it throws error , To pass a name you need to pass from_name with name value.
I am having some trouble implementing the pushwoosh class http://astutech.github.io/PushWooshPHPLibrary/index.html. I have everything set up but i am getting an error with the array from the class.
This is the code i provied to the class:
<?php
require '../core/init.php';
//get values from the clientside
$sendID = $_POST['sendID'];
$memID = $_POST['memID'];
//get sender name
$qry = $users->userdata($memID);
$sendName = $qry['name'];
//get receiving token
$qry2 = $users->getTokenForPush($sendID);
$deviceToken = $qry2['token'];
//i have testet that $deviceToken actually returns the $deviceToken so thats not the problem
//this is the array that the php class requires.
$pushArray = array(
'content' => 'New message from ' . $sendName,
'devices' => $deviceToken,
);
$push->createMessage($pushArray, 'now', null);
?>
And this is the actually code for the createMessage() method
public function createMessage(array $pushes, $sendDate = 'now', $link = null,
$ios_badges = 1)
{
// Get the config settings
$config = $this->config;
// Store the message data
$data = array(
'application' => $config['application'],
'username' => $config['username'],
'password' => $config['password']
);
// Loop through each push and add them to the notifications array
foreach ($pushes as $push) {
$pushData = array(
'send_date' => $sendDate,
'content' => $push['content'],
'ios_badges' => $ios_badges
);
// If a list of devices is specified, add that to the push data
if (array_key_exists('devices', $push)) {
$pushData['devices'] = $push['devices'];
}
// If a link is specified, add that to the push data
if ($link) {
$pushData['link'] = $link;
}
$data['notifications'][] = $pushData;
}
// Send the message
$response = $this->pwCall('createMessage', $data);
// Return a value
return $response;
}
}
Is there a bright mind out there that can tell me whats wrong?
If I understand, your are trying to if your are currently reading the devices sub-array. You should try this:
foreach ($pushes as $key => $push) {
...
// If a list of devices is specified, add that to the push data
if ($key == 'devices') {
$pushData['devices'] = $push['devices'];
}
You iterate over $pushes, which is array('content' => ..., 'devices' => ...). You will first have $key = content, the $key = 'devices'.
It looks like the createMessage function expects an array of messages, but you are passing in one message directly. Try this instead:
$push->createMessage(array($pushArray), 'now', null);
The class is expecting an array of arrays; you are just providing an array.
You could do something like this
//this is the array that the php class requires.
$pushArrayData = array(
'content' => 'New message from ' . $sendName,
'devices' => $deviceToken,
);
$pushArray[] = $pushArrayData
Will you ever want to handle multiple messages? It makes a difference in how I would do it.
I am trying to write a PHP script that will generate a PDF and email it. My PDF generator works perfectly as an independent URL, but for some reason when I try to make the script email the generated PFD, the received file can't be opened. Here is the code:
include_once('Mail.php');
include_once('Mail/mime.php');
$attachment = "cache/form.pdf";
// vvv This line seems to be where the breakdowns is vvv
file_put_contents( $attachment, file_get_contents( "http://www.mydomain.com/generator.php?arg1=$arg1&arg2=$arg2" ) );
$message = new Mail_mime();
$message->setTXTBody( $msg );
$message->setHTMLBody( "<html><body>$msg</body></html>" );
$message->addAttachment( $attachment );
$body = $message->get();
$extraheaders = array( "From" => $from,
"Cc" => $cc,
"Subject" => $sbj );
$mail = Mail::factory("mail");
$headers = $message->headers( $extraheaders );
$to = array( "Jon Doe <jon#mydomain.com>",
"Jane Doe <jane#mydomain.com>" );
$addresses = implode( ",", $to );
if( $mail->send($addresses, $headers, $body) )
echo "<p class=\"success\">Successfully Sent</p>";
else
echo "<p class=\"error\">Message Failed</p>";
unlink( $attachment );
The line that I marked does generate a PDF file in the cache folder, but it will not open, so that seems to be a problem. However, when I try to attach a PDF file already exists, I have the same problem. I have tried $message->addAttachment( $attachment, "Application/pdf" ); as well, and it does not seem to make a difference.
Typically web server directories should have write permissions locked down. This is probably why you are experiencing problems with file_put_contents('cache/form.pdf').
// A working example: you should be able to cut and paste,
// assuming you are on linux.
$attachment = "/var/tmp/Magick++_tutorial.pdf";
file_put_contents($attachment, file_get_contents(
"http://www.imagemagick.org/Magick++/tutorial/Magick++_tutorial.pdf"));
Try changing where you are saving the pdf to a directory that allows write and read permissions to everyone. Also make sure this directory is not on your web server.
Also try changing the following three things
From
$message = new Mail_mime();
To
// you probably don't need this the default is
// $params['eol'] - Type of line end. Default is ""\r\n""
$message = new Mail_mime("\r\n");
From
$extraheaders = array(
"From" => $from,
"Cc" => $cc,
"Subject" => $sbj,
);
To
$extraheaders = array(
"From" => $from,
"Cc" => $cc,
"Subject" => $sbj,
'Content-Type' => 'text/html'
);
From
$message->addAttachment($attachment);
To
// the default second argument is $c_type = 'application/octet-stream'
$isAttached = $message->addAttachment($attachment, 'aplication/pdf');
if ($isAttached !== true) {
// an error occured
echo $isAttached->getMessage();
}
And you always want to make sure that you call
$message->get();
before
$message->headers($extraheaders);
or the whole thing wont work
I am pretty sure it must be an ini problem blocking file_get_contents(). However I came up with a better solution. I reworked the generator.php file and turned it in to a function definition. So I've got:
include_once('generator.php');
$attachment = "cache/form.pdf";
file_put_contents( $attachment, my_pdf_generator( $arg1, $arg2 ) );
...
$message->addAttachment( $attachment, "application/pdf" );
This way I do not need to write the file first. It works great (although I am still having slight issues with Outlook/Exchange Server, but I think that is a largely unrelated issue).
i don't understand how send one email for all users, i do it this in my controller :
// Init
$data = $this->request->data['Email'];
$d = array(
'subject' => $data['subject'],
'message' => $data['message']
);
// QUERY
$all = $this->Spoutnik->find('all', array(
'conditions' => array(
'Spoutnik.role >=' => '1'
),
'fields' => array('Spoutnik.email')
));
$this->set(compact('all'));
// list
$bcc = '';
foreach ($all as $user) {
$bcc .= $user['Spoutnik']['email'].',';
}
// MAIL
App::uses('CakeEmail', 'Network/Email');
$CakeEmail = new CakeEmail('default');
$website_short_name = Configure::read('website.short_name');
$CakeEmail->bcc("$bcc");
$CakeEmail->subject(''.$website_short_name.' :: '.$d['subject'].'');
$CakeEmail->viewVars(array(
'message' => (''.$d['message'].'')
));
$CakeEmail->emailFormat('html');
$CakeEmail->template('message_direct');
// final
$CakeEmail->send();
But i have error "no valid mail" , and after the liste of user's mail
what is wrong in my code ?
Couple of things I've noticed at a quick glance...
foreach ($all as $user) {
$bcc .= $user['Spoutnik']['email'].',';
}
In that code, you're adding a comma after every email, so at the end of your string you'll have a comma. Try this:
$e = 0;
foreach ($all as $user) {
if($e > 0) $bcc .= ',';
$bcc .= $user['Spoutnik']['email'];
$e++;
}
--edit-- good point Deepak, Cake's documentation suggests you give BCC an array. It's easier and more efficient to produce so do that.
Second, $CakeEmail->bcc("$bcc"); doesn't need the quotes. It should work fine with them, but I've seen Cake do some pretty weird things... Try taking them out:
$CakeEmail->bcc($bcc);
Third, you're setting all those emails to BCC which is fine, but I can't see a to address. If you want to send out to a lot of email address without them seeing each other, you still need to send the email to somewhere, even if its noreply#yourdomain.com. Add a to address in before you send:
$CakeEmail->to('noreply#yourdomain.com');
I will just use the addBcc function of CakeEmail and modify the loop:
App::uses('CakeEmail', 'Network/Email');
$CakeEmail = new CakeEmail('default');
// list
foreach ($all as $user) {
$CakeEmail->addBcc($user['Spoutnik']['email']);
}
$website_short_name = Configure::read('website.short_name');
$CakeEmail->subject(''.$website_short_name.' :: '.$d['subject'].'');
$CakeEmail->viewVars(array(
'message' => (''.$d['message'].'')
));
Try changing your $bcc block to this:
// list
$bcc = array();
foreach ($all as $user) {
$bcc[]= $user['Spoutnik']['email'];
}
Also refer to CakeEmail Documentation
I'm new in Amazon SES and I'm trying to send an email with this code:
<?php
require_once 'aws/sdk.class.php';
$ses = new AmazonSES();
$to = array('ToAddress' => 'mario#wowfi.com');
$content = array('Subject.Data' => 'Tema', 'Body.Text.Data' => 'hello');
$r = $ses->send_email("mario#wowfi.com", $to , $content);
print_r($r);
?>
In the output it says: Missing required header 'To', what am I doing wrong?
I already solved it, I had two problems in my code:
The index "ToAddress" is incorrect, it has to be in plural "ToAddresses".
And the value it has to be an array like this: array('mario#wowfi.com')