I am trying to insert the name, email, company, phone and country from the array into the email message, but all I get is an empty email.
The subject, to and from all works, but I am not getting the message in the email.
Can anyone tell me what I am doing wrong?
<? php
public function contact_mail() {
$data = array(
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'company' => $this->input->post('company'),
'phone' => $this->input->post('phone'),
'country' => $this->input->post('country')
);
$success = $this->data->save_contact_info($data);
if ($success) {
$data = array('success_message' => 'Thank you. We\'ll be in touch.');
$this->load->view('ajax/json', array('json' => $data));
$this->load->library('email');
$config['mailtype'] = 'html';
$this->email->initialize($config);
$this->email->from("noreply#example.com", "Example Notification Service");
$this->email->to(array('mha#example.com'));
$this->email->subject('Contact form');
$this->email->message($data);
$this->email->send();
}
else {
$this->load->view('ajax/error', array('error_msg' => 'Please fill out all fields.'));
}
}
?>
Change this line:
$this->load->view('ajax/json', array('json' => $data));
to this:
$view = $this->load->view('ajax/json', array('json' => $data));
Then instead of sending $data through which is just an array, you send through $view which is the HTML that includes the array.
Also, you're using $data to get all of the POST variables, but then use data later to set a flash message.
I'm not sure whether you mean to overwrite $data or not, but it's always best to keep the variable names different for different things.
Related
The 2nd foreach should send to each new_token->mob .but it sends to one and then stops.what am doing wrong?
My code is :
foreach($new_tokens as $new_tokenss)
{
foreach($new_tokenss as $new_token)
{
$message = "Message";
$message_encoded = urlencode($message);
$postData = array(
'authkey' => \Config::get('procedures.msg91.authKey'),
'mobiles' => $new_token->mob,
'message' => $message_encoded,
'sender' => \Config::get('procedures.msg91.senderId'),
'route' => \Config::get('procedures.msg91.route')
);
$curl = new \anlutro\cURL\cURL;
$curl->post(\Config::get('procedures.msg91.api_url'), $postData);
return 'New emergency patient';
}
}
return 'New emergency patient';
This like makes the function to return the above mentioned value and thus break the for loop.
I have successfully setup a Mailgun account for Transaction email.
The Problem;
I want my email content which are dynamic inside my Email Template please help and even my file attachment not working
<?php
$filePath='#/home/allinclende/public_html/apply/CIW.pdf';
$message = $message;
$html = file_get_contents('email_1.html');
$html .= $message;
$mgClient = new Mailgun('key-xxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
//enter domain which you find in Default Password
$domain = "apply.allinc.com";
# Make the call to the client.
$result = $mgClient->sendMessage($domain, array(
'from' => 'Jane Doe <jdoe#allinc.com>',
'to' => $add,
'cc' => 'inquiry <info#allinc.com>',
'subject' => $subject,
'text' => $message,
'html' => $html,
'attachment[1]' => $filePath
));
?>
In your email_1.html file include some unique placeholders. Make an array containing them:
$search = array("FIRSTNAME", "LASTNAME", "APPOINTMENTDATE");
Query your database and get the variables to replace each one, make an array of those:
$replace = array($firstname, $lastname, $appointmentdate);
Then it's as simple as:
$customhtml = str_replace($search, $replace, $html);
Send $customhtml via mailgun, then loop through again to personalise the email for each customer
Correct construct for sending an attachment is like this:
$result = $mgClient->sendMessage($domain, array(
'from' => 'Jane Doe <jdoe#allinc.com>',
'to' => $add,
'cc' => 'inquiry <info#allinc.com>',
'subject' => $subject,
'text' => $message,
'html' => $html
), array(
'attachment' => array('#/home/allinclende/public_html/apply/CIW.pdf')
));
Is there an easy way to "override" the destination of an email using CakeEmail?
I have a model method that send an email using this piece of code:
$Email = new CakeEmail();
$Email->config('default');
$sent = $Email->template('new-notification')
->subject('A new notification for you')
->viewVars([
'name' => $foo['User']['name'],
'text' => $foo['Notification']['text'],
)
->emailFormat('html')
->to($foo['User']['email'])
->send();
And this configuration:
class EmailConfig
{
public $default = array(
'host' => 'smtp.server.com',
'port' => 587,
'username' => 'user#domain.com',
'password' => 'pwdAsYouKnow',
'from' => array('noreply#domain.com' => 'Company'),
'transport' => 'Smtp'
);
}
As you can see, I send the email for a dynamically defined user's email.
My objective is when I'm developing locally on my machine, to force every call to ->send() to force a destination like developer#domain.com.
I can easily detect if I'm on development, but I don't know how to force in a "master" way to CakeEmail only send to a defined account overriding the one set on the call.
I already tried to set an
'to' => array('developer#domain.com' => 'Dev'),
inside $default but, no success.
I appreciate any help, thanks!
i'm assuming when you are on local machine you run in debug mode, so you can check if debug mode is on then use that to send to different email
if(Configure::read('debug')){
$emailTo = 'developer#domain.com'
}
else{
$emailTo = $foo['User']['email'];
}
then you just use variable for email address:
$sent = $Email->template('new-notification')
->subject('A new notification for you')
->viewVars([
'name' => $foo['User']['name'],
'text' => $foo['Notification']['text'],
)
->emailFormat('html')
->to($emailTo)
->send();
I need to reply to incoming emails including the attachment they contain.
I wrote this code, but for some reason when I check my debug log and mandrill API logs the attachment isn't included into the request.
Where's my fault?
if ($message['attachments'])
{
$mail=
[
'html' => $mail->msg->html,
'text' => $mail->msg->text,
'subject' => $mail->msg->subject,
'from_email' => 'test#test.com',
'from_name' => $mail->msg->from_name,
'to' => [
[
'email' => 'test#test.com',
'name' => 'test#test.com',
'type' => 'to'
]
],
'headers' => [
'Reply-To' => $mail->msg->from_email
],
];
//just some sample data for testing
foreach ($message['attachment'] as $attachment)
{
$mail['attachments']['name'] ='sample.png';
$mail['attachments']['type'] ='image/png';
$mail['attachments']['content'] ='iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABlBMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDrEX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
}
$async = false;
$ip_pool = 'Main Pool';
$v = var_export( $mail, true);
file_put_contents('phplog.txt', 'gesendet: ' . $v, FILE_APPEND);
$result = $mandrill->messages->send($mail, $async, $ip_pool, $send_at);
}
Below line looks suspicious. If you check https://mandrillapp.com/api/docs/messages.html here. You can clearly see attachments is a multidimensional array.
//just some sample data for testing
foreach ($message['attachment'] as $attachment)
{
$mail['attachments']['name'] ='sample.png';
$mail['attachments']['type'] ='image/png';
$mail['attachments']['content'] ='iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABlBMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDrEX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
}
At least it should be like following, but still you don't use $attachment in your code. But the correct usage like following.
foreach ($message['attachment'] as $key => $attachment)
{
$mail['attachments'][$key]['name'] ='sample.png';
$mail['attachments'][$key]['type'] ='image/png';
$mail['attachments'][$key]['content'] ='iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABlBMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDrEX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
}
If you want to try in more clear way please use as following :
$attachment = [];
$attachment['name'] ='sample.png';
$attachment['type'] ='image/png';
$attachment['content'] ='iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABlBMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDrEX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
$mail['attachments'][] = $attachment;
UPDATED ACCORDING TO COMMENT :
things look fine, just change your if statement if ($message['attachments']) to if (is_array($message['attachments']) && count($message['attachments']) > 1) then after if statment place $message['attachments'] = array_values($message['attachments']);
so loop like the following one. Just pay attention to $attachment I added to semantic keys to it, you can change according to your needs. I don't know where is your source you getting file, static or file upload etc. I added as an example.
foreach ($message['attachment'] as $key => $attachment)
{
$mail['attachments'][$key]['name'] =$attachment['fileName'];
$mail['attachments'][$key]['type'] =$attachment['mimeType'];
$mail['attachments'][$key]['content'] = chunk_split(base64_encode(file_get_contents($attachment['filePath']))); ;
}
I can find out rejected addresses within swiftmailer from the ->setTo(array(. With the following code:
$mailer = Swift_Mailer::newInstance( ... );
$message = Swift_Message::newInstance( ... )
->setFrom( ... )
->setTo(array(
'receiver#bad-domain.org' => 'Receiver Name',
'other#domain.org' => 'A name',
'other-receiver#bad-domain.org' => 'Other Name'
))
->setBody( ... )
;
// Pass a variable name to the send() method
if (!$mailer->send($message, $failures))
{
echo "Failures:";
print_r($failures);
}
/*
Failures:
Array (
0 => receiver#bad-domain.org,
1 => other-receiver#bad-domain.org
)
*/
Now I want to find out rejected addresses from Cc and Bcc fields too. How can I add similar code? Is there a tutorial or a workaround? There is no example in the swiftmailer tutorial.
You can use a try-catch block where you set the to, cc, and bcc email addresses.
From the manual:
If you add recipients automatically based on a data source that may
contain invalid email addresses, you can prevent possible exceptions
by validating the addresses using Swift_Validate::email($email) and
only adding addresses that validate. Another way would be to wrap your
setTo(), setCc() and setBcc() calls in a try-catch block and handle
the Swift_RfcComplianceException in the catch block.
So you can use the try-catch on $message object:
$message = Swift_Message::newInstance();
$emails = array(
'receiver#bad-domain.org' => 'Receiver Name',
'other#domain.org' => 'A name',
'other-receiver#bad-domain.org' => 'Other Name'
);
// loop through emails and set the individually to catch exceptions
foreach($emails as $email => $name)
{
try {
$message->setTo(array($email => $name));
} catch(Swift_RfcComplianceException $e) {
echo "The email ".$email." seems invalid";
}
}
// And do the same thing with cc and bcc emails
$ccEmails = array(
'receiver#ccemail.org' => 'CC Receiver Name'
);
foreach($ccEmails as $email => $name)
{
try {
$message->setCc(array($email => $name));
} catch(Swift_RfcComplianceException $e) {
echo "The email ".$email." seems invalid";
}
}
You could set the failures to a variable if you depending on how you want to handle them.
Edit: If you have an array of users with separate first and last names you can user the below:
$users = array(
array('email' => 'receiver#bad-domain.org', 'first' => 'Receiver', 'last' => 'Name'),
array('email' => 'other#domain.org', 'first' => 'A', 'last' => 'name'),
array('email' => 'other-receiver#bad-domain.org', 'first' => 'Other', 'last' => 'Name')
);
// loop through users and set the individually to catch exceptions
foreach($users as $user)
{
try {
$message->setTo(array($user['email'] => $user['first'].' '.$user['last']));
} catch(Swift_RfcComplianceException $e) {
echo "The email ".$user['email']." seems invalid";
}
}