Send email from address with laravel - php

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.

Related

Mailgun cannot send email to cc

I'm using mailgun to sending email, but I'm not able to send more emails (if I duplicate the code below and add change 'to' variable) or I am not able to send cc. If I remove 'cc' from the code, everything works fine, oherwise it throws error pasted below:
$result = $mgClient->sendMessage($domain, array(
'from' => 'Me <me#domain.com>',
'to' => $_POST["customer"]["email"]),
'cc' => 'me#domain.com',
'subject' => 'Confirmation – domain.com',
'html' => $text
));
error:
Fatal error: Uncaught Mailgun\Connection\Exceptions\MissingRequiredParameters: The parameters passed to the API were invalid. Check your inputs! The domain is unverified and requires DNS configuration. Log in to your control panel to view required DNS records.
There is no problem with your code.
The error is for an unverified domain name. You used domain.com as domain, make sure this domain name is verified and valid.
for more read this

Cakephp email Encoding subject gives wrong characters

I'm sending emails with cakephp and I have been testing it with the followign website:
http://spamcheck.postmarkapp.com/
I have had several problems, which one of them was the encoding of the subject.
Right now, that one is solved, following some solutions I've found in the internet. First line of the code is what i'm doing with the subject
Here's my code:
$newsubject='=?UTF-8?B?'.base64_encode($subject).'?=';
$email = new Email('aws');
$email->from(['xxxx#zzzz.zz' => 'test'])
->template('default','confirmation')
->viewVars([
'user_email' => $emailTo,
])
->emailFormat('both')
->to($emailTo)
->subject($newsubject)
->replyTo('support#uphill.pt')
->helpers(['Html', 'Text'])
//->attachments($attachment->path)
->send($message);
After I recieve the email, the subject shows: "=?UTF-8?B?SW5zY3Jpw6fDo28gbm8gZXZlbnRvOiBOYXRpb25hbCBDb25mZXJlbmNlIG9uIEh1bWFuIFBhcGlsbG9tYSBWaXJ1cw==?="
What am I missing?
EDIT:
I'm using Cakephp 3.3 and here's my aws email transporter config
'aws' => [
'transport' => 'aws',
'from' => 'xxxx#zzzzz.z',
'charset' => 'utf-8',
'headerCharset' => 'utf-8',
]
Here's my email:
http://pastie.org/private/hzcicqrlzx425ucanxyl5a
Thanks
About the result :
You encoded the email Subject text with base64.
So, you get the encoded text from your Subject plain text.
You should use email Subject as string/plain text. Mail services doesn't decode your custom encode type automatically.
Change your Subject string :
$newsubject='=?UTF-8?B?'.base64_encode($subject).'?=';
#TO
$newsubject=$subject;
Search by typing EmailTransport or Email from config/app.php and check your email configuration is correctly configured.
Her is the details about CakePHP 3.x Email

"From Email" doesn't work in laravel 5.3

This is my code:
Mail::send('view',$dataView, function($message) use ($user)
{
$message->from('my_email#gmail.com', 'Myname');
$message->subject('This is title');
$message->to(sender_email#gmail.com, $user->user_username);
});
It works! But When I check sender_email#gmail.com then I see "from email" which I config in .env ( MAIL_USERNAME ), it isn't "from email" in code (my_email#gmail.com), How to I can change it to my_email#gmail.com? Thanks and sorry about my english.
In config/mail.php, around line 58, try changing:
'from' => [
'address' => 'hello#example.com',
'name' => 'Example',
],
to:
'from' => [
'address' => env('MAIL_USERNAME'),
'name' => env('MAIL_USERNAME')
],
You are trying to send emails via dynamic senders which is not allowed by Gmail for preventing spamming mails, so automatically Gmail will change your sender address to your default address.
I guess you're misunderstanding the MAIL_USERNAME which is in your .env file.
So basically, Laravel provides simple solution to send emails via dynamic senders. Lets say you've signed up for MailGun or SendGrid to send mails and MAIL_USERNAME is the username for your mail provider not your sender mail address. (Not Gmail, as Gmail doesn't support dynamic senders. Its good if you're testing your mails.).
so, you're .env will look like this.
MAIL_DRIVER=smtp
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME=sendgridUsername
MAIL_PASSWORD=sendgridPassword
MAIL_ENCRYPTION=tls
Now, you're eligible to send mails using sendgrid. Now Lets say your domain is example.com so you can use admin#example.com support#example or any email address with your domain to send emails.
To Receive messages either you use webmail or Gmail.
Hope this helps.

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.

Cannot send mail to yahoo (any address) using PHP code and SMTP server

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);

Categories