I'm using the clockwork api. The error code is: Message failed - Error: Invalid 'To' Parameter
I think it's beacause in the $message line. I've not set the variables correctly.
<?php
require 'class-Clockwork.php';
try
{
// Create a Clockwork object using your API key
$API_KEY = "API_KEY";
$clockwork = new Clockwork( $API_KEY );
$phonenumber = $_POST['phonenumber'];
$message = $_POST['sendmessage'];
// Setup and send a message
$message = array( 'to' => '$phonenumber', 'message' => '$sendmessage' );
$result = $clockwork->send( $message );
// Check if the send was successful
if($result['success']) {
echo 'Message sent - ID: ' . $result['id'];
} else {
echo 'Message failed - Error: ' . $result['error_message'];
}
}
catch (ClockworkException $e)
{
echo 'Exception sending SMS: ' . $e->getMessage();
}
?>
$message = array( 'to' => '$phonenumber', 'message' => '$sendmessage' );
This should be
$message = array( 'to' => $phonenumber, 'message' => $sendmessage );
Otherwise you get the literal text "$phonenumber", not the contents of the variable.
Related
I am trying to send an email with both a calendar invite and HTML body content, but I can't seem to get the both added to the email object to be sent via SendGrid
I am able to send a calendar invite by itself and HTML body content by itself but not together.
function sendgridAPI(){
GLOBAL $mgClient,$domain,$toName, $toEmail, $fromName, $fromEmail, $subj, $body, $cc, $bcc, $attachments, $mimeMessage, $sendgrid_api_key;
$email = new \SendGrid\Mail\Mail();
$email->setFrom($fromEmail, $fromName);
$email->setSubject($subj);
$toEmails = [$toEmail => $toName,];
$email->addTos($toEmails);
if ($mimeMessage != ""){
echo "<br> 1 <br>";
$contents = [
"text/calendar" => $mimeMessage,
"text/html" => $body
];
$email->addContents($contents);
}
else{
$content = ["text/html" => $body];
$email->addContent($content);
}
if($cc != ""){
$ccEmails = [$cc => "CC",];
$email->addCcs($ccEmails);
}
if ($attachments != ""){
$filePath = $attachments;
$fileName = substr($attachments, strrpos($attachments, '/') + 1);
$fileData = base64_encode(file_get_contents($filePath));
$fileExtension = substr($attachments, strrpos($attachments, '.') + 1);
$fileType = 'application/'. $fileExtension;
$email->addAttachment(
$fileData,
$fileType,
$fileName,
"attachment"
);
$email->addAttachments($attachments);
}
$sendgrid = new \SendGrid($sendgrid_api_key);
try {
$response = $sendgrid->send($email);
$data = $response->headers();
print_r($data);
gettype($data['5']);
$responseSG = substr($data['5'], strpos($data['5'], ":") + 1);
return $responseSG;
//echo $responseSG;
} catch (Exception $e) {
echo 'Caught exception: '. $e->getMessage() ."\n";
return "";
}
}
?>
The variables are passed to this function then the email object is constructed to be sent using the SendGrid API
You need to create an attachment object for addAttachment(), not pass in a filename. And an array of Attachment objects for addAttachments()
https://github.com/sendgrid/sendgrid-php/blob/master/lib/mail/Mail.php#L1152-L1172
Here's the constructor for an Attachment:
https://github.com/sendgrid/sendgrid-php/blob/master/lib/mail/Attachment.php#L35-L52
I am sending an e-mail with different variables as an JSON encoded array from an online store. I get the mail just fine and all the data is in there except for one of the variables, which is a JSON encoded array by itself. this particular variable shows as "false" in the e-mail. I'm missing something?
I'm using PHP mail to do it.
<?php
require_once "Mail.php";
$link = mysqli_connect("localhost", "xxx", "xxx", "xxx");
if ($link === false) {
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sql = "SELECT * FROM users WHERE user = '" . $_SESSION['logged'] . "'";
$result = mysqli_query($link, $sql);
$row = mysqli_fetch_assoc($result);
$user = $_SESSION['logged'];
$rua = $_POST['rua'];
$numero = $_POST['numero'];
$apt = $_POST['apt'];
$cep = $_POST['cep'];
$total = $_POST['total'];
$comment = $_POST['observacion'];
$from = $row['mail'];
$mailTo = "xxxxx#hotmail.com";
$subject = "compra online - no cep";
$compra = $_SESSION["shopping_cart"];
$compra2 = json_encode($compra);
$bod = array(
'user' => $user,
'rua' => $rua,
'cep' => $cep,
'compra' => $compra2,
'comment' => $comment,
'total' => $total,
);
$body = json_encode($bod);
$headers = array(
'From' => $from,
'To' => $mailTo,
'Subject' => $subject,
);
$smtp = Mail::factory('smtp', array(
'host' => 'smtp-mail.outlook.com',
'port' => '587',
'auth' => true,
'username' => 'xxxxxxx#hotmail.com',
'password' => 'xxxxxxx',
));
$mail = $smtp->send($mailTo, $headers, $body);
if (PEAR::isError($mail)) {
echo ("<p>" . $mail->getMessage() . "</p>");
} else {
header("Location: lojacart.php?mailok");
}
mysqli_close($link);
I'm getting the e-mail with all the data except for the $compra2 variable which is showing as 'compra'=false. now if I echo the variable $compra2 it actually have a large string of data on it.
I think it might have to do with the variable being a JSON encoded session, but I'm not sure.
After a lot of trying and getting frustrated I came with a solution, for those looking for a something like this:
I added a hidden input to the form on the previous page and assigned it a value as "print_r($SESSION['whatever you session is called'], TRUE)" ...
on the next page I took the $_POST[] form that input and added the variable to the $body array for the mail().
It will print some array garbage in the middle BUT it works like a charm! and I cleaned all the "extra" text form the array using preg_replace(). It may not be an elegant way of doing it but as I said... it worked
I have a problem with an ajax call to php mail() script failing on success, no json data is returned. It's basically just posting a form to a php script, validating & returning some json.
If I encounter either a validation error the json is returned correctly & my jquery executes as expected.
If NO errors are encountered, PHP processes and sends mail correctly but no data is returned to my jquery script.
Here is my code:
<?php
require "gump.class.php";
$gump = new GUMP();
$mailto = 'me#mydomain.com';
$subject = 'A new form inquiry has been submitted.';
$output = array(
'status' => 'success',
'msg' => 'Mail processed successfully',
'success' => 'success',
);
function render_email($input) {
//error_log("render_email " . print_r($input,TRUE), 0);
ob_start();
include "email-template.phtml";
return ob_get_contents();
}
$input = $gump->sanitize($_POST);
$gump->validation_rules(array(
'first_name' => 'required',
'last_name' => 'required',
'email' => 'required',
//'country' => 'required|valid_email',
//'gender' => 'required|exact_len,1',
//'company' => 'required|valid_cc|max_len,2|min_len,1',
//'bio' => 'required'
));
$gump->filter_rules(array(
'first_name' => 'trim|sanitize_string',
'last_name' => 'trim|sanitize_string',
'email' => 'trim|sanitize_string',
));
$validated = $gump->run($_POST);
if($validated === false){
error_log("GUMP: validation Error: " . print_r($gump->get_readable_errors(true),TRUE));
$output = array(
'status' => 'error',
'msg' => '<strong>Validation Error: </strong>' . $gump->get_readable_errors(true),
'error' => 'error',
);
}else{
error_log("GUMP: Successful Validation, processing mail",0);
// ghead & mail the form
$to = $mailto ;
$subject = $subject;
$body = render_email($input);
$headers = "From: Metz Tea <sales#mydomain.com>" . "\r\n";
$headers .= "Reply-To: sales#mydomain.com\r\n";
$headers .= "Return-Path: info#example.com\r\n";
$headers .= "X-Mailer: PHP\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
if(!mail($to,$subject,$body,$headers)){
$output = array(
'status' => 'error',
'msg' => 'The system failed to send mail.',
'error' => 'error',
);
};
error_log("mail complete",0);
}
header("HTTP/1.1 200 OK");
header('Content-type: application/json');
$output = json_encode($output);
echo $output;
return;
and the jquery:
$('form').submit(function(event){
event.preventDefault();
})
$(document).on("forminvalid.zf.abide", function(event,frm) {
var modal = $('form').closest('.reveal').attr('id');
$(".success").hide();
$(".alert").show();
console.log("Form id "+event.target.id+" is invalid");
formToTop(modal);
}).on("formvalid.zf.abide", function(event,frm) {
console.log("Form id "+event.target.id+" is VALID");
var modal = $('form').closest('.reveal').attr('id');
var data = frm.serializeArray();
$.ajax({
type : 'POST',
url : 'process.php',
data : data,
dataType : 'json',
encode : true
}).done(function(data) {
console.log('completed successfully '+ data);
if (data.status != 'success') {
console.log('AJAX returned error = ' + data.msg);
$('.callout p').html(data.msg);
$('.alert').show();
formToTop(modal);
} else {
console.log( 'AJAX returned success = ' + data.status);
$('.callout p').html(data.msg);
$('#orderform').find("input[type=text], textarea, select").val("");
$('.alert').hide();
$('.success').show();
$('form').slideUp('500');
formToTop(modal);
}
}).fail(function(data) {
//error
});
event.preventDefault();
});
It must be the mail() function somehow, it does send mail on success, but no data is sent back to the ajax script.
what is my error here?
The problem is here:
function render_email($input) {
//error_log("render_email " . print_r($input,TRUE), 0);
ob_start();
include "email-template.phtml";
return ob_get_contents();
}
You've got side-effects both leaving the contents of your rendered template in the buffer, and leaving buffering enabled. You'll want to change this to:
function render_email($input) {
//error_log("render_email " . print_r($input,TRUE), 0);
ob_start();
include "email-template.phtml";
$ret = ob_get_contents();
ob_end_clean();
return $ret;
}
How to post the 360degree image to Facebook using PHP Facebook SDK v5?
I tried using this parameter 'allow_spherical_photo'=> true, But it is not working.
Posting image as simple flat image.
here is my code
$message = '';
$title = '';
$link = 'http://google.com/';
$description = 'My Image Posting';
$url = 'My Public image url';
$caption = 'My Image';
$attachment = array(
'message' => $message,
'name' => $title,
'allow_spherical_photo'=> true,
'source' => $url,
'description' => $description,
'photo'=>$url,
);
echo "<pre>";
print_r($attachment);
try{
echo "try";
//Post to Facebook
$fb->post('/me/feed', $attachment, $accessToken);
//Display post submission status
echo 'The post was submitted successfully to Facebook timeline.';
}catch(FacebookResponseException $e){
echo 'Graph returned an error: ' . $e->getMessage();
exit;
}catch(FacebookSDKException $e){
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
can anyone help me out? I searched a lot.
Hi
I have a jabberserver and i would like to be able to push messages out to users from a php script.
F.x. if i call script.php from my browser, it sends a message to a user.
I've tried both with jaxl and xmpphp which are xmp frameworks, but i cant get it to work. Not with my own server, and neither with facebooks server.
I have the following ind my script.php:
error_reporting(E_ALL);
include("lib/xmpphp/XMPPHP.php");
$conn = new XMPP('chat.facebook.dk', 5222, 'username', 'password', '', 'chat.facebook.com', true, XMPPHP_Log::LEVEL_VERBOSE);
$conn->connect();
$conn->processUntil('session_start');
$conn->message('someusername#chat.facebook.com', 'This is a test message!');
$conn->disconnect();
But nothing happends and no errors either.
I've followed this guide to set up an echobot, and it works with both my server and facebook. The script is here: http://abhinavsingh.com/blog/2010/02/writing-your-first-facebook-chat-bot-in-php-using-jaxl-library/ <-- and is run on the server commandline, and waiting for a message, and then replys.
What do i have to do ?
<?php
set_time_limit(0); // some time connection take while
require_once 'xmpp-lib/XMPPHP/XMPP.php';
$host = 'you Host name'; // ex.192.168.2.1
$port = '5222'; // its defauls xmpp port
$username = 'name#host' // ex vivek#host
$pass = 'userpass';
$conn = new XMPPHP_XMPP(host , $port, $username, $pass, 'xmpphp','yourhost', $printlog=false, $loglevel=XMPPHP_Log::LEVEL_INFO);
try {
$conn->connect();
$conn->processUntil('session_start');
$conn->presence();
$conn->message('anotherusername#host', 'Hello!');
$conn->disconnect();
} catch(XMPPHP_Exception $e) {
die($e->getMessage());
}
?>
<?php
$command = 'send_message';
$requestParams = array('type' => 'chat',
'to' => '919999999999#127.0.0.0',
'from' => '919999999991#127.0.0.0',
'body' => 'Hi vivek patel',
'subject' => 'test',
);
$request = xmlrpc_encode_request($command, $requestParams, (array('encoding' => 'utf-8')));
$context = stream_context_create(array('http' => array('method' => "POST",
'header' => "User-Agent: XMLRPC::Client mod_xmlrpc\r\n" .
"Content-Type: text/xml\r\n" .
"Content-Length: " . strlen($request),
'content' => $request
)
)
);
try {
$file = file_get_contents("http://127.0.0.0:4560/RPC2", false, $context);
$response = xmlrpc_decode($file);
} catch (Exception $e) {
echo $e->getMessage();
exit;
}
if (xmlrpc_is_fault($response)) {
trigger_error("xmlrpc: $response[faultString] ($response[faultCode])");
}
echo '<pre>';
echo print_r($response);
echo '</pre>';
?>