PHP Mailer - problems setting from mail [duplicate] - php

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.

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

E-Mails of PHPMailer still getting into SPAM on some clients even after adding SPF-record

So I developed an automated Mailing System through which I send automated e-mails in PHP using the PHPMailer Extension.
First, most of the e-mails I've sent with the PHPMailer dropped into the spam of several clients, a well-known issue. I've checked with my host and we created an SPF record; and the result improved a lot.
Now by coincidence, I've found that some clients still seem to drop messages received via the PHPMailer script into their Spam folder. If I send the same e-mail manually, it doesn't happen, so it seems to be related to PHPMailer; so I must be doing something wrong.
The configs / code I'm currently using are / is :
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->Host = $host; // verified with my host
$mail->Port = 465;
$mail->Username = 'mymail#mydomain.com'; // verified with my host
$mail->Password = 'mypassword'; // verified with my host
$mail->SMTPSecure = 'ssl';
$mail->SMTPAutoTLS = true;
$mail->setFrom( 'mymail#mydomain.com', 'This is the Header of the E-Mail' );
$mail->addAddress( 'recipientsmail#hisdomain.com', '' );
$mail->isHTML( true );
$mail->Subject = 'This is the Subject of the E-Mail';
$mail->CharSet = 'UTF-8';
$mail->Body = 'HTML Content of the E-Mail';
$mail->send();
$mail->SmtpClose();
Please note that my hosting provider does not support any DKIM signature authentication; and I've also verified the correct reverse DNS lookup with my provider.
What am I missing folks?
I interestingly came about this post here; which writes that "IANA has reassigned a new service to this port [465], and it should no longer be used for SMTP communications."
The article recommends the use of tls and port 587 should be the default approach. Is this maybe the issue; or am I missing something out on PHPMailer? I just wanna be sure before switching anything, as I'm not at all an expert in the area of e-mails.. And well, I've never heard about this port issue.
Whether mail ends up in spam is very difficult to control. Implementing SPF and DKIM can help, but still provide no guarantees. If it was easy to bypass spam filters, spammers would do it, and they would not be spam filters! There's an article in the PHPMailer wiki about avoiding spam filters that you may find helpful. The headers in a received message will often tell you why a message has been put in the spam folder, for example listing the spamassassin rules it matched.
You say it works "manually", but is that sending from the same place (e.g. on your local machine)? You can get the raw text of messages sent through each route and compare them to see what's different (other than obvious things like message IDs).
That MailGun article is outdated. Since then, RFC8314 has not only "undeprecated" port 465, it's now recommended as the default because it eliminates a possible attack vector in the pre-encryption stage that SMTP+STARTTLS uses on port 587. Unfortunately it also makes it harder to debug from PHP, and denies the chance to do opportunistic encryption when encryption is not requested explicitly, so it's not the default in PHPMailer (yet).

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.

phpMailer not arriving in Hotmail?

