phpmailer to send email from any gmail - php

I used the following code snippet using phpmailer class inorder to send an email using gmail server when a form is submitted in my web application! Though it works for my personel gmail address, it does not send emails when I replace my gmail address and password with my clients gmail address and password!
How can i correct this probem?
require "phpmailer/class.phpmailer.php"; //include phpmailer class
// Instantiate Class
$mail = new PHPMailer();
// Set up SMTP
$mail->IsSMTP(); // Sets up a SMTP connection
$mail->SMTPAuth = true; // Connection with the SMTP does require authorization
$mail->SMTPSecure = "ssl"; // Connect using a TLS connection
$mail->Host = "smtp.gmail.com"; //Gmail SMTP server address
$mail->Port = 465; //Gmail SMTP port
$mail->Encoding = '7bit';
// Authentication
$mail->Username = "sunethperera#gmail.com"; // Your full Gmail address
$mail->Password = "******"; // Your Gmail password
// Compose
$mail->SetFrom($_POST['emailid'], $_POST['fullname']);
$mail->AddReplyTo($_POST['emailid'], $_POST['fullname']);
$mail->Subject = "Pharmacy order confirmaton"; // Subject (which isn't required)
$mail->MsgHTML($message);
// Send To
$mail->AddAddress($_POST["Email"], "Recipient Name"); // Where to send it - Recipient
$result = $mail->Send(); // Send!
$message = $result ? 'Successfully Sent!' : 'Sending Failed!';
unset($mail);
When I send the message from my client's email, I get the error message:
SMTP Error: Could not authenticate.
How can I correct this issue?

Trying using this:
$mail->SMTPSecure = "tls";
Remove the Encoding parameter
And also check you have the correct from address and the password as well.

Update your PHPMailer right now. You are running an old vulnerable version. Read the troubleshooting guide and do what it says. Do not use the submitter's address as the from address - its forgery and will make you fail SPF checks. Gmail does not let you send from arbitrary from addresses. Your auth problem is covered in the guide, but you will need to either enable less secure apps in Gmail, or implement oauth, as described in the docs and examples.
This has all been covered many times on SO - search before you post.

Related

PHPMailer sets the From same as the smtp username

