Jump to content

what's wrong with my code?


fruid
 Share

Recommended Posts

What's wrong with my code? It just won't send emails. It worked before when not using wiremailSMTP but now it stops. This is used in a contact form.

It has to be the code because when I run a test in the module, it works fine and the email reaches me.
Even the Promailer-Newsletters using WireMail SMTP work fine now.
Even when I put some debug code on the template to be send to me via email with every email, that email reaches me. 
But somehow the email in question, send with contact form does not.
BTW if-condition works fine cause redirection works as well.

Here's my code:

if (isset($_POST['sendform']) && $captcha->verifyResponse() === true) :

    $givenname = $input->post->text('givenname'); 
    $familyname = $input->post->text('familyname'); 
    $fullname = $givenname.' '.$familyname;
    $fullname = ucwords($fullname);

    $email = $input->post->email('emailaddress'); 
    $subject = $input->post->text('subject'); 
    $message = $input->post->text('message'); 

    $m = wireMail();
    $m->to('myemail@mydomain.com', 'my Name');
    $m->from($email, $fullname);
    $m->subject($subject);
    $m->body($message);
    $m->send();
    $session->redirect($pages->get("/kontakt/danke/")->url);

endif;

I recently installed a SSL certificate on the webserver, but that should be irrelevant, right?

Further settings:

port: 587
Allow Connection without Authentication: no
SMTP User and SMTP Password provided
Use Start-TLS: no
TLS Crypto method: 1.2
Use SSL: no
Allow self signed certificated: no

Thanks for help!

 

Link to comment
Share on other sites

OK I just realised that an email send via my contact form only go through when $email is the actual SMTP-user's email address

$m->from($email, $fullname);

So I understand that by enforcing authentication when sending an email, the ->from email needs to be the authentified user's email. Thus the email that I receive can't and won't have the user's email. That's only possible with a connection without authentication? So I need to include the email somewhere else, in the email's body? So "replying" is not possible as such.

Is my assessment accurate?

Link to comment
Share on other sites

56 minutes ago, fruid said:

So I understand that by enforcing authentication when sending an email, the ->from email needs to be the authentified user's email.

In theory, no, you can use any field you want for the From field in the e-mail. In practice however, many email providers will block this, as it's essentially spoofing. And even if your provider doesn't reject emails like this entirely, they will probably go directly to the spam folder, as your server probably is not authorised to send mail from this domain (using SPF, DKIM or similar methods). The mails might even be rejected entirely, depending on DMARC records.

And for good reason – you wouldn't want just anybody to be able to send spam that appears to have been sent from your address, right?

Quote

That's only possible with a connection without authentication?

You shouldn't send unauthenticated emails, and by the way you also shouldn't send emails without SSL – but that's a separate issue.

56 minutes ago, fruid said:

So "replying" is not possible as such.

Yes, it is. That's what the reply-to field in an email is for. This way, you can have a sender address (From field) that's clearly coming from your domain (something like no-reply@your-domain.com) and a different recipient for answers, which is the correct way to approach this.

That said, I have encountered one hosting provider that blocks emails where either the from OR the reply-to email don't belong to a domain that is registered with that provider. In this case, you'd have to find a workaround, like sending the email to both recipients and instructing them to hit reply all. Or integrate messaging into your application instead of using email. Alternatively, you might use mailto links with pre-filled recipient, subject line and body and instruct your users to send their own mail. Which might be cleaner in terms of email security / authentication, though it depends on your application.

  • Like 1
Link to comment
Share on other sites

23 minutes ago, MoritzLost said:

many email providers will block this, as it's essentially spoofing

Yes, our provider actually does seem to block it at least when using Authentication, regardless of whether or not I "allow" sending without.
And that's good, I must say I always found it strange that it's possible in theory to send an email from whatever email, given the right insecure email server.

25 minutes ago, MoritzLost said:

Yes, it is. That's what the reply-to field in an email is for. This way, you can have a sender address (From field) that's clearly coming from your domain (something like no-reply@your-domain.com) and a different recipient for answers, which is the correct way to approach this.

Yes, thanks, I found the ->replyto() method and it works like a charm. Just wondering now, I agree noreply@ is a very good practice, but since WireMail needs to be logged in with the sender's email-account, I need to create a noreply@ email-address and mailbox on my email-server?

Thanks so far

Link to comment
Share on other sites

2 hours ago, fruid said:

Just wondering now, I agree noreply@ is a very good practice, but since WireMail needs to be logged in with the sender's email-account, I need to create a noreply@ email-address and mailbox on my email-server?

