Yii framework SMTP mail with gmail from email id not changed - php

I am using this extension.
http://www.yiiframework.com/extension/smtp-mail
I would work properly. But From Email id in mail not set that i have defined in "SetFrom()" function but it takes Gmail username (myemail#gmail.com).
Below is my code for sending mail in my Controller.
$mail = Yii::app()->Smtpmail;
$mail->SetFrom("otherid",$from_name); // This id not coming in my response mail
$mail->Subject = $subject;
$mail->MsgHTML($message);
$mail->AddAddress($to, "");
config/main.php
'Smtpmail'=>array(
'class' => 'application.extensions.smtpmail.PHPMailer',
'Host' => "smtp.gmail.com",
'Username' => 'myemail#gmail.com',
'Password' => 'password',
'Mailer' => 'smtp',
'Port' => 465,
'SMTPAuth' => true,
'SMTPSecure' => 'ssl'
),

Gmail doesn't seem to allow sending the email via a different email-id if its not registered to the primary gmail account.
When logged in to the primary gmail account -> go to Account tab (top-right).
Search for Add another email address under Send mail as.
Add the new email address there. Confirm the link sent to the additional email account.
On confirmation, your account can send emails from either of the address.
Try the new email address now in $mail->SetFrom("new_gmail_id#gmail.com", $from_name); and it will work fine.
I have tried that and it works. Let us know if you have done all this already.

Related

Add 'From' Header on email using Codeigniter

I'm working with Codeigniter in CPanel and my code already sends a mail, but when it gets to the receiver, the hostname is shown on the sender.
I tried some answer to questions as : Change the sender name php mail instead of sitename#hostname.com but in Codeigniter, they don't work.
This is my code:
$config = Array(
'protocol' => 'ssmtp',
'smtp_host' => 'ssl://ssmtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'mail#domainiwant.com',
'smtp_pass' => 'password',
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'useragent' => 'MY NAME',
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('mail#domainiwant.com', 'MY NAME');
$email_to = 'receiver#gmail.com';
$this->email->to($email_to);
$this->email->message('Message testing ...');
$this->email->send();
However, as I said, when the mail gets to the receiver, they appear with the hostname and a completely different mail address like the one I put on $config
I know this only sets the envelope sender but I want to set the mail address to be mail#domainiwant.com instead of receiving the mail with somemail#host.com.ex
According to the documentation of CodeIgniter's email library available HERE, your whole problem is a simple typo.
$config['protocol'] allows mail, sendmail and smtp as values. If you don't set the variable, or use a value which is not allowed, the whole library defaults to mail which attempts to use your own server as the mail gateway (which explains why your sender address shows as username#servername)
Change the protocol from ssmtp to smtp so that you actually use the Google SMTP server you intend to use and you'll get the results you expect

"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.

Sending mail to a gmail account via codeigniter fails

I tried to send a email to a Gmail account (both receiver and sender accounts were created by me and both allow access to less secured apps) via CodeIgniter, using the following settings (from my localhost WAMP sever):
$config['smtp_user']='my gmail address';
$config['smtp_pass']='my password';
$config['smtp_port']=465;
$config['smtp_host']='ssl://smtp.googlemail.com';
$config['charset']='utf-8';
$config['protocol'] ='smtp';
The code snippet looks like this :
$this->email->from(my gmail address... , 'admin');
$this->email->to($reciever_email);
$this->email->subject(...);
$this->email->message(...);
$this->email->send();
But the mail does not get send (No weird error messages like fatal error etc. is displayed only the custom message I decided to show on failure is shown).
Can any one tell me why and how to get rid of this problem.
Try this instead:
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => '465',
'smtp_user' => 'my user',
'smtp_pass' => 'my password',
'smtp_charset' => 'utf-8'
);

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

SMTP Authentication in Zend Framework

Here's a scenario; I want to send email using PHP and Zend Framework. Here's an example on how this is usually done:
$smtp = new \Zend_Mail_Transport_Smtp(array(
'host'=> 'localhost'
, 'auth' => 'login'
, 'ssl' => 'TLS'
, 'username' => 'john'
, 'password' => '123'));
\Zend_Mail::setDefaultTransport($smtp);
$mail = new \Zend_Mail();
$mail->addTo('jane.smith#localhost', 'Jane Smith');
$mail->setSubject('Greetings');
$mail->setBodyText('Hi there');
$mail->setFrom('john.smith#localhost', 'John Smith');
$mail->send();
My question is, how can we make sure that the from address (in this case 'john.smith#localhost') actually exists and it belongs to the username and password provided to SMTP connection? Because if it is not, then I can send email on behalf of anyone!
[UPDATE]
I believe I found a partial answer to my own question. It should be done through IMAP / POP3 protocols. But yet these two protocols take in a username and password in order to athenticate and so you can not check for the association between the provided username and an email address. So the question is how to authenticate an email address through IMAP / POP3 in PHP and Zend Framework?
SMTP does allow to send emails as anyone. This is why most security-oriented people promote the use of digital signatures.
Validation can be done by the SMTP itself. Gmail's SMTP will refuse to send emails with an address not attached to the account for example.

Categories