Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PhpMailer doesn't sent mail in some cases #348

Closed
Redani opened this issue Jan 21, 2015 · 39 comments
Closed

PhpMailer doesn't sent mail in some cases #348

Redani opened this issue Jan 21, 2015 · 39 comments

Comments

@Redani
Copy link

Redani commented Jan 21, 2015

I'm getting really crazy trying to figure out what's going on with phpmailer :/
I'm using phpMailer Lite 5.1, then i downloaded phpMailer (not Lite) and still the same problem!
here is a portion of my code:

try {
// defaults parameters
$mail = new PHPMailer(true); // or PHPMailerLite
$mail->CharSet = "utf-8";
$mail->IsHTML(true); // send as HTML
$mail->AddReplyTo("[email protected]", "Do not reply");
$mail->SetFrom("[email protected]", "Site.com"); // works fine
//$mail->SetFrom("[email protected]", ""); // doesn't work :(

$mail->AddAddress(trim($email));

// subject of the email
$mail->Subject = $subject;

// HTML body
$mail->MsgHTML($msg);

// send the email
$mail->Send();

// clear all addresses and attachments for the next mail
$mail->ClearAddresses();
$mail->ClearAttachments();

echo "email sent";
}
catch (phpmailerException $e) {
echo $e->errorMessage();
}

With the first SetFrom, it workds perfectly, but when someone sends me an email from contact form, SetFrom should contain his email, in that case, it does'nt work all the time... And the worst thing is that, it doesn't output any error message!

I also have just noticed that if SetFrom is set to my yahoo email address, i receive a "Undelivered Mail Returned to Sender" email !! :/

Do you please have an idea of what that might be??

Thanks a lot for your help

@Synchro
Copy link
Member

Synchro commented Jan 21, 2015

Pleae don't use PHPMailer Lite. there is no advantage in doing so, it's out of date, buggy and no longer supported, at least by me.

Aside from that, most email providers (particularly yahoo and gmail) do not allow you to set arbitrary from addresses. You can pre-configure aliases in gmail, but you can't just use anything as it looks like forgery.

@Synchro Synchro closed this as completed Jan 21, 2015
@Redani
Copy link
Author

Redani commented Jan 21, 2015

Thank you Synchro for your answer.
OK, I have downloaded class.phpmailer.php and class.smtp.php that I'm now using!
Acutally I wasn't using arbitrary email addresses from gmail or yahoo, i was using my own ones but still it doesn't work!!

I'm now trying to use gmail SMTP but my web hoster apparently blocks connection on port 465, i'll call them to find out what to do!

Meanwhile, any suggestions for the problem i'm having??

Thanks

@Synchro
Copy link
Member

Synchro commented Jan 21, 2015

Read the docs, base your code on the examples provided. Don't use SSL on port 465; use tls on port 587.

By arbitrary, I mean anything other than gmail or yahoo addresses on each service respectively.

@Redani
Copy link
Author

Redani commented Jan 21, 2015

I followed exactely the gmail example but i still get an error:

