Sending SMTP email from PHP Script hosted on Hostgator Servers - php

I have created a web application as such in the application I require to send to send users their passwords if they have forgotten them. Now I use a gmail account to send the email. When I send the email locally from my machine using XAMPP everything works fine and it delivers as expected. When I look to put the php script onto a Hostgator server and try send the user their password I can't. But the reason I think this is happening is because Gmail immediately send me the following:
Someone recently used your password to try to sign in to your Google Account myemail#gmail.com. This person was using an application such as an email client or mobile device.
We prevented the sign-in attempt in case this was a hijacker trying to access your account. Please review the details of the sign-in attempt:
Tuesday, January 21, 2014 1:42:56 PM UTC
IP Address: 198.57.247.245 (gator3281.hostgator.com.)
Location: Los Angeles, CA, USA
If you do not recognize this sign-in attempt, someone else might be trying to access your account. You should sign in to your account and reset your password immediately
Based on this email I would assume that Gmail is touchy that hostgator is trying to send an email via them. My problem is I don't know how to fix this problem (This is my first time doing something like this) As such I was using a PHP framework called codeigniter and here is the code used to send the email (Note this code works more than fine locally i.e. I dont think there is anything wrong with the code):
public function SendEmailValidate($email,$subject,$message,$type)
{
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'myemail#gmail.com',
'smtp_pass' => 'mypassword',
'smtp_timeout' => 30,
'mailtype' => $type
);
$CI = &get_instance();
$CI->load->library('email',$config);
$CI->email->set_newline("\r\n");
$CI->email->from('myemail#gmail.com','Book Bay');
$CI->email->to($email);
$CI->email->subject($subject);
$CI->email->message($message);
if($CI->email->send())
{
return true;
}
else
{
return false;
}
}
Any help on this matter would really help, thanks

its in your gmail settings; you need to allow your website to send emails;
to do so go to https://accounts.google.com/DisplayUnlockCaptcha and click continue; then useyour website to send email an email; and google will detect your login attempt and allow you.

Related

Delay due to email send PHP CodeIgniter

First of all, I would like to thank StackOVerflow and its users for helping me solve a number of technical problems. I created this account today since I could not see similar problem being addressed.
The problem - I am stuck with a problem with sending emails in PHP CodeIgniter. The function $this->email->send() is taking around 3-4 seconds each time to execute.
We have a social platform where users come and post things. Every time the user uploads a new update we want to send an email to him/her.
The problem is the send email function takes around 3-4 seconds and we would love to bring it down under 1 second.
IS there any way wherein we can execute the send email process in parallel ? Say a python code which runs continuously to fetch new updates and send emails to those users. Any better method than that ?
Technology stack - PHP CI, MySQL, Apache, Windows
This is the email code that gets called on every new update -
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'smtp.gmail.com',
'smtp_port' => 587,
'smtp_user' => 'mailid#gmail.com',
'smtp_pass' => 'password',
'mailtype' => 'html',
'charset' => 'utf-8',
'smtp_crypto' => "tls"
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->set_mailtype("html");
$this->email->from('mailid#gmail.com', 'Support');
$this->email->to($this->session->userdata['email']);
$this->email->subject('You have posted a new update');
$this->email->message('Please login to check your new update. Do not reply to this email. For any issues reach out to email#emailid.com');
if (!$this->email->send())
{
show_error($this->email->print_debugger());
}
else
{
echo true;
}
If your function is working faster but email sending is slower, it must be problem for your smtp connection. Either your server is not much faster to connect SMTP quickly or the SMTP you are using bit slower to response.
In that case, try with a gmail SMTP and see the result.
It's my suggestion to store the emails in a database and send the emails using a cron job.
The uploading user should not wait to send emails one at a time .
If you need code for a cron job or an smtp mailer, add a comment to this answer.

How to avoid storing mail account and database password in plain text in codeigniter 3

I have created a codeigniter 3 helper to send mails from different modules of application. I am using my gmail account to send all the mails and it works perfectly fine except that i have to store my gmail password inside the helper php file.
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'saurabh#gmail.com',
'smtp_pass' => 'my password',
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$result = $this->email->send();
How can i avoid storing the plain text password in the above code while still able to use my gmail account. Same is true for my database password, I am storing plaintext database password in the codeigniter's database.php configuration file. How this can be avoided ?
Database password cannot be hidden or connvert it in other way. But how ever no one can access your config files with the browser.
And in email password can be done. You can keep it in database and on mail sending place you can call it.
FYI - your project files cant access with browser or any other. Thats the reason we use frameworks. Bcz its security level is higher than normal oop pograms.
So your password and other stuff will not be public at any how.
The only way I see it possible is to use Gmails OAUTH mechanism instead of smtp method. here's the link to the api https://developers.google.com/identity/protocols/OAuth2 and some examples https://developers.google.com/gmail/xoauth2_libraries#php_sample. Rest assured you will have to put your plain password somewhere in your app (either in DB or at a location in file system which no one has access to except apache) or write a crypt function (even this function requires/ outputs the plain password)

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

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

Can't send email via SMTP because "550 - Relay Not permitted"

I'm using CakePHP to send automated emails to clients. It's been working great, but it seems some recipients aren't receiving our emails. So I decided to use the SMTP option for sending emails, and route emails through our email provider at Media Temple.
However, when trying to send email from a Media Temple account, I get the error "550- relay not permitted".
That sounds like the Media Temple server is just plain not allowing me to send mail through it.
That's odd because I've confirmed the username and password I'm using is correct and I can send mail via SMTP through it from my macmail client and iPhone mail client. I've also confirmed my cakephp email settings are correct, because I can send emails via SMTP with a gmail account with the exact same configuration in cakephp.
Any idea why I'm getting this error and how to resolve it?
Thanks
Here's the code that handles sending an email. I use this class just like the regular EmailComponent from within many different controllers.
class CanadafindsEmailerComponent extends EmailComponent
{
...
function send($content = null, $template = null, $layout = null) {
if(!in_array(TECHY_MONITOR_EMAIL,$this->bcc) && is_array($this->bcc))
$this->bcc[]=TECHY_MONITOR_EMAIL;
else if (!in_array(TECHY_MONITOR_EMAIL,$this->bcc) && !is_array($this->bcc))
$this->bcc=array(TECHY_MONITOR_EMAIL);
if(DEVSITE){//commented-out code are settings for smtp with gmail, which works fine
$this->delivery = 'smtp';
$this->smtpOptions = array(
'port'=>'465',//'465',
'timeout'=>'30',//'30',
'auth' => true,
'host' => 'ssl://mail.thenumber.biz',//'ssl://smtp.gmail.com',
'username'=>USERNAME,//'USERNAME#gmail.com',
'password'=>SMTP_PASSWORD//,
);
$this->to=$this->correctFormatOn($this->to);
$this->bcc=$this->correctFormatOn($this->bcc);
$this->cc=$this->correctFormatOn($this->cc);
$this->replyTo=$this->correctFormatOn($this->replyTo);
$this->from=$this->correctFormatOn($this->from);
}
return parent::send($content,$template,$layout);
}
function correctFormatOn(&$email){
if(is_array($email)){
$copiedEmail=array();
foreach($email as $singleEmail){
$copiedEmail[]=$this->correctFormatOnSingle($singleEmail);
}
$email=$copiedEmail;
}else{
$email=$this->correctFormatOnSingle($email);
}
return $email;
}
function correctFormatOnSingle(&$email){
$subEmails=explode(",",$email);
$fixedSubEmails=array();
foreach($subEmails as $subEmail){
$fixedSubEmails[]=preg_replace('/<?([^< ]+)#([^>,]+)[>,]?/i', '<$1#$2>', trim($subEmail));
}
$email=implode(",",$fixedSubEmails);
return $email;
}
}
The main problem I was having was that clients weren't receiving emails from our server, (and so I wanted to use an SMTP server to see if that would fix it, instead of the server's default email server).
But I managed to get those clients to receive emails from the server by making some other changes, thus removing the need to use SMTP and the Media Temple email server.
(As an FYI, I found that we were getting bouncebacks from client email servers stating Diagnostic-Code: smtp; 550 Access denied - Invalid HELO name (See RFC2821
4.1.1.1), but they were being sent directly back to the server, and going into the linux user account "www-data". (I read them in /var/mail/www-data, just using tail and vim). I found that postfix, which was handling the sending of emails, was marking the email sender's hostname (ie, "HELO name") as canadafinds3, the name I gave the server in Rackspace, not the domain name: canadafinds.com. So I changed that in /etc/postfix/main.cf, restarted postfix, et voila! No more bouncebacks from those particular clients, and everyone's happy again.)
I ended up writing my own PHP mail() script based on https://web.archive.org/web/20180401094709/http://www.dreamincode.net/forums/topic/36108-send-emails-using-php-smtp-direct/ in order to circumvent this error.

Categories