PHP mailer, gmail smtp not sending email from live server - smtp

I have the following code to send email:
$mail->IsSMTP();
$mail->Host = 'imap.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'my gmail id';
$mail->Password = 'mypassword';
$mail->SetFrom('info#somedomain.com', 'Bilafon');
$mail->Subject = $subject;
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
$mail->MsgHTML($body);
$mail->AddAddress($email_id, $user_name);
return $mail->Send();
Its sending email from the localhost but not from the live server. On live server it shows this error:
SMTP Error: Could not authenticate
Any kind of help is appreciated

Check your gmail settings. It might be SMTP disabled from there.

Related

SendGrid only sends emails to Gmail

I'm trying to integrate SendGrid API via phpMailer (also tried with php API library too). In localhost and live server I got 250 OK with phpmailer and 202 OK with API library response.
After submission (4 receiver), I've only receive 1 email to gmail. Another domain email addresses shows Delivered in SendGrid activitiy page but I dont receive these emails except Gmail.
My sender email adress and domain address already verified. Whats wrong on my code?
phpMailer
$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->IsSMTP();
$mail->Host = 'smtp.sendgrid.net';
$mail->Port = 587;
$mail->Username = 'apikey';
$mail->Password = 'API_KEY';
$mail->SMTPAuth = true;
$mail->SMTPDebug = true;
$mail->Debugoutput = 'html';
$mail->SMTPKeepAlive = true;
$mail->SMTPSecure = 'TLS';
$mail->Priority = 3;
$mail->Encoding = 'base64';
$mail->CharSet = "utf-8";
$mail->SetFrom('sender#mail.com', $name = 'Sender Title');
$mail->AddAddress('receiver#mail.com', 'Receiver Title');
$mail->Subject = 'SendGrid Test';
$mail->MsgHTML('Mail body');
$sending = $mail->Send();
$mail->SmtpClose();
With API Library
$email = new \SendGrid\Mail\Mail();
$email->setFrom("sender#mail.com", "Example User");
$email->setSubject("Sending with Twilio SendGrid is Fun");
$email->addTo("receiver#mail.com", "Example User");
$email->addContent("text/plain", "and easy to do anywhere, even with PHP");
$email->addContent(
"text/html", "<strong>and easy to do anywhere, even with PHP</strong>"
);
$sendgrid = new \SendGrid('API_KEY');
try {
$response = $sendgrid->send($email);
print $response->statusCode() . "\n";
print_r($response->headers());
print $response->body() . "\n";
} catch (Exception $e) {
echo 'Caught exception: '. $e->getMessage() ."\n";
}
If your emails are sending successfully and being received by Gmail, then there is nothing wrong with your code (save for the minor issues that Synchro pointed out in the comments).
Email deliverability isn't an exact science, so there could be something in the contents of your email that triggers mail blocking for some inboxes. I'd follow the guidelines in this article on email deliverability to ensure you are setting yourself up to get emails into inboxes.
Beyond that, I would also set up the SendGrid Event Webhook. That will delivery you events based on what your emails are doing, including delivery events when an email is dropped, deferred, bounced, blocked or delivered. That may help you understand what is happening to your email.

How to stop MailApp.sendEmail from sending myself a copy of the email