I am using PHPMailer library to send SMTP email for booking enquiry. (Note: The same problem I was facing in PHP Pear Mail library as well).
I set the email from as below
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true;
$mail->Username = 'gmail.owner#gmail.com'; // SMTP username
$mail->Port = 465; // TCP port to connect to
$mail->isHTML(true);
$mail->Password = 'xxxx';
$mail->setFrom(mark.antony#example.com, Mark Antony);
$mail->replyTo(mark.antony#example.com, Mark Antony);
$mail->addAddress(gmail.owner#gmail.com, Gmail Owner);
$mail->Subject = $whatever subject;
$mail->Body = $whatever html;
The problem is after sending email in the received mailbox I see the From: is the same as To: (Or same as the gmail/smtp username).
I have fixed the problem on reply to by setting replyTo value.
Is there anyway I can fix this? Or is that how it suppose to be?
Google does not allow you to set arbitrary from addresses, because doing so is usually forgery. If you try to, gmail will ignore it and substitute your Username address, which is what you're seeing.
There is one exception to this: you can set up additional aliases in your gmail settings (which you need to verify), and once you've done that you can use those addresses as from addresses.
If you're using this for a contact form (as it sounds like you are), setting to and from to your own address, and the submitter as a reply-to is the correct way to go, as the PHPMailer contact form example demonstrates.

why does "Email failed to send: SMTP connect() failed." still appear after having enabled less secure apps

My PHPMailer will not connect to my gmail account properly although I have enabled less secure apps and have checked my username and password several times.
I have enabled less secure apps to access my gmail account "tokens.tts#gmail.com". I have also tried to use both ssl and tls but neither seem to work. I have looked at several videos online and they all seem to mention enabling the less secure apps option but nothing else.
<?php
use PHPMailer\PHPMailer\PHPMailer;
$name = "Michiel Olieslagers";
$email = "tokens.tts#gmail.com";
$subject = "This is just a regular email";
$body = "I am writing this email to see whether this actually function propperly.";
require_once "PHPMailer/PHPMailer.php";
require_once "PHPMailer/SMTP.php";
require_once "PHPMailer/Exception.php";
$mail = new PHPMailer();
// $mail->isSMTP();
// $mail->Host = "smtp.gmail.com";
// $mail->SMTPAuth = true;
// $mail->SMTPSecure = "tls";
// $mail->Username = "tokens.tts#gmail.com";
// $mail->Password = "hqyzZYax";
// $mail->Port = "587";
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Username = "tokens.tts#gmail.com";
$mail->Password = "********";
$mail->Port = "465";
$mail->isHTML(TRUE);
$mail->setFrom($email, $name);
$mail->addAddress("micholieslagers#gmail.com");
$mail->Subject = $subject;
$mail->Body = $body;
if ($mail->send()){
echo "Email is sent";
}
else { echo "Email failed to send: ". $mail ->ErrorInfo; }
?>
I should be receiving a test email at my second account "micholieslagers#tts.edu.sg" however this does not happen.
Looks like the new process for GMTP SMTP has a compulsion.
Please follow the below steps to achieve the same.
Make sure you enable 2-step authentication process with your mobile number before proceeding below steps.
Go to google account from this link.
You will see settings page with tabs on left panel. Click on security tab.
Click on app passwords link. Make sure here you enabled 2-step verification.
You will see select the app and device you want to generate the app password for.
Select app as mail and select your relevant device or you can choose other at last.
Click on the generate button, you will get the generated password. Copy and save it somewhere as it's one-time password otherwise you have to generate it again by creating new app password.
Now STMP configuration,
Host - smtp.gmail.com
Username - your email address for which you generated the app.
Password - Your generated password
SMTPSecure - TLS or SSL whichever work
Port - 465 for SSL and 587 for TLS
This should work as I am still using this configuration.

How to send an email using php [duplicate]

This question already has answers here:
send email using php
(3 answers)
Closed 6 years ago.
When I execute this code, a new tab is opened. It displays my code. How do I fix this?
<?php
$mail = new PHPMailer();
//Send mail using gmail
if($send_using_gmail){
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "myemailid#gmail.com"; // GMAIL username
$mail->Password = "mypassword"; // GMAIL password
}
//Typical mail data
$mail->AddAddress('myemailid#gmail.com', 'Name');
$mail->SetFrom('myemailid#gmail.com', 'Name');
$mail->Subject = "My Subject";
$mail->Body = "Mail contents";
try{
$mail->Send();
echo "Success!";
} catch(Exception $e){
//Something went bad
echo "Fail :(";
}
?>
I am trying to send an email using PHP here.
$mail = new PHPMailer();
$body = file_get_contents('contents.html');
$body = eregi_replace("[\]",'',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "mail.yourdomain.com"; // sets the SMTP server
$mail->Port = 26; // set the SMTP port for the GMAIL server
$mail->Username = "yourname#yourdomain"; // SMTP account username
$mail->Password = "yourpassword"; // SMTP account password
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddReplyTo("name#yourdomain.com","First Last");
$mail->Subject = "PHPMailer Test Subject via smtp, basic with authentication";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "whoto#otherdomain.com";
$mail->AddAddress($address, "John Doe");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
try this one it worked for me. If didnt work let me know
I know this will not answer OP's question but i want to make this in people's attention, Please excuse me...
Source :- https://sendgrid.com/blog/gmail-dmarc-update-2016/
What is Gmail changing?
June of 2016 Gmail will change its DMARC policy from p=”none” to p=”reject.” This means any message sent using gmail.com in the from address, will have to originate from Gmail’s infrastructure.
What does this mean for me?
It depends. If you have any mail streams that send messages using gmail.com in the from address, you will have to make changes before June, or risk having those messages filtered or blocked outright.
If you only send email using your own domain or another domain that you control, you have nothing to worry about. However, it’s not uncommon for some applications or websites to send messages using their users’ email addresses. For example, if a user wants to send a message to their friend using your platform, it could make sense to send the message using their personal email address. If their email address happens to be a gmail.com address, this message will no longer deliver once these changes take place. A good alternative to sending mail from your user’s email address is to use their name in the friendly from. A “friendly from” is when you use a name to appear as the from address, instead of the email address itself:
exampleuser#yahoo.com can be sent as “Example User”
This way your recipients still recognize the individual that sent the message, and you’re no longer at risk of violating Gmail’s DMARC policy.

Can I use gmail as smtp server for my website

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?

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