PHPMailer using SMTP username as senders email address - php

So I'm trying to send an email using current version of PHPMailer (https://github.com/PHPMailer/PHPMailer). The SMTP connection and the mail sending is working fine, except the script is not using the "SetFrom" $email variable for the senders mail, but instead the "Username" email.
I've tried using multiple forms of setting the "From" mail and nothing seems to work, although it's important to note that the "name" in the "SetForm" is set just fine, but "email" is not.
Here's the code:
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'USERNAME(MY EMAIL)';
$mail->Password = 'PASSWORD';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->isHTML(true);
$mail->SetFrom($email,$name);
$mail->addAddress($email_received);
$mail->Subject = $subject;
$mail->Body = $user_msg;

This is a common question, asked many times on here. gmail does not allow you to set arbitrary senders - if you're sending through gmail, you have to use either your gmail address, or a pre-configured alternative. See here for google's docs on the matter. Generally it's a bad idea to try to do this anyway as it's likely to cause your message to fail SPF tests that the address owner may have configured.

If you check header of the received mail, you will find your SetFrom email id defined as X-Google-Original-From.
It's because Gmail automatically rewrites the From line of any email you send via its SMTP server to the default 'Send mail as` email address in your Gmail or Google Apps email account Settings.
If you really want to use Gmail as the email provider, then you need to open a Google Apps account and verify that you own the domain which you are using for your email id.
There is also a workaround for this problem (but not recommended):
In your Gmail Settings, go to the 'Accounts' tab and add another email address you own in Send mail as section. This will cause Google's SMTP server to re-write the From field with whatever address you enabled as the default Send mail as address. Here, you need to enter smtp settings of your domain.
So, when you send email using this setting, PHPMailer will connect to Gmail and Gmail will connect to your SMTP server before sending any email.

Related

How to send email from GoDaddy Linux VPS with gmail SMTP so that sent emails are saved

I have used the following PHP code to send email, but when I login to the gmail account after sending an email, the email that has sent is not recorded in the sent mail section of gmail. But emails are successfully delivered each time. I can use the IsSMTP() function in my laptop and in that case sent mails are recorded, but in GoDaddy VPS I can not use IsSMTP() but to use IsMail().
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\OAuth;
use PHPMailer\PHPMailer\POP3;
require '../PHPMailer/Exception.php';
require '../PHPMailer/PHPMailer.php';
require '../PHPMailer/SMTP.php';
require '../PHPMailer/OAuth.php';
require '../PHPMailer/POP3.php';
$mail = new PHPMailer(true);
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->IsMail();
//$mail->IsSMTP(); // if enabled instead of IsMail() an error "can not connect to SMTP" appears.
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'username#gmail.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
//Recipients
$mail->setFrom('username#gmail.com', 'OFFICE');
$mail->addReplyTo('username#gmail.com', 'OFFICE');
$mail->addBCC("username#gmail.com", "OFFICE");//since sent mails are not saved
$mail->isHTML(true);
$mail->addAddress('to#gmail.com');
$mail->Subject = 'Subject';
$mail->Body = 'Hello';
$mail->AltBody = 'Hello';
$mail->send();
The problem is that Godaddy (and many other VPS providers) block SMTP ports in order to fight spammers.
When you are using $mail->IsMail(), the e-mails will go via GoDaddy's servers completely bypassing your Gmail account.
Now there is another problem as well, Google protects your #gmail.com addresses using various methods (such as SPF), so even if you are able to send the e-mail, it will be most likely marked as SPAM - if delivered at all. Otherwise anyone could send e-mails as you..
I'd highly suggest purchasing a domain (if you dont have one already) and either:
Hosting it in GoDaddys e-mail service and sending it via their SMTP servers (their SMTP servers are allowed)
Use external service which does not rely on SMTP (such as MailGun and their PHP SDK for example, but do your own research for a suitable one)
There might be some other "hacky" ways to still send these e-mails trough Gmail if you really need to, such as using VPN inside VPS or[1] proxying google SMTP externally on some other port which is not blocked. Beware, these methods still require some external service (most likely paid) and might conflict with GoDaddy's and/or Gmail TOS
[1] Apparently most of the VPN providers also block SMTP ports to avoid e-mail spammer flood

PHP Mailer - problems setting from mail [duplicate]

This question already has answers here:
How to change from-address when using gmail smtp server
(6 answers)
Closed 4 years ago.
I'm using the PHPMailer library for sending emails. Everything is working just fine except for one small problem. I have a default gmail account that is used to most my clients as a SMTP Auth.
$mail->Host = 'ssl://smtp.gmail.com';
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = 'email#gmail.com';
$mail->Password = 'gmail**pass';
However, when I setup the FROM that it was supposed to use, it uses my GMAIL instead of the defined FROM as a reply-to and that never happened before. I thought it might be some php.ini setting, but looking into the phpinfo(), nothing caught my attention.
$mail->From = 'contact#client.com';
$mail->FromName = 'Client Name';
Does anyone have any idea what that might be?
Gmail will not permit you to set a From address unless it's in your Gmail settings as a verified email alias (they overwrite it to be your Gmail address). Even if they would, doing so would generally get your email flagged as spam, because in most situations Gmail isn't going to be set up as a valid sender in the domain's SPF records.
Leave From as an address under your control, and set the Reply-To header instead to direct replies to the right address.

php mail and SMTP while using cloudflare

I am using cloudflare on my website and I want to keep my server's IP (the ORIGIN IP) private, to avoid DDoS attacks being sent directly to my server's IP. My server uses Apache, PHP, MySQL.
When using php mail to send emails (even if I use phpmailer library to send the emails through external SMTP) the IP of my server is added to the mail headers.
It happens with Google SMTP, Mailgun and others, because probably it is in their policy to write in the header the IP from which the mail came.
At the moment, the only solution that I have in mind and requires a lot of effort, which is to create my own REST API and send emails through another server, something like this:
ORIGIN SERVER IP sends email data in text format via my REST API to MY MAIL SERVER IP and then MY MAIL SERVER IP uses php mail function with phpmailer to send the email via SMTP to the user. This way, the IP of MY MAIL SERVER will appear in the email headers and not the IP of the ORIGIN SERVER.
Is there a much more elegant way to do this? Is there a mail service that offers a rest API and if I use their API, they won't display my server's IP in the email headers? Or maybe there is an already developed REST API / library for sending emails remotely as I requested, so I won't have to develop and test my own from scratch?
You should send emails through mailgun (or sendgrid, or jetmail, or SES, or ...) through their API and not the SMTP protocol and your IP won't be disclosed.
For instance for Mailgun SDK: https://github.com/mailgun/mailgun-php
$mg = Mailgun::create('key-example');
# Now, compose and send your message.
# $mg->messages()->send($domain, $params);
$mg->messages()->send('example.com', [
'from' => 'bob#example.com',
'to' => 'sally#example.com',
'subject' => 'The PHP SDK is awesome!',
'text' => 'It is so simple to send a message.'
]);
But there are SDK for most providers:
Sendgrid php sdk
Mailjet php sdk
AWS SES PHP SD
Moreover, I would recommend using SwiftMailer which is powerful library to handle email. One of the cool thing is that it abstract the transport and you would be able to switch from SMTP or any provider API using packages.
You can use e.g. mailchimp, amazon SES, or other mail service provider, they should not add your ip. But that services are paid.
Long time ago, at college, I can not use php mail command because of firewall rules so write my own SMTP autentication class.
Some time latter I start using PHPMailer class and I never had another problem, even using Gmail as sender.
Give a look at https://github.com/PHPMailer/PHPMailer .
This is a simple example:
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//Load composer's autoloader
require 'vendor/autoload.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
//Attachments
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
get an instance from any cloud provider, send REST request to that instance or whatever you prefer, then your original web server's ip will be totally invisible.
There is no API or elegant way to hide your IP from emails that you're sending. Any SMTP provider who offers this is worthy of blacklisting and would be immediately overcome by spammers signing up to abuse this privacy.
You must use your idea of creating an internal Web relay system to send to other IPs before initiating SMTP. But the hassle of setting that up should be more of a hassle than rebuilding your current site with another IP.
This sounds like a classic case of treating your server like a pet instead of like cattle. If rebuilding your current site on a new IP is less attractive than building and maintaining a custom Web API to hide your IP from exposure, you need to investigate automation tools.

PHP mail use FROM of multiple mail client (Yahoo | Google | Hotmail | Rediff | Personal Mail domain)

Currently I'm Using PHP mail function Or CodeIgnitor mail function to send mail.
From mail id can be of any domain , example xyz#gmail.com, xyz#yahoo.com, xyz#hotmail.com
Also To mail can be of any domain.
My mails are sent proper when FROM is set to any mail other than that of YAHOO.
Having trouble to send mail from PHP mail FROM any mail of YAHOO.
Is yahoo blocking my mails ?
How can I solve this problem ?
You cannot send mail successfully on behalf of the domains stated above using your mail server. Each of those domains have something in use called an SPF (Sender Policy Framework) record in DNS which tells all recipients mail severs which check SPF to confirm the senders IP is the same as the SPF. You would need to send the email via their SMTP servers by relaying from your own.
SPF example for GMAIL & YAHOO
v=spf1 redirect=_spf.google.com
v=spf1 redirect=_spf.mail.yahoo.com
Check if an SPF record exists on domain by using this site:
http://mxtoolbox.com/spf.aspx
In my opinion, your best option is to relay your mail to the correct SMTP servers per domain. You can do this very easily using PHPMailer. If the domain set in the FROM option is a privately managed domain, then you should be able to relay on there behalf if no SPF record is set and your sending IP is not blacklisted.
A site for checking if your sending IP is blacklisted:
http://mxtoolbox.com/blacklists.aspx
An example of how you can choose which SMTP settings are used for a specific domain:
$email = 'xyz#yahoo.com';
$domain = explode('#', $email) ;
switch ($domain[1]) {
case 'yahoo.com':
//NOT REAL SMTP SETTINGS!
$mail->Host = 'smtp1.yahoo.com';
$mail->SMTPAuth = true;
$mail->Username = 'user#yahoo.com';
$mail->Password = 'secret';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
break;
case 'gmail.com':
//NOT REAL SMTP SETTINGS!
$mail->Host = 'smtp1.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'user#yahoo.com';
$mail->Password = 'secret';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
break;
}
//Rest of PHP Mailer code

How to make localhost to send email? [duplicate]

This question already has answers here:
How can I send an email using PHP?
(20 answers)
Closed 7 years ago.
I am unable to send mails through localhost Xampp even after making changes in php.ini and sedmail.php files as per THIS.
I have adoubt in sendmail.php file what email & PassWord to give here;
auth_username=
auth_password=
Please somebody get me through this.
You can use SMTP mail sending library and try that function to send the mail from localhost
Let we resolve this issue by follow some steps.
1. Make sure error reporting is enabled and set to report all errors(use code in .php file)
error_reporting(-1);
ini_set('display_errors', 'On');
set_error_handler("var_dump");
2. Check your server's mail logs
Your localhost should be logging all attempts to send emails through it. The location of these logs will user's root directory under logs. Inside will be error messages the server reported, if any, related to your attempts to send emails.
3. Make sure you have a mail server set up on localhost
If you are developing on your local workstation using XAMPP, an email server is probably not installed on your workstation. Without one, PHP cannot send mail by default.
You can overcome this by installing a basic mail server. For Windows you can use the free Mercury Mail.
You can also use SMTP to send your emails. See this answer to re-check how to do this.
4. Check to see if mail() returns true or false with message
Please use below code and let me know what happen. This code will display actual error message and we can resolve this issue.
Replace
mail($to, $subject, $message, $headers);
With
$mailReturn = mail($to, $subject, $message, $headers);
print('<pre>');
print_r($mailReturn);
print('</pre>');
exit();
If above 3 steps done perfect but no success then follow step 4. Let me know what this code print.
5. SMTP settings(in php.ini) for send mail from localhost
If you are using Gmail then you got lucky. Gmail lets us use their SMTP provided that you will have to authenticate it using your own username and password. To use Gmail SMTP the values will be like below:
//Set the hostname of the mail server
$mail->Host = "smtp.gmail.com";
//enable this if you are using gmail smtp, for mandrill app it is not required
//$mail->SMTPSecure = 'tls';
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 25;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = "YOUR.ID#GMAIL.COM";
//Password to use for SMTP authentication
$mail->Password = "YOUR_PASSWORD";
Or, if you don't want to use your Gmail account then I would suggest to create one account on Mandrill and get your SMTP host, username and password from there. I have tested both gmail and mandrill and they are working pretty good.
//Set the hostname of the mail server
$mail->Host = "smtp.mandrillapp.com";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 25;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = "YOUR_REGISTER_EMAIL#MANDRILL.COM";
//Password to use for SMTP authentication
$mail->Password = "YOUR_PASSWORD";
Make sure all variables value are checked and change if require.

Categories