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
Related
My code works, but for some strange reason when I return a string it adds extra space and I think a new line as well.
The code below works fine. However the length of this when fetched with Ajax is 11, the string success has a length of seven so this isn't right and it also doesn't equal to 'success' when comparing.
I've looked through a few other questions but all the solutions they offer don't seem to be working for me. No trailing spaces anywhere as far as I can see. And it's also not coming from any of the files I'm requiring.
<?php
/*
Plugin Name: Quotation Rest API
Plugin URI:
Description: A rest API for sending quotation request through to email using Sendgrid.
Version: 1.0
Author: Niels
Author URI:
*/
add_action( 'rest_api_init', function () {
register_rest_route( 'offerte', '/send',array(
'methods' => 'POST',
'callback' => 'send_mail',
) );
} );
include('MailChimp.php');
use \DrewM\MailChimp\MailChimp;
function send_mail( WP_REST_Request $request) {
$data = $request['data'];
$name = htmlspecialchars($request['name']);
$email = htmlspecialchars($request['email']);
$comment = htmlspecialchars($request['comment']);
$website = htmlspecialchars($request['url']);
$company = htmlspecialchars($request['company']);
$tel = htmlspecialchars($request['tel']);
$mailchimp = htmlspecialchars($request['mailchimp']);
$dataArray = json_decode($data, true);
ob_start();
include('mail.php');
$mailHTML = ob_get_clean();
$success = 'success';
if (!empty($mailchimp)) {
// add email to mailchimp list
$client = new MailChimp('-us14');// get from setttings
$list_id = ' ';// get from setttings
$result = $client->post("lists/$list_id/members", [
'email_address' => $email,
'status' => 'subscribed',
]);
}
$to = " #icloud.com";// get from setttings
$subject = "Offerte van " . $name;// // get from setttings (maybe...)
$message = $mailHTML;
if(wp_mail( $to, $subject, $message)){
return $success;
} else {
// return 'error';
}
}
?>
I am trying to send email to multiple recipient address in cake php 3.
my codes are :
$this->loadModel('AsIndividualDetails');
$EmailDetails = $this-> AsIndividualDetails->find('all',['fields'=>'email']);
$EmailDetails = $EmailDetails->toArray();
foreach ($EmailDetails as $key => $a) {
$this->loadModel('DomainEmailDetails');
$DomainEmailDetails = $this-> DomainEmailDetails->find('all')->first();
$DomainEmailDetails = $DomainEmailDetails->toArray();
$host = 'ssl://'.$DomainEmailDetails['host_name'];
$username = $DomainEmailDetails['user_name'];
$password = $DomainEmailDetails['user_password'];
$port = $DomainEmailDetails['port'];
$email_to = $a['email'];
$senderName = 'abc';
$email_id ='xyz110#gmail.com';
Email::configTransport('WebMail', [
'className' => 'Smtp',
'host' => $host,
'port' => $port,
'timeout' => 30,
'username' => $username,
'password' => $password,
'client' => null,
'tls' => null,
]);
////////// SEND MAIL
$email = new Email('WebMail');
$email ->template('default','default')
->emailFormat('both')
->from([$username => $senderName])
->to($email_to)
->replyTo($email_id)
->subject('Client Message');
$response = $email->send('My msg');
if($response){
echo 'success';
}else{
echo 'failed';
}
}
When I run this script just only one mail send successfully and after that an error has come :
Cannot modify an existing config "WebMail"
How to solve this error and send mail to all recipient mail address.
If you really need set the config inside a loop, you could delete it before rewrite the config:
use Cake\Mailer\Email;
Email::dropTransport($key);
See Class Email API for more info
Make your email configuration outside of the loop. You don't want to try and establish the configuration every time you send the emails - just one time. Then send all the emails based on that one configuration.
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.
this seems pretty straightforward but i just can't get mailchimp to get the emails. I have a checkbox and all I want to do is say, if check then subscribe.
I get an error 500 and I have no idea where to go with this.
<?php echo CHtml::CheckBox('[subscribe]',true, array ( 'value'=>'yes',)); ?>
if ( $_POST['subscribe'])
{
Yii::import('application.vendors.mailchimp');
$mailchimp = new Mailchimp(Yii::app()->params['mailchimp']['key']);
$listId = '*******';
$email = array(
'email' => trim($_POST['User']['email']));
$subscriber = $mailchimp->list->subscribe(
$listId,
$email,
$merge_vars=null,
false,
false
);
}
I am using two different servers for dev site and live site. My webservice function to send email confirmation is working on Dev Site but it is not working in Live Site. My
email confirmation function is:
function getEmailConfirmation($UserName, $ChecklistID,$UserID,$AnswerBulkID, $TotalSubmitted){
if ( ($UserID == '') || (!isset($UserID)) ) {
return error("partial");
}
$entry = new Entry();
$entry_date_check = date('Y-m-d'); //for check if already
$count_tbl_answers = $entry->listEntry("SELECT count(*) as total_from_answers FROM tbl_answers where user_id='".$UserID."' and answer_bulk_id ='".$AnswerBulkID."' and date(ent_date) = '".$entry_date_check."' ");
$to = "****#gmail.com";
$subject = "TDL Checklist email";
$message = "Hello there! \n User ID: ".$UserID." \n User Name: ".$UserName."\n Check List ID: ".$ChecklistID."\n Answer Bulk ID: ".$AnswerBulkID;
$from = "TLD-Email-Confirmation";
$headers = "From:" . $from;
if(($count_tbl_answers[0]['total_from_answers']) == $TotalSubmitted){
mail($to,$subject,$message,$headers);
$result = array(
"user_id" => $UserID,
"user_name" => $UserName,
"check_list_id" => $ChecklistID,
"answer_bulk_id" => $AnswerBulkID,
);
} else{
$delete_bulk = $entry->query("delete FROM tbl_answerbulk WHERE user_id ='".$UserID."' and check_list_id ='".$ChecklistID."' and id ='".$AnswerBulkID."' ");
$delete_answer = $entry->query("delete FROM tbl_answers WHERE user_id='".$UserID."' and answer_bulk_id ='".$AnswerBulkID."' ");
return error("custom","","Could not submit your answer this time. Please try again later.");
}
$message = json_encode(
$result
);
$fin_data['response'] = json_encode(array('Message'=>'ok','Data'=>$message));
return $fin_data;
}
This function works perfectly on dev server, but it does not work on live server. What might be the reason?
Note:
Although mail function not working on Live site, it returns the result array which is just after the mail function.
That is:
#mail($to,$subject,$message,$headers);
$result = array(
"user_id" => $UserID,
"user_name" => $UserName,
"check_list_id" => $ChecklistID,
"answer_bulk_id" => $AnswerBulkID,
);
I also tried ini_set('smtp_port', 587); and #mail($to,$subject,$message,$headers); but does not worked.