i have written this code for sending emails
Route::post('contact', function(){
$inputs = Input::all();
$rules = array(
'email' => 'required|email',
'name' => 'required|min:2',
'message' => 'required',
'recaptcha_response_field' => 'required|recaptcha',
);
$validator = Validator::make($inputs, $rules);
if($validator->passes()){
$fromEmail = Input::get('email');
$fromName = Input::get('name');
$subject = Input::get('subject');
$data = array('message' => Input::get('message'));
$toEmail = 'info#danielchikaka.com';
$toName = 'Daniel Chikaka';
Mail::send('emails.contact', $data, function($message) use ($toEmail, $toName, $fromEmail, $fromName, $subject){
$message->to($toEmail, $toName)->from($fromEmail, $fromName)->subject($subject);
});
return Redirect::to('/');
}
return Redirect::to('/#contact')->withInput()->withErrors($validator);
});
and my view emails.contact is
<html>
<body>
<p><b>Email From:</b> {{$fromName}} of {{$fromEmail}}</p>
<p><b>Subject:</b> {{$subject}}</p>
<b> Message:</b> <br>
</html> {{$data}}
</body>
but whenever i send email all i get is :
Email From:{{$fromName}} of {{$fromEmail}}
Subject: {{$subject}}
Message:
{{$data}
why my view does not pick up the data from Mail Class?
Make sure your view has the blade extension:
views/emails/contact.blade.php
Your $data array consists of only the variable message. So, in your blade template only {{$message}} will work. Ensure that you are passing all the variables required in your view into the $data array.
$data = array(
'message' => Input::get('message'),
'fromName' => Input::get('name'),
'fromEmail'=> Input::get('email')
);
Now, in your contact.blade.php you can use them as normal variables {{$formName}}, {{$fromEmail}} etc;
Also, since you are using blade templating, ensure that your view file is having proper extension i.e contact.blade.php
i have found a solution to my problem:
$fromEmail = Input::get('email');
$fromName = Input::get('name');
$subject = Input::get('subject');
$data = array('content' => Input::get('message'));
$toEmail = 'info#danielchikaka.com';
$toName = 'Daniel Chikaka';
Mail::send('emails.contact', $data, function($message) use ($toEmail, $toName, $fromEmail, $fromName, $subject){
$message->to($toEmail, $toName)->from($fromEmail, $fromName)->subject($subject);
});
and in the view has to be:
<html>
<body>
{{$content}}
</html>
</body>
Important:
Never use {{$message}} in contact view file, according to Taylor Otwell, that is reserved in email view files
Hope this will help someone one day!
Related
I'm trying to create an email with php in Dynamics 365 by using the AlexaCRM php-crm toolkit after someone fills in the form on our website.
The email also appears in Dynamics, but the from and to fields are empty. The email itself and the subject are stored in Dynamics.
Does anyone had the same problem or does anyone knows what I'm doing wrong?
This is the code I'm using.
<?php
require_once '../vendor/autoload.php';
use AlexaCRM\CRMToolkit\Client as OrganizationService;
use AlexaCRM\CRMToolkit\Settings;
use AlexaCRM\CRMToolkit\Client;
use AlexaCRM\CRMToolkit\Entity\MetadataCollection;
use AlexaCRM\CRMToolkit\Entity\EntityReference;
$options = [
'serverUrl' => 'xxx.dynamics.com',
'username' => 'xxx#xxx.com',
'password' => 'xxxxxx',
'authMode' => 'OnlineFederation',
];
$serviceSettings = new Settings( $options );
$service = new OrganizationService( $serviceSettings );
$email = $service->entity( 'email' );
$email->subject = 'TEST SUBJECT';
$email->description = 'TEST EMAIL';
$email->sender = 'Sender Name';
$email->from = 'test#gmail.com';
$email->to = 'Our Company';
$email->torecipients = 'test#ourcompany.com';
$emailId = $email->create();
?>
Thanks for your help.
I created activityparties, but it still doesn"t write the email correctly to Dynamics.
I don't know how to combine the two parties.
This is my code:
$to = $service->entity( 'activityparty' );
$to->partyid = new EntityReference( 'systemuser', ''.$guid_stassen.'');
$from = $service->entity( 'activityparty' );
$from->partyid = new EntityReference( 'contact', ''.$guid.'');
$email = $service->entity( 'email' );
$email->subject = 'TEST SUBJECT';
$email->description = 'TEST EMAIL';
$email->from = ''.$from.'';
$email->to = ''.$to.'';
$emailId = $email->create();
rather than
$email->from = ''.$from.'';
$email->to = ''.$to.'';
you will have to use email_activity_parties as an array. I am not familair with PHP but something like below
$email->email_activity_parties=[
{
"partyid_systemuser#odata.bind" : "/systemusers(CED2E02D-188E-4AA8-B6E2-D746E9B370C1)",
"participationtypemask" : 1
},
{
"addressused":"vvyas#cloudfronts.com",
"participationtypemask" : 2
}
];
This happens because To and From field is not text field rather it is Party list field.
you cannot directly add/put email address, you will need to create object with it's type and email address.
Take a look at this blog, it has all the information you need.
Below sample for To and from, but here these are users in system.
"email_activity_parties" : [
{
"partyid_systemuser#odata.bind" : "/systemusers(CED2E02D-188E-4AA8-B6E2-D746E9B370C1)",
"participationtypemask" : 1 ///From Email
},
{
"partyid_account#odata.bind" : "/accounts(69C38067-EDB7-E811-A961-000D3A363C81)",
"participationtypemask" : 2 ///To Email
}]
Creating Email with unresolved emails (To field of email is not record in MS CRM).
"email_activity_parties" : [
{
"partyid_systemuser#odata.bind" : "/systemusers(CED2E02D-188E-4AA8-B6E2-D746E9B370C1)",
"participationtypemask" : 1 ///From Email
},
{
"addressused":"vvyas#cloudfronts.com",
"participationtypemask" : 2 ///To Email
}
]
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 can send a message correctly , but I have a problem with variable subject , how I can I send the variable $subject into Mail method.
->subject($subject)
I recieve an undefined variable , this is my code.
public function enviarmensaje()
{
$email = Request::input('email');
$subject= Request::input('subject');
$name = Request::input('nombre');
$message = Request::input('message');
$data = array( 'name' => $name , 'correo' => $email , 'mensaje' => $message,'subject'=> $subject );
Mail::send('contact', $data , function ($message) {
$message->to('email#gmail.com','To anyone')->subject("Contact form");
});
}
You have to use the $subject variable:
Mail::send('contact', $data , function ($message) use ($subject) {
$message->to('email#gmail.com', 'To anyone')->subject($subject);
});
Alternatively, you can call Request::input('subject') from within the closure itself:
Mail::send('contact', $data , function ($message) {
$message->to('email#gmail.com', 'To anyone');
$message->subject(Request::input('subject'));
});
This is my html template
Dear ##name##(##email##),
Thank you for contacting us.
I want to replace ##name## and ##email## with the receiver's name and email of the person who gets it which will be provided in the array. How do I do it?
This is what I've got so far
$to_email = array('a#example.com', 'b#example.com', 'c#example.com');
$to_name = array('apple', 'ball', 'cat');
$Email = new CakeEmail();
$Email->from($from);
$Email->to($to_email );
$Email->subject($subject);
$Email->emailFormat('html');
$Email->viewVars(array('data' => $body));
$Email->template('bulk');
$Email->send();
You should start with creating template for your email, which will be including your current content (I use name example_template.ctp in my samples below):
Dear <?php echo $name; ?> <?php echo $email; ?>,
Thank you for contacting us.
Then you have to modify way of setting up your viewVars() and template():
$Email->viewVars(array('email' => $email, 'name' => $name));
$Email->template('example_template');
There is also required to change way of sending emails to loop over emails instead of sending all recipients in one field. So combine your input arrays into one, e.g.:
$emails = array(
'a#example.com' => 'apple',
'b#example.com' => 'ball',
'c#example.com' => 'cat'
);
Then just foreach over your array and send mails:
$Email = new CakeEmail();
foreach ($emails as $email => $name) {
$Email->from($from);
$Email->to($email);
$Email->subject($subject);
$Email->emailFormat('html');
$Email->viewVars(array('email' => $email, 'name' => $name));
$Email->template('example_template');
$Email->send();
$Email->reset(); // for cleaning up CakeEmail object
}
I am trying to send an email with mailguns PHP api:
define('MAILGUN_KEY', 'key-ExamPle3xAMPle');
define('MAILGUN_DOMAIN', 'example.com');
$mailgun = new Mailgun\Mailgun(MAILGUN_KEY);
$mailgun->sendMessage(MAILGUN_DOMAIN, [
'from' => 'noreply#signstoptt.com',
'to' => $email,
'subject' => 'Sign Stop mailing list confirmation.',
'html' => "
Hello{$name},</br></br>
This is a test."
]);
I have even tried to use array() instead of [ ].
I receive the following error in my php error log:
MissingRequiredParameters
It implies that what I am passing to the post function is incomplete or incorrect. upon inspecting the post function in the RestClient, I see that the function requires 2 arrays and not 1, so I tried adding a 2nd array with message attachments and it just got more errors, this time with guzzle (a dependency for mailgun)
[26-Jan-2015 14:32:50 UTC] PHP Fatal error: Uncaught exception 'Mailgun\Connection\Exceptions\MissingRequiredParameters' with message 'The parameters passed to the API were invalid. Check your inputs!' in C:\Users\Zachary\Documents\NetBeansProjects\SS_MailingList\vendor\mailgun\mailgun-php\src\Mailgun\Connection\RestClient.php:187
Stack trace:
#0 C:\Users\Zachary\Documents\NetBeansProjects\SS_MailingList\vendor\mailgun\mailgun-php\src\Mailgun\Connection\RestClient.php(116): Mailgun\Connection\RestClient->responseHandler(Object(Guzzle\Http\Message\Response))
#1 C:\Users\Zachary\Documents\NetBeansProjects\SS_MailingList\vendor\mailgun\mailgun-php\src\Mailgun\Mailgun.php(106): Mailgun\Connection\RestClient->post('signstoptt.com/...', Array, Array)
#2 C:\Users\Zachary\Documents\NetBeansProjects\SS_MailingList\vendor\mailgun\mailgun-php\src\Mailgun\Mailgun.php(53): Mailgun\Mailgun->post('signstoptt.com/...', Array, Array)
#3 C:\Users\Zachary\Documents\NetBeansProjects\SS_MailingList\subscribe.php(26): Mailgun\Mailgun->sendMessage('signstoptt.com', Array)
#4 in C:\Users\Zachary\Documents\NetBeansProjects\SS_MailingList\vendor\mailgun\mailgun-php\src\Mailgun\Connection\RestClient.php on line 187
Has anyone else had this problem. I am running the site on a glassfish server setup by netbeans. I also used composer to install mailgun and its dependencies.
EDIT: Added more information.
init.php
<?php
require_once 'vendor/autoload.php';
define('MAILGUN_KEY', 'key-854743a7e');
define('MAILGUN_PUBKEY', 'pubkey-b00e47d7');
define('MAILGUN_DOMAIN', 'example.com');
define('MAILGUN_LIST', 'customers#example.com');
define('MAILGUN_SECRET','xjhbJH7');
$mailgun = new Mailgun\Mailgun(MAILGUN_KEY);
$mailgunValidate = new Mailgun\Mailgun(MAILGUN_PUBKEY);
$mailgunOptIn = $mailgun->OptInHandler();
subscribe.php
<?php
require_once 'init.php';
if(isset($_POST['name'], $_POST['email']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$validate = $mailgunValidate->get('address/validate', [
'address' => $email
])->http_response_body;
if($validate->is_valid)
{
$hash = $mailgunOptIn->generateHash(MAILGUN_LIST, MAILGUN_SECRET, $email);
$result = $mailgun->sendMessage(MAILGUN_DOMAIN, [
'from' => 'noreply#example.com',
'to' => $email,
'subject' => 'example mailing list confirmation.',
'html' => "
Hello{$name},</br></br>
You submitted a request to join our mailing list, to confirm this subscription please click on the link provided below.</br></br>
http://localhost:8000/confirm.php?hash={$hash}"
]);
$mailgun->post('lists/' . MAILGUN_LIST . '/members', [
'name' => $name,
'address' => $email,
'subscribed' => 'no'
]);
header('Location: ./');
}
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Subscribe | Mailing list</title>
</head>
<body>
<div class="container">
<form action="subscribe.php" method="post">
<div class="field">
<label>
Name
<input type="text" name="name" autocomplete="off">
</label>
</div>
<div class="field">
<label>
Email
<input type="text" name="email" autocomplete="off">
</label>
</div>
<input type="submit" value="Subscribe" class="button">
</form>
</div>
</body>
</html>
You forgot the text key which is used when html isn't available by the mail client.
Your code will look like
define('MAILGUN_KEY', 'key-ExamPle3xAMPle');
define('MAILGUN_DOMAIN', 'example.com');
$mailgun = new Mailgun\Mailgun(MAILGUN_KEY);
$mailgun->sendMessage(MAILGUN_DOMAIN, [
'from' => 'noreply#signstoptt.com',
'to' => $email,
'subject' => 'Sign Stop mailing list confirmation.',
'text' => 'Hello ' . $name . ', this is a test.',
'html' => '
Hello ' . $name . ',</br></br>
This is a test.'
]);
By the way; I recommend always using single quotes or double quotes for readability.