SERVER -> CLIENT: 220 mx.google.com ESMTP ej10sm1813444wib.1 - gsmtp
CLIENT -> SERVER: EHLO site.com
SERVER -> CLIENT: 250-mx.google.com at your service, [217.160.90.97]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
CLIENT -> SERVER: STARTTLS
SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
CLIENT -> SERVER: EHLO site.com
SERVER -> CLIENT: 250-mx.google.com at your service, [217.160.90.97]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
CLIENT -> SERVER: AUTH LOGIN
SERVER -> CLIENT: 334 VXNlcm5hbWU6
CLIENT -> SERVER: b3B0aWd1cmFAZ21haWwuY29t
SERVER -> CLIENT: 334 UGFzc3dvcmQ6
CLIENT -> SERVER: QGRtaW5vZzJPMU8h
SERVER -> CLIENT: 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbvLb534-5.7.14 XIwYkG9J_wuAvmHiw4RnhyEfhwpG-6TQKbM9KjqVBTkonjaYi8zpBBanDB-sZO6s-G2JtD534-5.7.14 fKBXtIbsDlwuxfdv-hnYFbBNj5SswsYdGYrj0WA5Uka46T3X2REzXxTbeoNQkBi7eTdAk2534-5.7.14 uqPXJ-fEKmhauljHkI3-Atp-Pwqsmo8NnzL5gFlKtMPBxLaDyzkdg7UZWjRbRqFuzkEa_2534-5.7.14 3ZqmnxA> Please log in via your web browser and then try again.534-5.7.14 Learn more at534 5.7.14 https://support.google.com/mail/bin/answer.py?answer=78754 ej10sm1813444wib.1 - gsmtp
SMTP ERROR: Password command failed: 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbvLb534-5.7.14 XIwYkG9J_wuAvmHiw4RnhyEfhwpG-6TQKbM9KjqVBTkonjaYi8zpBBanDB-sZO6s-G2JtD534-5.7.14 fKBXtIbsDlwuxfdv-hnYFbBNj5SswsYdGYrj0WA5Uka46T3X2REzXxTbeoNQkBi7eTdAk2534-5.7.14 uqPXJ-fEKmhauljHkI3-Atp-Pwqsmo8NnzL5gFlKtMPBxLaDyzkdg7UZWjRbRqFuzkEa_2534-5.7.14 3ZqmnxA> Please log in via your web browser and then try again.534-5.7.14 Learn more at534 5.7.14 https://support.google.com/mail/bin/answer.py?answer=78754 ej10sm1813444wib.1 - gsmtp
CLIENT -> SERVER: QUIT
SERVER -> CLIENT: 221 2.0.0 closing connection ej10sm1813444wib.1 - gsmtp
SMTP connect() failed.

I'm only including class.phpmailer.php and class.smtp.php, but i don't think it's the issue!!

and here is my code:

$mail             = new PHPMailer();
              $mail->IsSMTP();
              $mail->SMTPAuth   = true;
              $mail->SMTPDebug = 2;
              $mail->Debugoutput = 'html';

              $mail->Host       = "smtp.gmail.com";
              $mail->Username   = "[email protected]";
              $mail->Password   = "pwd";
              $mail->SMTPSecure = 'tls';
              $mail->Port       = 587; //465

              $mail->setFrom('[email protected]', 'Matt');
              $mail->addReplyTo("[email protected]","Matt");
              $mail->addAddress("[email protected]", "My Name");

              $mail->Subject    = $subject;
              //$mail->Body       = $message;
              $mail->msgHTML($message);
              $mail->AltBody    = "Any message.";

@Synchro
Copy link
Member

Synchro commented Jan 21, 2015

If you're including those two classes, you're not following the example.

This isn't a PHPMailer problem - it's your gmail account. Have you tried doing what it suggests? Following that link?

@Redani
Copy link
Author

Redani commented Jan 21, 2015

Yes, I followed the links but it redirects me to my personal email instead of the email I used in $mail->Username to signup with!!

And don't you think it's caused by a misconfiguration in the server? or even something I should set in gmail account?

@Redani
Copy link
Author

Redani commented Jan 21, 2015

Following the links shows now some info about the correct gmail account, but it only shows some account info...
I'm sure it must be a little stupid parameter which is blocking everything! :/

P.S. I'm now using PHPMailerAutoload.php :D

@Synchro
Copy link
Member

Synchro commented Jan 21, 2015

You will need to sign out of your personal account and sign in with the one you're sending through. Probably worth googling the message as well.

@Redani
Copy link
Author

Redani commented Jan 21, 2015

that's exactly what I did!!
I think I'm gonna give up using gmail.smtp, but before, please i'd like to ask you one question. What's the benefit of using gmail.smtp??

@Synchro
Copy link
Member

Synchro commented Jan 21, 2015

Well, it's a reliable (usually!), well-specified SMTP service and many people already have an account, so it's quick and easy to set up. It also means that you don't need to have a local mail server running to send mail from PHP - though of course you can use any server for that.

