Store send e-mail in database - php

I am currently building a support ticket platform in PHP language. (Laravel framework to be exact)
I would like to have the feature that customers can e-mail to a certain e-mail address and that the e-mail gets stored in our database as a ticket itself. ( Or at least calls a url or something with postdata )
How would I go about forwarding e-mails to a PHP url/script or something, can someone get me on track?

You could possibly use the ImapMailbox PHP library to connect to an email inbox, grab the message content, store the data in your database, then delete the email from the inbox.
Alternatively, you could use an external service like Postmark to receive inbound mail and send your server a webhook for processing in PHP.
Hope this helps.

You can send information to server and save message to the db before it will sends. You don't need to post it back to your server, here is some abstract code:
public function store(Request $request)
{
$message = $request->get('message');
$to = $request->get('to');
$user = Auth::user();
Ticket::create(['message' => $message, 'to' => $to->id, 'by' => $user->id]);
Mail::send('emails.ticket', $message, function($m) use ($to,$user){
$m->from('app#example.com', 'Your Application');
$m->to($to->email, $to->name)->subject('Email from user '. $user->name);
});
}

Related

Using Laravel 5, how can I find out if a Mailgun email was sent successfully?

I'm using the native Mailgun driver in Laravel 5 to send an email
\Mail::send('emails.notification', $data, function($message) use ($data)
{
$message->to('name#gmail.com', 'Joe Bob')->subject("Headline here");
});
That works well and the emails are being received, but I would like to know how I can get a response from Mailgun letting me know that the email was sent.
How can I go about getting that information?
To know the status of the email you can use Mailgun webhooks. From the Mailgun admin area you can specify a webhook url that Mailgun will post to after they have processed your email.
Ensure that whatever url you use as a webhook exists in your application and has a route and method in whichever controller you are using. Once your application receives the posted data you can parse the data sent and determine the status of the email. You can then update your database or whatever you need to do.
To identify which email you will need to add some custom headers in the email so you can identify it later on.
see this answer for more details on how to do that: Using Laravel's Mailgun driver, how do you (gracefully) send custom data and tags with your email?
The webhook url needs to be live for it to work, so if you are testing locally with laravell homestead you can take a look at ngrok, which allows you to tunnel your local environment to a url which will work for testing purposes.
I figured out the solution and it's actually quite simple.
With Laravel (Using the Bogardo Mailgun Package):
$to_name = "Jim Bob";
$to_email = "jimbob#gmail.com";
$subject = "Howdy everyone!";
// Send the email and capture the response from the Mailgun server
$response = Mailgun::send('emails.transactional', array('body' => $body), function($message) use($to_email, $to_name, $subject)
{
$message->to($to_email, $to_name)->subject($subject);
});
// HTTP Response Code 200 means everything worked as expected
if ($response->http_response_code === 200) {
echo "Woohoo! The message sent!";
}
In plain ol' PHP (Using the official Mailgun PHP SDK):
// Config Information
$mailgun = new Mailgun("key-v876876dfsv876csd8768d876cfsd4");
$domain = "mg.mydomain.com";
// Prepare the necessary information to send the email
$send_data = array(
'from' => $from_name . ' <' . $from_email . '>',
'to' => $to_name . ' <' . $to_email . '>',
'subject' => $subject,
'html' => $html
);
// Send the email and capture the response from the Mailgun server
$response = $mailgun->sendMessage($domain, $send_data);
// HTTP Response Code 200 means everything worked as expected
if ($response->http_response_code === 200) {
echo "Woohoo! The message sent!";
}
Check out a list of all Mailgun HTTP Response Codes: (https://documentation.mailgun.com/api-intro.html?highlight=401#errors)

How to change header information of mail in Laravel?

I am creating a simple contact us form using Laravel 5.1
public function sendMessage(){
$data = [
'name' => Input::get('name'),
'email' => Input::get('email'),
'subject' => Input::get('subject'),
'body' => Input::get('body')
];
Mail::send('emails.contact',$data, function($message) use($data){
$message->from($data['email'], $data['name']);
$message->to('smartrahat#gmail.com','Mohammed');
$message->subject($data['subject']);
});
Session::flash('success_message','Mail sent successfully!');
return redirect('contact');
}
Everything is working fine but the sender email address is not the one it get from the contact page. The email is sent from the address which I configure in .env
I want to have the email from the sender email address which he filled up contact form. Or, you can say I want to change the header information (only from, I can change other information).
Well Laravel will send the mail through the given smtp server. I guess the smtp server (e.g. google doing this) will not let you change your from address to another address then the account belongs to.
If you want to reply to this address directly in your email programm you can add $message->replyTo($data['email'], $data['name']);.
The $message variable inside the Mail::send() function is a SwiftMailer message instance. Therefore, you can work with headers just like the SwiftMailer documentation shows. Just use $message->getSwiftMessage() to get and manipulate the headers.
Also, you absolutely can change the From header, but if you change it to a different domain, you'll have to deal with phishing warnings in the client. You can resolve that by setting up DKIM and SPF records, and you will need to have access to the DNS settings for the domain to do that.

CakePHP and masking email sender

I am currently using a function called send:
public function send(){
if ( !empty($this->request->data) ) {
$email = new CakeEmail('default');
$email->from(array($this->Auth->user('email') => $this->Auth->user('username')))
->to(array('helpdesk#example.com'))
->subject($this->request->data['Ticket']['subject'])
->send(array($this->request->data['Ticket']['issue']));
$this->Session->setFlash('Email Sent Successfully', 'default', array('class' => 'message update span9'));
$this->redirect(array('action' => 'index'));
}
to Send emails to our helpdesk and deposit them into their database. All is working EXCEPT the FROM always shows the username/email address from the configuration options. It is not masking the email with the users email.. I need this to happen so that we know who is having the support issue.
Does anyone have a suggestion here on what to do?
*Addition
This is an intranet application and thus we have an authenticated GENERIC USER using smtp settings. This is not spamming, we just want to know which user the Help Desk ticket came from when inserting to the DB.
Why are you using the Config default anyway?
If you use $email = new CakeEmail();, does the email sent references the Authenticated User email info.
Also, you should always use $email->sender('support#yourcompany.com', 'Your Company Support');. This ensures that if there is an issue the problem get redirected to you and not the user, your app is sending an email on his/her behalf.
I have that setup in my account and it works just fine. To Mark's point, it may not be legal (although, that does not seem to be your issue), but I know it is possible as I have currently a system setup that works with whatever email I want. I do not use any Config and also I do not use any SMTP

Multi functional email set up with Code igniter

I have an application built in CodeIgniter. I needed some help with the following email related tasks. Please note all emails will require SSL.
1) Send email to congratulate and welcome the user to the site
2) Send an email to confirm a user account has been deleted should they choose to leave.
3) Send an email to alert the user for a request sent to them from another user.
4) Set up and send an email for "forgot username" and last but not not least
5) Send an email to reset password in case the user can't remember how to login.
Thanks for your help, appreciate it.
function signup(){
$data = array(
'sign_up_mail'=>'Welcome and thanks for joining...'
);
$htmlMessage = $this->parser->parse('user/email/signup_html', $data, true);
$txtMessage = $this->parser->parse('user/email/signup_txt', $data, true);
#send the message
$this->email->from('test#gmail.com', 'test app');
$this->email->to($this->input->post('email_address'));
$this->email->subject('Account Registration');
$this->email->message($htmlMessage);
$this->email->alt_message($txtMessage);
$this->email->send();
}
Would you use this listed below as the method for changing the message within the various emails?
data['message'] = "Hey there, you've got a follower request!";
$email = $this->load->view('email/template', $data, TRUE);
I presume this method works for simple things like user welcome and alerts etc. How would I go about connecting a process to resetting a username/password or confirming a deletion? How do you connect the email process with manipulating data in the db?
From your question, I think you are looking for some kind of authentication related tasks. If so, there is an authentication, library in codeigniter, that provides complete authentication system. Have a look, it it is useful to you. http://konyukhov.com/soft/tank_auth/