Not necessarily. The username of your email account is independent from the sender address – this is why, for example, you can have one email account with multiple associated email addresses. Though some providers don't make that distinction clear, and nowadays your username is usually the primary email address, so most people (in my experience) are not aware that those are two different things.

WireMailSmtp needs the username of the account to log in, and often this is the same as the sender address, but it doesn't have to be. This is why you can specify the username and sender address separately. WireMailSmtp doesn't care about the domain the sender address belongs to, any restrictions regarding that are done by the mail provider.

What matters for verification and spam prevention is the domain you're sending from. If you're sending from @your-domain.com and you've properly set up the SPF and DKIM records for your mail server, your mail server can send emails from every address @your-domain.com without problems. Of course, any mail provider / server tool can add any restrictions for sender addresses they want on top of that. For example, if you send your Email through Gmail, it probably won't let you use a sender address that doesn't belong to your account. So it comes down to what email provider you're using.

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

OK so I'm in a pickle now, no email module seems to work.

As mentioned above, I had been using the php mail function before, which worked ok for some time, but then not anymore.

So I learned about WireMail SMTP and that sending via SMTP is always better in terms of spam protection. As required by the host (World4You), I setup an authenticated user in the settings with port 587. This worked fine for some time, now it doesn't anymore.

Now I installed SwiftMail which also has test connection and test email features. I tried these tests, both are claimed successful, no errors, but no emails get received. This is scary! Is there any mail module that actually works? There are no errors neither on PW nor on the server, just success messages from PW.

How to debug this stuff? Need help urgently!

Link to comment
Share on other sites

Thanks but no, that doesn't seem to be the problem. I'm now using WireMailPHPMailer which works fine. The contact form at least which uses the code and API I wrote on the contact form's template file. So that's good, at least I know the module is working and the connection to the SMTP server is established. Rock-mail-logger is also good as a log-tool.

Still having issues with sending newsletters with Promailer via WireMailPHPMailer though, which doesn't use any custom code AFAIK, just the settings in the module. But that's to be discussed in the VIP support.

Link to comment
Share on other sites

@fruid I'd still test your mail with the mail-tester tool mentioned above to make sure your email doesn't appear "spammy". Depending on your settings and whether you're sending through the mail server of your hosting your mail may appear to be unauthenticated. Anyway, it's curious that WireMailSmtp isn't working but WireMailPHPMailer is, are you using the same settings for both?

By the way, I'd be slightly wary when the provider tells you to use port 587. In general, TLS-encrypted mail should use port 465. Port 587 is for STARTTLS, which starts SSL-encryption only after a connection has been established, which means you're susceptible to a man-in-the-middle attack. Your hoster only supports port 25 (plain text) and 587 (STARTTLS), that's a red flag.

Anyway, if you're using the suggested settings for SMTP connections by your hoster and they're not working, I'd just talk to them to see what might be the problem.

  • Like 1
Link to comment
Share on other sites

mails get sent now, contact form and newsletters. However, I struggle with sending HTML content with ProMailer via WireMailPHPMailer. The issue definitely is with WireMailPHPMailer because HTML emails get sent just fine via regular php. Also, on the newsletter template I have

if($input->get('type') === 'html') { ?>
    <!DOCTYPE html>
    <html>
    <head>
        <!-- <meta http-equiv="content-type" content="text/html; charset=utf-8" /> -->
        <meta http-equiv="content-type" content="html; charset=utf-8" />
...

etc.

And it always enters this block, no matter what is sent, but it always "auto generates" text, I guess, and strips the HTML.

Don't know why. 

Link to comment
Share on other sites

OK I had some sort of success with the contact form. Apparently 

$mail->AltBody = $message;

destroys the line-breaks of the message. When I remove this, it sends emails with line breaks, that's all the message via contact form needs anyway.

But I'm still having troubles with the newsletter though, I'd appreciate if someone could just thought-shower with me, I'm out of ideas.

It doesn't use any more code other than what is in the email's template (shown above) which is the code provided template when you buy it (!). Other than that, as far as I know, it completely relies on what I configure in the modules' settings. But no matter what I do, it just won't send any markup or css styles. I don't even understand what's happening and google is no help either.

So is it the ProMailer module? (I hope not), the WireMailPHPMailer? (positive), the mail server settings? or my email client? (unlikely)

I also changed the content-type of the template for the promailer-email template for the newsletter emails to "text/html". No success.

Thanks for help!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...