@Redani
Copy link
Author

Redani commented Jan 21, 2015

OK, well, i'm so confused now, don't know if the problem comes from the server, or from the gmail account!!
Are they any specific settings that need to be done in gmail account to be able to use smtp.gmail? the more i check tutorials and videos about it the more i get crazy coz it's not working for me :-(

@Synchro
Copy link
Member

Synchro commented Jan 21, 2015

No, gmail is pretty standard SMTP - all you need is tls on port 587 and use normal auth.

This answer seems like a likely solution: http:https://stackoverflow.com/a/20645752/333340

@Redani
Copy link
Author

Redani commented Jan 21, 2015

I have already tried all the changes mentioned in the link you sent me!!
IT'S INCREDIBLE, have just hang up with my web hoster 1&1, they asked me to use port 25 and it WOOOORKED :D hard to believe :) I'll do some more testing and will get back to you ASAP ;)

@Redani
Copy link
Author

Redani commented Jan 21, 2015

OMG, it's a real nightmare!! as soon as I signed out from the gmail account, the problems starts again!!
I'm trying to figure out how to fix this!! :/

@Synchro
Copy link
Member

Synchro commented Jan 21, 2015

Did you see the comment in that question about allowing app access in gmail?

If you do use port 25 on 1&1, make sure it lets you use tls as well otherwise it's not worth using.

@Redani
Copy link
Author

Redani commented Jan 21, 2015

Yes, "Access for less secure apps" is enabled!!

I was using port 25 with tls when it worked for few minutes...

@Redani
Copy link
Author

Redani commented Jan 21, 2015

Hi,
After struggling the whole day, i came with the following conclusions...
1- setFrom("[email protected]", "Company.com") MUST ALWAYS be the company name to prevent the mail from being filtered by gmail or yahoo

2- if the email is being sent by someone through the contact form, WE MUST use the following:
$mail->setFrom("[email protected]", "Company.com");
$mail->addReplyTo("[email protected]", "");

with this, we can be sure to receive all emails...

as per the smtp.gmail, i finaly made it work again, but it was always showing the @gmail.com in "sender" field, i added a new email address i can send mails from, but in this case, it shows "sent via gmail" !
so i'm still thinking what to do

Hope this will help people who were in the same situation as me!

To be continued and a special thank you to Synchro for his help ;)

@Synchro
Copy link
Member

Synchro commented Jan 21, 2015

Yes - this is what I meant about not allowing arbitrary addresses, and reply-to is the appropriate workaround. It's quite right that they should do this, as if you say it's from the submitter, it's straight forgery (because they never sent the email message, you did) and it's becoming increasingly likely that the email will be blocked by their SPF settings anyway.

@Redani
Copy link
Author

Redani commented Jan 21, 2015

I'm thinking to use Mandrill with phpMailer, what do you think about it?

@Synchro
Copy link
Member

Synchro commented Jan 22, 2015

No particular feelings either way, but PHPMailer works well with it.

@Redani
Copy link
Author

Redani commented Jan 22, 2015

Well, i'm just happy the problem is solved :) thanks ;)

I'll have some very few questions please, I would love to have your opinion:
1- do I have to start all my emails with the following code ?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http:https://www.w3.org/1999/xhtml"> <head> <title>Mail Title</title> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> </head> ...
I just don't see how the title would be used since we mention an $mail->subject

2- what's the recommender $mail->Priority to use?

3- when using $mail->msgHTML() do we still need to add $mail->isHTML(true); ??

Thanks a lot ;)

@Synchro
Copy link
Member

Synchro commented Jan 22, 2015

  1. I'd recommend either old-school HTML4, or HTML5. Not a huge amount to be gained by using XHTML. Take a look at http:https://www.email-standards.org
  2. Leave as default
  3. No - msgHTML sets HTML mode.

@Redani
Copy link
Author

Redani commented Jan 22, 2015

1- I didn't find any info about doctype... :/
and are the head and title tags necessary ??

2- OK
3- OK thanks ;)

