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.
Related
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);
});
}
I am using Mailgun to handle email in my Laravel 5.1 application. There are times when I need to email a large number of users at once, such as internal group emails and alert notices. So far, I have created an array of recipient email addresses, sent the email to a webmaster type address, and included the end recipients in BCC:
$recipients = [];
foreach (User::emailRecipients()->get() as $user)
{
$recipients[] = $user->email;
}
$data['message'] = "Hello World";
$data['recipients'] = $recipients;
Mail::send('emails.group-email', $data, function($message) use ($data) {
$message->to('support#demo.com')
->bcc($data['recipients'])
->subject('Test message');
});
While this works, it's not ideal. For starters, I cannot reference anything unique to each recipient within the email (such as a custom unsubscribe link). I cannot use some sort of mailing list on an email provider because the recipients will never be the same, it all depends on several factors.
The next logical step is to iterate over each email address and use Mail::send on each iteration. But would this cause any performance issues or API restrictions with Mailgun? It's possible that one email could be sent to approximately 200 recipients.
Rather than using Laravel's built in Mail, I elected to use Mailgun's API (specifically batch sending) directly - mailgun/mailgun-php
$mailgun = new Mailgun('API-KEY');
$recipientVariables = [
"someone#demo.com" => ['name' => 'Benny'],
"someoneelse#demo.com" => ['name' => 'James']
];
$mailgun->sendMessage('demo.org', [
"from" => 'support#demo.org',
"to" => 'someone#demo.com, someoneelse#demo.com',
"subject" => 'Cool Email',
"html" => $content, // HTML from Blade template
"text" => "Plain text message",
"recipient-variables" => json_encode($recipientVariables)
]);
This also allows me to access unique recipient variables within my email template, like so:
<h1>Hello, %recipient.name%</h1>
I was already passing my Blade template through a CSS inliner, which turns it into plain HTML, so this works perfectly.
I have configured my mail.php config file to send emails using the gmail account.
Everything looks great, but when I try to send email from another account (not the address used in the mail.php config file) I get an email from the address set on the configuration file.
Mail::send('emails.contact_mail', array(
'email' => $fromEmail,
'name' => $user,
'subject' => $subject,
'message' => $message_content,
), function($message)use ($fromEmail,$user,$subject) {
$message->to('s111#gmail.com');
$message->from('test#gmail.com', $user);
$message->subject($subject);
});
It seems like the $message->from doesn't work. I get on the s111#gmail.com an email from the address set in mail.php file and not from test#gmail.com.
Is there any solution? How to change the from address?
You will still use the SMTP servers set in your config file. Setting another 'from' header is theoretically possible, but the GMail SMTP will ignore it unless you set an address that has been added to your Google account.
Solution: either don't use another 'from' address, or add that address to your Google account.
There are a couple things you can try:
$message->replyTo('test#gmail.com', $user);
And to my understanding, the "From: " header in emails is what should determine what it will display as the sender. This may work for changing the "From" header:
$message->getHeaders()->addTextHeader('From', 'Your Name <test#gmail.com>');
I think this is best solution for this question.
We can figure out this as follows:
$message->from('sender#email.com', 'sender_name');
And also we should remove the following code block in config/mail.php.
'from' => [
'address' => 'sender#email.com',
'name' => 'sender_name',
],
of course, this will be working on Laravel Framework.
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
I have the following code which sets up the SMTP server:
ini_set("send_from", "test#gmail.com");
ini_set("SMTP", "smtp.gmail.com");
and i create a simple mail in this way:
mail("test#yahoo.com", "A subject", "My message for you", "From: TEST");
When I run this code, it fails to send mail to Yahoo e.g. some.email#yahoo.com. But when i use any Gmail mail address as the first argument, it works.
What's wrong ?
To send mail as an authenticated user you should use email authentication methods like SPF, DKIM etc.
Also you need to make sure your domain should point to your IP address and IP address MUST point to same domain. This is called Reverse DNS
Other good practice that prevents mails from going into spam folder are
Make sure you have a unsubscribe link
Make sure the Reply-To header is added and the email used here is a valid email.
Add a Name in the To field. Like First Last <email#example.com>
Add a postal address of the company you are mailing from which must include a phone number.
There was a form to white list email senders IP for yahoo. Now I dont find it. So try the above things, It should work well.
In thi case you dont auth (user name passwort) and dont usw tls. This wont be accepted.
Better use this:
XAMPP Sendmail using Gmail account
or an framework to send emails via smtp like
Zend Mail Gmail SMTP
http://framework.zend.com/manual/1.12/en/zend.mail.sending.html
Here a code example
http://framework.zend.com/downloads/latest#ZF1
require('Zend/Mail.php');
$config = array(
'ssl' => 'tls',
'port' => 587,
'auth' => 'login',
'username' => 'your_gmail_address#gmail.com',
'password' => 'password'
);
$smtpConnection = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config);
Zend_Mail::setDefaultTransport($smtpConnection);
Zend_Mail::setDefaultFrom('your_gmail_address#gmail.com', 'Your real name');
$mail = new Zend_Mail();
$mail->addTo('any_address#yahoo.com', 'Test');
$mail->setSubject(
'Demonstration - Sending Mails per SMTP Connection'
);
$mail->setBodyText('...Your message here...');
$mail->send($smtpConnection);