After setting up SPF Record i still am here to verify why i cannot recieve this email to hotmail account. I am able to send it to gmail with no issues. Please confirm if the code is correct, and SPF record is correct:
<?php
require_once 'PHPmailer/class.phpmailer.php';
$mail = new PHPMailer();
$body = "Thankyou for your Purchase. <br/><br/> Here is your Policy! You are now Protected during your Travels.";
$mail->AddAddress('$payer_email');
$mail->From = "noreply#example.com";
$mail->FromName = "Name";
$mail->Subject = "Thankyou for Your Purchase";
$mail->MsgHTML($body);
$mail->AddAttachment("tosend/xxx.pdf");
if(!$mail->Send()) {
echo "There was an error sending the message";
$sql = "UPDATE purchases SET policy_sent = 'Not Sent' WHERE id = '$lastid' ";
$stmt = $mysqli->query($sql);
$mysqli->close();
exit;
}
echo "Message was sent successfully";
$sql = "UPDATE purchases SET policy_sent = 'Sent', email_to = '$payer_email' WHERE id = '$lastid'";
$stmt = $mysqli->query($sql);
$mysqli->close();
?>
Here is the SPF:
v=spf1 a mx include:secureserver.net ~all
Are all these correctly configured?
Use SMTP Auth, then Hotmail wouldn't complain anymore. Anonymous mails are considered as spam by almost all receiving servers.
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "mail.yourdomain.com"; // sets the SMTP server
$mail->Port = 26; // set the SMTP port
$mail->Username = "yourname#yourdomain"; // SMTP account username
$mail->Password = "yourpassword"; // SMTP account password
But ofc. depending on whether you have control over your SMTP or not, you should make sure, that basic stuff like reverse-dns-lookup is setup properly
Due to the discussion in the comments, I want to add a little bit more information about my thinking about why SMTP Auth will fix this:
IF you are using PHPMailer without the definition of an SMTP-Server, PHPMailer will operate in mail mode, which will just call the mail() function of php.
The mail-function itself will use the smtp-settings configured in the PHP-INI file, or the default values, which are listed here: http://php.net/manual/en/mail.configuration.php
defaults:
SMTP = "localhost"
smtp_port = "25"
Since the OP has configured a local mail server (or why would he setup MX-records?), php will now connect to this SMTP-Server without Authentication. The Server will accept the message and send it to the next server.
(Same applies if unix' sendmail is used)
Each Server in the chain and especially the receiving server can now see, that a private SMTP has been used and no Authentication has been provided.
That is already Spam-Score over 9000 because with a setting like that (theoretically) everyone could use that server to send mails!
Restrictions like only from localhost are ofc. not known by other servers, therefore that SMTP is considered to be an Open Mail Relay http://en.wikipedia.org/wiki/Open_mail_relay
Switching PHPMailer to SMTP-Auth (EVEN if still the local SMTP Server is used) will add This information to the Entry created by the server when forwarding the mail. The Entry will look like this:
Received: from SERVER1 ([xxx.xxx.xxx.xx]) by mydomain.de with ESMTPA
The trailing A after ESMTPA now tells the receiving Server, that Server1 has used a valid user account on mydomain.de to start the sending-attempt, which means that the SMTP-Server knows the origin of the mail and could provide information about the sender.
Still, the local SMTP-Server is not a known one, so in this case it might end up beeing greylisted and checked on various RBLs, which shouldn't be any problem in this case.
If the (local) SMTP-Server now passes ALL checks (Reverse-DNS Lookup, Greylisting, RBLs and what not) - the mail has a good chance to be successfully delivered, even if no remote smtp is used, because the server could be successfully authenticated as well as the sender using that server. (Otherwise no company could setup own servers)
So, using SMTP-Auth (Or any other authentication method) will also have an impact, even if no remote-SMTP Server is used.
Authenticated Mails are not a guarantee for not beeing considered as spam - but unauthenticated Mails are definitly ranked higher in the Spam-Score of common systems.
Had the same issue. I have checked the whole script 30 times. Each setting each . and , Checked and rechecked dns records dkim and spf 100 times.
Nothing worked.
I e-mailed hotmail/outlook/live about the issue. I didn't expect any help but within a few hours the problem was solved.
It wasn't in the script. Hotmail just blocks any e-mail that is sent trough most webpages. Even if your spam reputation is fine. They have placed the server's ip on a whitelist and the problem was solved.
They can change it back if they detect bad behavior. There's a limit for sending e-mail. If your server has a bad reputation they aren't likely to add the server to their whitelist. This can be an issue on shared hosting packages. Someone else on the same server where you host your website can f up the servers reputation.
Have you tested on a live server? If yes, what error did you get?
Have you enabled your phpmailer's debug to know what errors it has? If you are not using SMTP you can test with this
$mail->do_debug = true;
If you are using SMTP then use
$mail->SMTPDebug = true;
What output did you get from your debugger? You get a clue to where your problem is coming from from there.
I also suppose #dognose's answer by asking you to better use SMTP. That way your email passes through an authentication which would help indicate your identity was confirmed.
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "mail.yourdomain.com"; // sets the SMTP server
$mail->Port = 26; // set the SMTP port
$mail->Username = "yourname#yourdomain"; // SMTP account username
$mail->Password = "yourpassword"; // SMTP account password
If $mail->Port = 26; doesn't work you can try $mail->Port = 25;.
The SPF currently being published by your DNS is invalid because you have three records!:
# dig +short txt immortalinsurance.com
"v=spf1 a mx include:smtp.secureserver.net ~all"
"v=spf1 mx mx:v=spf1 a mx include:secureserver.net ~all include:secureserver.net -all"
"\"v=spf1 a\""
The first one is valid; You should delete the second two.
The content of this record is correct for your domain if you are sending mail from 182.50.130.73. The 'mx' stanza is not strictly necessary since it points at secureserver.net anyway, but it may save DNS lookups on receiving servers.
Before you start sending email using mail() or SMTP, make sure you create valid sender email account in your cpanel.
From = "noreply#immortalinsurance.com";
This is very important if not all emails you send will be tagged as SPAM and goes to junk.
Once this is done, you must setup domainkeys and SPF records. Having a dedicated IP instead of using shared given by your hosts will also help with the delivery.
Finally check your mail with all three major email providers such as Hotmail, Yahoo and Gmail.
You will probably have to break down the exact reason why step by step. I know for a fact Hotmail is using a rules based spam filter (which is quite strict) and Gmail uses more like a self-learning system, which could explain why Gmail accepts your message and Hotmail doesn't.
The following you can check though:
A spf-record only works on the domain the spf record is added and only works for mail sent from ips/domains defined in that spf. Therefore make sure your code is executed from a server that exists in your spf.
Using SMTP as proposed above can increase derliverability (but it really depends on the receivers' spam filter). If you're testing on a local server though, you can use SMTP to send the mails trough a mail server defined in the spf. Otherwise
If the mailserver you are using is an open relay, than that could (and probably would) be a reason to add extra spam points. Thus make sure you're using a closed relay (e.g. the mailserver you're using doesn't allow mails to be sent unauthenticated users)
Check if the mailserver you're sending from isn't on any blacklist (http://mxtoolbox.com/blacklists.aspx offers a great tool);
Use the message headers in Gmail to find out if Gmail sees any reason to add spam points to your message (Howto: https://support.google.com/mail/answer/22454?hl=en). There you can find if your spf works (by searching for 'spf=pass'). This might also work for Hotmail if your mail would be delevered into the spam folder.
One of the many rules spamfilter apply are keyword checking (which I think your mail contains a few off), the amount of text and such. Make sure the overall quality of your message is good;
PHPMailer uses php's mail function by default. You can change this to sendmail ($mail->isSendmail();, probaly doesn't work on Windows if sendmail is not set up properly) or perhaps check the settings of your php.ini what mailserver is used (use ini_set('SMTP', 'yourhost.com'); to override, but this only works on Windows as far is I know).
The user of the connection data $mail->Username must be the same as the $ mail->From, otherwise hotmail rejects the email or marks it as spam.
Also, if the email has images, it can also be marked as spam.
In summary, for everything to be perfect, send email without images and that the From is the same as the Username.

PHPMailer using SMTP username as senders email address

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.

Categories