E-mail sent via CakePHP shows up with blank message body when accessed via POP

I'm writing a small CakePHP application for an organization and have included a simple contact form that accepts an email address, subject, and message, and emails the message to the address.
Everything seems to work fine, and any email sent to myself or anyone at the organization arrives just fine, except if they access the message via POP, which most of them do. In this case, the email arrives with the subject, but the body is blank. The body shows up just fine, however, if the message is read via the webmail client.
Has anyone else run into this problem? Is it an issue with CakePHP, the email headers, or should I be talking to the hosting company? My code is based directly on the example given in the CakePHP documentation.
Here's the controller action that receives the request data and sends the email:
function send() {
if (!empty($this->data)) {
$contact = $this->Contact->read(null, $this->data['ContactMessage']['contact_id']);
$this->data['ContactMessage']['ip'] = $this->RequestHandler->getClientIp();
$this->ContactMessage->create();
if ($this->ContactMessage->save($this->data)) {
$this->Email->to = $contact['Contact']['email'];
$this->Email->subject = $this->data['ContactMessage']['subject'];
$this->Email->replyTo = $this->data['ContactMessage']['email'];
$this->Email->from = $this->data['ContactMessage']['email'];
$this->Email->sendAs = 'both';
$this->Email->send($this->data['ContactMessage']['message']);
$this->redirect(array('controller' => 'contacts', 'action' => 'thanks'));
} else {
$this->redirect(array('controller' => 'contacts', 'action' => 'oops'));
}
}
}
I think the problem is that you cant use send() to send both an html/text message. When you pass the message to send() that sends a basic text message, if you want to send an html message, you must set the data to the template.
http://book.cakephp.org/view/269/Sending-a-basic-message#Controller-273

Categories