@Synchro
Copy link
Member

Synchro commented Jan 22, 2015

The doctype determines whether you're talking HTML4, XHTML or HTML5 for the rest of the document, so it sets the context for all other markup. Head tags are stripped in some clients (possibly hotmail), and some spam filters like the title tag to match the subject line. I suggest you make your HTML as clean and valid as possible - it's much easier fixing small problems from that position. Doctype, head and title are all required for valid HTML of any flavour, so leave them in, and do what they say - so don't use XHTML makup in HTML5, or vice versa.

@Redani
Copy link
Author

Redani commented Jan 22, 2015

Great, thanks for the explanations... ;)
Cheers

@Redani
Copy link
Author

Redani commented Jan 24, 2015

Hi Synchro, I'll have one last question please.
When I'm running some tests in local, it takes forever the page to render cause phpmailer can't be executed. how can i fix this?
P.S. i don't want to make it work by sending message when in local

Thanks

@Synchro
Copy link
Member

Synchro commented Jan 24, 2015

Just wrap a condition around it and enable/disable it in your config?

You could try running a fake SMTP server, there are some links in the examples folder - I use https://github.com/Nilhcem/FakeSMTP

@Redani
Copy link
Author

Redani commented Jan 24, 2015

Thanks for the fast reply
sorry I don't really undersantd what you mean, i shoudl wrap a condition around what?

@Synchro
Copy link
Member

Synchro commented Jan 24, 2015

The code that calls PHPMailer, something like:

if ($config['enable_email']) {
   $mail = new PHPMailer;
  ...
}

@Redani
Copy link
Author

Redani commented Jan 24, 2015

Ah Ok, i see the idea
and what about somethibg like this ?
if(!strpos($_SERVER['SERVER_NAME'], ".localhost")) $mail->send();

@Synchro
Copy link
Member

Synchro commented Jan 24, 2015

Whatever works for you. If you're not going to send you could just skip the message building steps too.

@Redani
Copy link
Author

Redani commented Jan 24, 2015

OK, thanks you very much and have an amazing week end ;)
Cheers

@deifos
Copy link

deifos commented Nov 15, 2015

I spent the whole morning trying to figure out why last night I was able to send email using PHPMailer just fine and now it does not work, but thanks to this conversation I found the problem.

..Hi,
After struggling the whole day, i came with the following conclusions...
1- setFrom("[email protected]", "Company.com") MUST ALWAYS be the company name to prevent the mail from being filtered by gmail or yahoo

2- if the email is being sent by someone through the contact form, WE MUST use the following:
$mail->setFrom("[email protected]", "Company.com");
$mail->addReplyTo("[email protected]", "");

..

Thank you for posting this Redani, I will never forget this one for sure.

@Redani
Copy link
Author

Redani commented Nov 16, 2015

You're very welcome deifos ;)
I'm glad it helped you, and this is the purpose of the community ;)
Good luck for the rest.
Cheers

@ruimelodev
Copy link

@Redani, thank you for your solution.
This was what helped me:

1- setFrom("[email protected]", "Company.com") MUST ALWAYS be the company name to prevent the mail from being filtered by gmail or yahoo

I was using gmail.com as my Postfix in my MAMP server.

@Redani
Copy link
Author

Redani commented Mar 5, 2016

You're very welcome, and glad it helped you ;)

@Parvezkhan
Copy link

I am using PHPMAILER.
My Email is working fine with all the email id like [email protected], [email protected]
but it is not working with some Email id like [email protected]

when I am sending the email through PHPMAILER to the Email id like [email protected] it is not receiving at recipient end.

Can somebody help me here.

@Synchro
Copy link
Member

Synchro commented Apr 4, 2016

@Parvezkhan you need to define "not working" and read the troubleshooting guide. Please don't post comments on unrelated, closed tickets.

@codingbrains2-design
Copy link

getting "Could not access file".. path of attachment is correct..

@PHPMailer PHPMailer locked and limited conversation to collaborators Jun 6, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants