Can I use gmail as smtp server for my website - php

Hello I am trying to get a website up and running. It is currently hosted on AWS, so I do not have my own smtp server running at this moment. So after reading a few articles, I have understood that we could used gmail as a smtp server.
I wanted to double check if what I read was right, I am going to use smart job board software, can I plug in the values provided by gmail and use that as an smtp server??

Yes, Google allows connections through their SMTP and allows you to send emails from your GMail account.
There are a lot of PHP mail scripts that you can use. Some of the most popular SMTP senders are: PHPMailer (with an useful tutorial) and SWIFTMailer (and their tutorial).
The data you need to connect and send emails from their servers are your GMail account, your password, their SMTP server (in this case smtp.gmail.com) and port (in this case 465) also you have to make sure that emails are being sent over SSL.
A quick example of sending an email like that with PHPMailer:
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // SMTP authentication
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->Port = 465; // SMTP Port
$mail->Username = "john.doe#gmail.com"; // SMTP account username
$mail->Password = "your.password"; // SMTP account password
$mail->SetFrom('john.doe#gmail.com', 'John Doe'); // FROM
$mail->AddReplyTo('john.doe#gmail.com', 'John Doe'); // Reply TO
$mail->AddAddress('jane.doe#gmail.com', 'Jane Doe'); // recipient email
$mail->Subject = "First SMTP Message"; // email subject
$mail->Body = "Hi! \n\n This is my first e-mail sent through Google SMTP using PHPMailer.";
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>

I successfully use the GMail SMTP server.
We do have a corporate GMail account, though I don't think that matters. A personal GMail account should suffice.
I do not have a PHP sample however the following configuration for ASP.Net should provide adequate guidance:
<mailSettings>
<smtp from="me#gmail.com">
<network enableSsl="true" host="smtp.gmail.com" port="587" userName="me#gmail.com" password="mypassword" />
</smtp>
</mailSettings>
If someone has a suitable PHP sample, feel free to edit my answer or post your own.

Authentication is required I believe, but I don't see why not. I won't do the research for you, but there are a couple things to look into:
Their SMTP server requires TLS encryption and is hosted on a non-standard port (995). You will need to ensure that AWS supports both of these options for SMTP outbound.
There is probably a cap on emails you can send before being marked as spam. You should research this and ensure it is not beyond your requirements.

You can user PHPMailer class for this job. And you can easily configure the smtp.
An example of configuration
if (class_exists(#PHPMailer)) {
$smtp_mail = new PHPMailer();
$smtp_mail->isSMTP();
$smtp_mail->SMTPAuth = true; // enable SMTP authentication
$smtp_mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$smtp_mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$smtp_mail->Port = 465; // set the SMTP port
$smtp_mail->Username = "info#example.com"; // GMAIL username
$smtp_mail->Password = "password"; // GMAIL password
$smtp_mail->From = "info#example.com";
$smtp_mail->FromName = "Name";
$smtp_mail->AltBody = "This is the body when user views in plain text format"; //Text Body
$smtp_mail->WordWrap = 50; // set word wrap
$smtp_mail->AddReplyTo("info#example.com","Name");
$smtp_mail->isHTML(true); // send as HTML
}

Although you can technically use Gmail as SMTP server, it is not recommended for larger websites. By time you may receive issues like "421 4.7.0 Temporary System Problem" or similar, is it was not designed to be used by an application, rather a single person.
Related issue for the error message above:
Gmail SMTP error - Temporary block?

Related

PHP mail function not working in godaddy account [duplicate]

I'm using godaddy for hosting my site and using default godaddy mail service.
Now i want to sent email using php mail function to other email address from my 1 of my 15 email address of my godaddy's email accounts
How can i fix that from which email address email will be sent and how to place the username and password for the email address ?
Thanks
The PHP mail function uses the mailserver configured for that webhost. You can't change that. Since godaddy controls the mailserver they control what headers it sends. You could try inserting a custom From header but I doubt that will work. It will either get modified, flagged as spam, or rejected.
If you have 15 accounts at godaddy, perhaps it's time to look for a more serious hosting solution?
Instead of using the mail() function, which just calls the OS mail function (i.e. sendmail), try something like SwiftMail (free PHP mail library). It support many different ways of sending mail, including logging into a mail account and sending email, just like you would do from your own computer. You could even send email from a gmail account if you wanted.
http://swiftmailer.org/
I am using godaddy hosting . Just keep some fields blank and send mail it will work .
please see below code its working for me.
<?php
include("class.phpmailer.php");
function sendMail($address,$username,$body){
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
//$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
// $mail->SMTPAuth = true; // enable SMTP authentication
// $mail->SMTPSecure = "ssl"; // sets the prefix to the servier
// $mail->Host = "smtp.gmail.com"; // sets as the SMTP server
// $mail->Port = 465; // set the SMTP port for the server
// $mail->Username = "xyz#gmail.com"; // username
// $mail->Password = "test121232"; // password
$mail->SetFrom('contact#example.co.in', 'Contact');
$mail->Subject = "Enquiry for tour and travels package";
$mail->MsgHTML($body);
$address = $address;
$mail->AddAddress($address, $username);
$mail->AddCC('contact#example.co.in');
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
}
?>
just changed from email address, so you can send mail through this email id.
$mail->SetFrom('youremail#example.co.in', 'Contact');
I've had this issue on a couple of client accounts.
After MUCH playing around, I found that it didn't matter what email app or code you use.
I finally compared two accounts. One where I know email sends from PHP, and one where it doesn't. The difference I found was:
Working account had these 2 dns entries on the domain:
CNAME / mail / #
MX / # / mail.yourdomainname.com
And the MX Entry set within CPanel Zone Editor of:
Destination: yourdomainname.com
The account that was NOT working was the same, except it did not have the CNAME entry mentioned above. I added it, waited a few minutes and tested my code again and I finally started receiving the emails from my code!
This one worked for me.
I asked Godaddy and support they told me to use relays. So Instead of using mail() function, I used PHPMailer. Kindly check below the configuration for PHP Mailer.
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Host = "relay-hosting.secureserver.net";
$mail->Port = 25;
$mail->setFrom('from#mail.com', 'From Mail');
$mail->addAddress('to#mail.com', 'To Mail');
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->msgHTML("test body 123");
$mail->AltBody = 'HTML messaging not supported';
if(!$mail->send()){
echo "Mailer Error: " . $mail->ErrorInfo;
}else{
echo "Message sent!";
}
Note: Here from#mail.com is my mail ID which is in cPanel mail accounts.

Send email with false email on PHP

I installed recently PHPMailer, because of not being able to send without it.
Now I have another problem: I want to send an email with an invent one, for example, "no-reply#my-domain.com". I don't seem to be able to do it. when I send with Sendmail, it just won't send, and if I use SMTP with autentication, it sends with my email.
require "../PhpMailer/PHPMailerAutoload.php";
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "marcelo43#gmail.com";
$mail->Password = "My Password";
$mail->setFrom('no-reply#my-domain.com','MyDomain Admin');
$mail->addAddress('to#gmail.com','To');
$mail->Subject = "Test";
$mail->msgHTML('My message');
if(!$mail->send())
echo 'Could not send email';
else
echo 'Email succesfully sent';
This just sends an email to "to#gmail.com" with "MyDomain Admin" as the name and "marcelo43#gmail.com" as its email.
What do I need to do to send with the email "no-reply#my-domain.com"?
You can't send email with a false email because Gmail blocks mail from an other domain that its own.
If you want to send an e-mail with an other domain you should use an other SMTP server. You can use an SMTP from OVH for example if you have a mail server. Otherwise, Yahoo will let you send you e-mail I think.
Gmail doesn't let you use arbitrary from addresses at all, even from gmail domains. You can however create aliases within your gmail settings that are allowed as from addresses. If you want to use your own domain via gmail, you will neeed to configure gmail as the MX for your domain. If you do that, you will be able to do what you ask.
BTW, this is mentioned in the PHPMailer docs.

PHPMailer: Dynamic gmail forwarding

I do not know if what I want to do is possible (but finding out that it isn't would be useful in itself).
I cannot use my company's gmail account "real.business#gmail.com" directly with PHPMailer. I can, however, use an intermediary gmail account "fake.12345.account#gmail.com" which can have "less secure apps" enabled, which permits SMTP verification.
However I do not want to have the emails be sent from this fake.12345.account#gmail.com account (wouldn't look particularly professional) - but rather the company's gmail account.
I can send the emails from the intermediary account to real.business#gmail.com; either through the editing of the PHPMailer parameters, or by automatically forwarding emails from fake.12345.account#gmail.com to the company account.
The problem lies in how real.business#gmail.com can then successfully email the email (or at least appear to be the sender), as originally intended.
The code so far
$Mail = new PHPMailer();
$Mail->IsSMTP(); // Use SMTP
$Mail->Host = "smtp.gmail.com"; // Sets SMTP server for gmail
$Mail->SMTPDebug = 0; // 2 to enable SMTP debug information
$Mail->SMTPAuth = TRUE; // enable SMTP authentication
$Mail->SMTPSecure = "tls"; //Secure conection
$Mail->Port = 587; // set the SMTP port to gmail's port
$Mail->Username = 'fake.12345.account#gmail.com'; // gmail account username
$Mail->Password = 'a_password'; // gmail account password
$Mail->Priority = 1; // Highest priority - Email priority (1 = High, 3 = Normal, 5 = low)
$Mail->CharSet = 'UTF-8';
$Mail->Encoding = '8bit';
$Mail->Subject = 'Mail test';
$Mail->ContentType = 'text/html; charset=utf-8\r\n';
$Mail->From = 'testing.num.101#gmail.com'; //Your email adress (Gmail overwrites it anyway)
$Mail->FromName = 'Testing Again';
$Mail->WordWrap = 900; // RFC 2822 Compliant for Max 998 characters per line
$Mail->addAddress($personEmail); // To: the PERSON WE WANT TO EMAIL
$Mail->isHTML( TRUE );
$Mail->Body = ' Good news '.$personName.'! The email sent correctly!';
$Mail->AltBody = 'This is a test mail';
$Mail->Send();
$Mail->SmtpClose();
if(!$Mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $Mail->ErrorInfo;
exit;
}
So the issue is: not having the email sent to $personEmail from fake.12345.account#gmail.com (that's trivial) but rather how to send the email from fake.12345.account#gmail.com to real.business#gmail.com such that real.business#gmail.com forwards the message to $personEmail
What you're describing is really relaying, which is usually configured in the mail server config (not the messages), but you don't have access to anything like that in gmail.
You can set allowed aliases in gmail, but I would guess that these are not allowed to overlap with existing gmail account names as that would be a major security hole. Why not enable "less secure apps" on the main account? It's not as if it is actually any less secure - if anything it's better, because the setup to use OAuth2 is so deeply complex and unpleasant...
That said, rather than trying to do all this forgery, you may be interested in this PR and associated docs. It's fairly likely the xoauth branch will get merged into master and released without any further changes as PHPMailer 5.2.11, and it would very helpful if you could give it a try.
PHPMailer is made for sending.
What you want to do is forward an email. This implies receiving the email and then sending it through.
What you need is some kind of IMAP client in php, that will allow you to read the emails on fake.12345.account#gmail.com (and maybe real.business#gmail.com). Then save their body and title and pass it to PHPMailer. You can then use PHPMailer to send the emails with real.business#gmail.com.

php mail() from godaddy server

I'm using godaddy for hosting my site and using default godaddy mail service.
Now i want to sent email using php mail function to other email address from my 1 of my 15 email address of my godaddy's email accounts
How can i fix that from which email address email will be sent and how to place the username and password for the email address ?
Thanks
The PHP mail function uses the mailserver configured for that webhost. You can't change that. Since godaddy controls the mailserver they control what headers it sends. You could try inserting a custom From header but I doubt that will work. It will either get modified, flagged as spam, or rejected.
If you have 15 accounts at godaddy, perhaps it's time to look for a more serious hosting solution?
Instead of using the mail() function, which just calls the OS mail function (i.e. sendmail), try something like SwiftMail (free PHP mail library). It support many different ways of sending mail, including logging into a mail account and sending email, just like you would do from your own computer. You could even send email from a gmail account if you wanted.
http://swiftmailer.org/
I am using godaddy hosting . Just keep some fields blank and send mail it will work .
please see below code its working for me.
<?php
include("class.phpmailer.php");
function sendMail($address,$username,$body){
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
//$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
// $mail->SMTPAuth = true; // enable SMTP authentication
// $mail->SMTPSecure = "ssl"; // sets the prefix to the servier
// $mail->Host = "smtp.gmail.com"; // sets as the SMTP server
// $mail->Port = 465; // set the SMTP port for the server
// $mail->Username = "xyz#gmail.com"; // username
// $mail->Password = "test121232"; // password
$mail->SetFrom('contact#example.co.in', 'Contact');
$mail->Subject = "Enquiry for tour and travels package";
$mail->MsgHTML($body);
$address = $address;
$mail->AddAddress($address, $username);
$mail->AddCC('contact#example.co.in');
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
}
?>
just changed from email address, so you can send mail through this email id.
$mail->SetFrom('youremail#example.co.in', 'Contact');
I've had this issue on a couple of client accounts.
After MUCH playing around, I found that it didn't matter what email app or code you use.
I finally compared two accounts. One where I know email sends from PHP, and one where it doesn't. The difference I found was:
Working account had these 2 dns entries on the domain:
CNAME / mail / #
MX / # / mail.yourdomainname.com
And the MX Entry set within CPanel Zone Editor of:
Destination: yourdomainname.com
The account that was NOT working was the same, except it did not have the CNAME entry mentioned above. I added it, waited a few minutes and tested my code again and I finally started receiving the emails from my code!
This one worked for me.
I asked Godaddy and support they told me to use relays. So Instead of using mail() function, I used PHPMailer. Kindly check below the configuration for PHP Mailer.
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Host = "relay-hosting.secureserver.net";
$mail->Port = 25;
$mail->setFrom('from#mail.com', 'From Mail');
$mail->addAddress('to#mail.com', 'To Mail');
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->msgHTML("test body 123");
$mail->AltBody = 'HTML messaging not supported';
if(!$mail->send()){
echo "Mailer Error: " . $mail->ErrorInfo;
}else{
echo "Message sent!";
}
Note: Here from#mail.com is my mail ID which is in cPanel mail accounts.

How to send email without my own mail server by PHP?

Is it possible to use Google's mail server for testing purpose,and replace the address of mail server when my own server is ready?
You can just send your mails via smtp.gmail.com (port 465 or 587) as with any email client. Note anyway that you will need a Google email account for this. More details are here: Configuring email clients for using GMail
I suggest you to use phpmailer, this is an example working code with it:
<?php
include_once("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
// enable SMTP authentication
$mail->SMTPAuth = true;
// sets the prefix to the server
$mail->SMTPSecure = "ssl";
// sets GMAIL as the SMTP server
$mail->Host = 'smtp.gmail.com';
// set the SMTP port
$mail->Port = '465';
// GMAIL username
$mail->Username = 'your.gmail.user#gmail.com';
// GMAIL password
$mail->Password = 'your-gmail-password';
$mail->From = 'email address who send the email';
$mail->FromName = 'yourname';
$mail->AddReplyTo('email to reply', 'name to reply');
$mail->Subject = 'Test Gmail!';
if($is_your_mail_an_html){
$mail->MsgHTML($html_message);
$mail->IsHTML(true);
}else{
$mail->Body = $text_message;
$mail->IsHTML(false);
}
$mail->AddAddress('to address email', 'to name');
if(!$mail->Send()){
echo = $mail->ErrorInfo;
}else{
$mail->ClearAddresses();
$mail->ClearAttachments();
}
?>
But even without phpmailer, you can use gmail to send emails; Just set the port to 465 and enable the ssl auth.
P.s.: dont try to send nesletter throught gmail; they will block your account for 1 day if you send more than $x email per day ($x is 500 on the google documentation, but my experience say that is around 85!)
Yes google does offer that via smtp.
smtp.google.com
port: 587
You also will need your google username and password to send emails.
You need a php smtp class. PHPMailer has one.
If you run a Windows server you can just do this (if you have access to the php.ini). Otherwise follow Sarfraz suggestion.
<?php
ini_set('sendmail_from','test#test.com');
ini_set('SMTP','smtp.test.net');
mail(...);
?>

Categories