We are using Google Scripts to send emails after Google Forms are submitted. In the last day or two, we started receiving email delivery failures every time a form was submitted and the script ran. It is only unable to deliver messages to one account(sender#mydomain.com), which happens to be the account that the script is running as. However, this account should not be receiving a copy of this form anyways. All of the email addresses that are in the "To" field are getting the email with no issue, we are just trying to find out why we are getting this error message.
We are using the MailApp.sendEmail function and have been for years without any problem. We can't use the GmailApp functions because this account is not able to use Gmail and we've never needed to be able to in order to send emails.
In the script, when I add sender#mydomain.com to the To list, I receive the email and do not get any error messages. When I remove it from the To list, the rest of the recipients continue to receive the email but I get that error message again for sender#mydomain.com.
function formSubmitReply(e) {
var replyToAddr = "no_reply#mydomain.com";
var emailAddress = e.values[2]; // + ", sender#mydomain.com";
//Removed section that creates PDF, stores as the variable pdf
var subject = "Form Request from " + e.values[1];
var messageHtml = "<font size = 3><font face = arial>Form Request from: " + e.values[1];
var messagePlain = messageHtml.replace(/\<br\/\>/gi, '\n').replace(/(<([^>]+)>)/ig, "");
MailApp.sendEmail(emailAddress, subject, messagePlain, {htmlBody: messageHtml, name: "Sender", attachments: pdf, replyTo: replyToAddr});
}
It sounds weird to me that MailApp was working on an account that doesn't have Gmail enabled as MailApp affects the email sent Gmail quota but then I found No reports a 550 recipient rejected from MailApp. The only answer at this time looks to be wrong as MailApp sent emails are saved as sent emails on Gmail mailbox of the effective user that ran the script.

How to let googlemail send an automatic mail -from- a secondary mail adress?

I'd like to send a mail with GoogleScript - not from my main-Gmailaccount, but from a secondary mail account, which is setup correctly in the main Gmail account.
I can send emails from the secondary mail account manually in Gmail, so that works fine. So to make it clear again: it shouldn't send as abc#gmail.com, but abc#domainname.com)
In G-Script I use MailApp.sendEmail({ - in the documentary it seems that there are just options to set the name of the sender and set the replyTo adress. If I do this the abc#gmail.com still appears as the sender. Is there a way to change the sender itself?
Documentary: https://developers.google.com/apps-script/reference/mail/mail-app
If your secondary email is setup as an alias of your email account it is possible. The specification mention:
from: the address that the email should be sent from, which must be
one of the values returned by getAliases()
When you execute getAliases(), do you see your alias?
// Log the aliases for this Gmail account and send an email as the first one.
var me = Session.getActiveUser().getEmail();
var aliases = GmailApp.getAliases();
Logger.log(aliases);
if (aliases.length > 0) {
GmailApp.sendEmail(me, 'From an alias', 'A message from an alias!', {'from': aliases[0]});
} else {
GmailApp.sendEmail(me, 'No aliases found', 'You have no aliases.');
}
https://developers.google.com/apps-script/reference/gmail/gmail-app#getAliases()

Unable to use sub account with Google Service Account

I needed to create a script that uploads the resulting screen shots to google drive.
I was hoping I could just auth in as my google user, but that seems... harder? So I abandoned that tact. Next I moved onto service accounts. This works fine (now) for my service account, but when I attempt to specify a user ($auth->sub) I get "Unauthorized client or scope in request.".
function buildService($userEmail) {
$DRIVE_SCOPE = 'https://www.googleapis.com/auth/drive';
$SERVICE_ACCOUNT_EMAIL = 'notsupplied#developer.gserviceaccount.com';
$SERVICE_ACCOUNT_PKCS12_FILE_PATH = 'pathtofile.p12';
$key = file_get_contents($SERVICE_ACCOUNT_PKCS12_FILE_PATH);
$auth = new Google_Auth_AssertionCredentials(
$SERVICE_ACCOUNT_EMAIL,
array($DRIVE_SCOPE),
$key);
$auth->sub = 'myuser#gmail.com';
$client = new Google_Client();
$client->setAssertionCredentials($auth);
return new Google_Service_Drive($client);
}
I'd love to abandon the service account and just auth with my regular google user if thats just as easy. Or solve how (in the api settings maybe?) I can ensure myuser#gmail.com can be used.
Refresh_token is the key here. In a webbrowser use this link to approve your google user:
https://accounts.google.com/AccountChooser?service=lso&continue=https%3A%2F%2Faccounts.google.com%2Fo%2Foauth2%2Fauth%3Fresponse_type%3Dcode%26scope%3Dhttps%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive%26redirect_uri%3Dhttps%3A%2F%2Fwww.example.com%2Foauth2callback%26access_type%3Doffline%26client_id%3D<CLIENT_ID>%26hl%3Den%26from_login%3D1%26as%3D34eac985232ba748&btmpl=authsub&hl=en&approval_prompt=force&access_type=offline
which will return a URL like https://www.example.com/oauth2callback?code=
Then post code=&client_id=&client_secret=&redirect_uri=&grant_type=authorization_code to https://accounts.google.com/o/oauth2/token
This will return a "refresh_token" parameter. Save this. Very important. If you don't get one you have to go to https://security.google.com/settings/security/permissions to revoke permissions from your app.
After you get the refresh token you're good to go:
$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->addScope("https://www.googleapis.com/auth/drive");
$client->setAccessType('offline');
$token = $client->refreshToken('<YOUR_REFRESH_TOKEN>');
$service = new Google_Service_Drive($client);

How to Make a button send an email using AS3 automatically

I am working in actionscript 3.0 and am making a website!
In my website, I want to make a button that sends an email using a click of button and I don't want it to open their mail client, instead just send it.
I am currently using the "mailto" function but would like to know how to make it send automatically, or what else I can use to achieve that.
Here is a snippet of my code:
function submitPoll(e:MouseEvent):void {
//sending the email stuff
var request:URLRequest = new URLRequest("mailto:name#hotmail.com"+"?subject=Subject"+"&body= Hello world ");
navigateToURL(request, "_blank");
request.method = URLRequestMethod.POST;
//other
Submit_btn.x = -100;
pollUsed = true;
thanks_txt.x = 849;
thanks_txt.y = 656;
}
The situation with Flash is more or less the same as the situation with HTML when it comes to sending email. You have 2 options:
Pop open the email client on the users machine using mailto as you are doing.
Send a POST or GET request to a server which can send the email on your behalf.
What you want to do is number 2 so that means you need access to a server capable of sending mail that can also receive GET/POST requests. If you have script access to your webserver you can undoubtedly find a free online script that will allow you to send emails. For example:
PHP
Python
Ruby
Perl
ASP
How you send the information to the script will depend on what variables the script requires you to send. From ActionScript you will probably want to use URLLoader:
const SCRIPT_URL:String = "http:// .... your server ... / ... script file ...";
var request:URLRequest = new URLRequest(SCRIPT_URL);
var variables:URLVariables = new URLVariables();
// these depend on what names the script expects
variables.email = "name#hotmail.com";
variables.subject = "Subject";
variables.body = "Hello World";
request.data = variables;
// depends if the script uses POST or GET
// adjust accordingly
request.method = URLRequestMethod.POST;
var urlLoader:URLLoader = new URLLoader();
loader.load(request);
I think its a case of doing a URLRequest to another PHP file. Send the values of the textfield to the PHP file and process the email that way.
I don't have any code to hand, but this is what I did when I was doing the same. Hope this helps... and don't forget to validate :)