Jump to content

WireMailSmtp


horst

Recommended Posts

ok, this way it could be that some different mails has to be sent parallel, right?
 
You can go this way: (simplified)
 
    - sentLogReset isn't needed in your case, because you collect all recipients in the field $message->recipients.
   
    - you need to hook into sentLogGet, here you read all recipients from $message->recipients.
 
    - for each successfull sent message you should drop the recipient from the $message->recipients field in the hooked method sentLogAdd
      (don't forget to save the page)
      here you also can add a message to a textarea field
 
      Edit: striked the above, - see this post, it has a more correct solution
 
This way it should be possible to add some hundred recipients to the initial collection and if a script execution got interrupted, it could be resumed with the next cron run.
 
The only thing that you have to take care for is to determine if a page is allready sending. (no simultaneous sending of the same page!)
 
Hhm, maybe also it should not send different pages simultaneous?! Only one page after each other?
 
 
The used php libs allready provide personalized emailmessages in a faster way than to create and encode a new mail for each recipient. So, to use it in its meant fashion, it is needed to extend the current module. If you like and if it is possible that you can send me (only) the basic template code for the emailpages we can achieve that together.
 
 
Edit:
Oh, hasn't read your gists before.
 
Some questions:
- Is it possible to setup the cron to only start a new instance if the previous started one (5 minutes before) has allready finished? (but skip it if the previous one is still running)
 
- What would be the max number of recipients you can think of you will be collect for a mail (in the next 2 years) using this approach?

Edited by horst
Link to comment
Share on other sites

Hi Horst -

regarding the cron - since the cron just executes the page with this template (only way i could do it for the moment), i guess the thing would be to have some logic at the top of the template file to check is wiremail is still running? would that be possible? i definitely see your concern as far as scalability and different use cases; In my case i know for sure that i wouldn't have 2 messages scheduled 5 min apart with enough recipients to consume more that 5 min of sending time; but it is definitely something that should be accounted for...

When i first started this i was initially only thinking of sending to maybe 1-5 recipients at a time; now i figure that i could possibly need to send out to as many as 150; I was sort of figuring that when/if i get to the point of needing to send to a larger # that i would use a different system;

Another thing that i might play with is the Mandrill web hooks - it lets you enable posting back to a URL the json response for different events, like mail sent, mail opened, bounced etc; could be cool to have some fields in processwire to show the results of the send without having to go and look at the mandrill interface each time... I'll let you know how this goes.. should have some progress on it in a few days; the other good thing about reading back the mandrill responses is that they don't store that data for very long, so would be good to keep those records in PW...

Link to comment
Share on other sites

@Macrura: I think with a peak of 150 recipients you can go like in your second gist with the replacements (regarding personalization) and send them one after the other. But you need to use sendBulk(true) !!

Has you tried to use gmdate? Did it solve the issue?

// LOGGING:

you need to add a field to the messages template / pages that can collect the successful used recipients emailadresses!

(I use a textfield or simple textareafield named $message->recipientsSuccess in the following example)

// at the top of the template add the hooks

    // the sentLogReset isn't needed to hook into.
    // If you want to reset it, just delete the value of the $message->recipientsSuccess field!

    // this hook read in all successful used recipients
    wire()->addHook("wireMail::sentLogGet", function(HookEvent $event) {    
        $p = wire('page')->tmpMessagePage;  // get the current processed message-page
        // read the emailadresses from the recipientsSuccess field
        $emailaddresses = unserialize(trim($p->recipientsSuccess));  // you may also use json instead of serialized code or any other solution
        if(!is_array($emailaddresses)) $emailaddresses = array();    // should return an array !!  
        $event->replace = true;
        $event->return = $emailaddresses;
        return $emailaddresses;
    });

    // this hook add recipients to the successful collection and log the success
    wire()->addHook("wireMail::sentLogAdd", function(HookEvent $event) {    
        $p = wire('page')->tmpMessagePage;  // get the current processed message-page
        $emailaddress = $event->arguments[0];
        $logentry = "From: {$p->tmpFrom}; To: {$emailaddress}; Subject: {$p->tmpSubject}\n";
        // here you need to add the recipient to the successfull recipients collection
        $tmp = unserialize(trim($p->recipientsSuccess));  // you may also use json instead of serialized code or any other solution
        if(!is_array($tmp)) $tmp = array();               // should return an array !!
        $newRecipientsCollection = array_merge($tmp, array("{$emailaddress}"=>"{$emailaddress}"));
        $p->of(false);
        $p->recipientsSuccess = serialize($newRecipientsCollection);
        $p->email_log = $p->email_log . $logentry;
        $p->save();
        $event->replace = true;
        $event->return = true;
    });

When starting the wiremail instance, you need to setup it to use the sentLog functionality:

$mail = wireMail()->useSentLog(true)->sendBulk(true);  // don't forgett this !!!

At the end of your $messages loop you need something like this:

// Send

    // add some temporary vars to the current page, so it simply can be read within the hooked functions
    $page->of(false);
    $page->tmpFrom = $message->identity_select->email;
    $page->tmpSubject = $title;
    $page->tmpMessagePage = $message;
    $page->save();
    $page->of(true);

    // send the mails
    $numSent = $mail->useSentLog(true)->sendBulk(true)->send();  // useSentLog: you also can set it here !

    // ready with this $message page 

I haven't tested it but something like this should work & you get the idea I think. :)

Link to comment
Share on other sites

  • 1 month later...

Hey, Can anyone help with me this?

I'm trying to send mail with this module, however I keep getting this in error log "No unauthenticated relaying permitted".

How can I resolve this?

Note:

I'm using ipage shared hosting environment. Ipage told me i don't need to log in to their mail server to send mail. So i'm not sure what I am doing wrong.

Code I'm using is this...

$mail = wireMail(); // calling an empty wireMail() returns a wireMail object
 
$numSent = wireMail($to, '', $subject, $textBody); // or with a default sender emailaddress on config page 
  • Like 1
Link to comment
Share on other sites

  • 3 weeks later...

The connection-test uses your credentials and try to establish a connection to the server you has entered in the configscreen together with your credentials.

If the connection-test always succeed, there is no reason why the connection when trying to send a mail shouldn't do so too. (Because it is 100% the same code used in both cases, and the same credentials, empty or not.)

The error you are getting tells another story: "No unauthenticated relaying permitted"

- Do you have a personal SMTP account at ipage?

- Have you asked them if you need credentials for sending through your personal SMTP-Account and they have answered to you that you do not need those?

- Or do you have asked them if you need credentials for sending emails via php, and they think you mean simply using the php mail() function through your webhost?

The wiremailSmtp module cannot use php's native mail() function. This can be used with the base class wiremail and without credentials.

  • Like 2
Link to comment
Share on other sites

I contacted iPage again, and indeed you were right, I did need the credentials for the SMTP-account to work.

Before they told me I didn't, maybe as you said, they may have been thinking i was using the mail() function.

Once I added the credentials for my account, it worked beautifully.

Thank you very much Horst!!! :)

  • Like 1
Link to comment
Share on other sites

If the connection-test always succeed, there is no reason why the connection when trying to send a mail shouldn't do so too. (Because it is 100% the same code used in both cases, and the same credentials, empty or not.)

I had a similar situation the other day with a gmail smtp setup. The test connection worked fine, but the email didn't send. Turns out the test worked with a few different config setups, but the email only sent with one specific setup. So I think a successful test connection is not always a guarantee that everything is set up correctly. Not sure that your module can do anything about that though.

Link to comment
Share on other sites

I had a similar situation the other day with a gmail smtp setup. The test connection worked fine, but the email didn't send. Turns out the test worked with a few different config setups, but the email only sent with one specific setup. So I think a successful test connection is not always a guarantee that everything is set up correctly. Not sure that your module can do anything about that though.

Ok Adrian that may be right, but I think you have entered your credentials for the SMTP account(?). In this special case JasonS hasn't entered any credentials and got everytime a "No unauthenticated relaying permitted" in the error-log. Also he said that the test-connection works everytime! Hhm? Finally he has found that he need credentials for using the SMTP server, (not a big surprise at all, :lol: isn't it?). The times when you could use a mail relay without authentication are gone for a decade or two! :) We have 2014.

Link to comment
Share on other sites

Hey horst - I am not really up on the latest with this, but just to give you an idea, I can get a successful test with gmail with only:

smtp.gmail.com 

587

use START_TLS

with no credentials entered at all. Not sure if this is unique to gmail. I agree though, I thought most SMTP servers required authentication these days. It seems that gmail doesn't actually send a message, but the test is still successful without.

BTW, the settings that actually do send an email via gmail for me are:

smtp.gmail.com

465

use SSL

email and password as credentials

NB: I actually need to turn off use START_TLS

  • Like 1
Link to comment
Share on other sites

Sure?

This would make the testconnection nearly useless. The only reason for it is to test if one has no typos in the server settings and credentials.

I will try this gmail and with other servers and report back.

Link to comment
Share on other sites

Wow! I have found out that in the Connect() method of the base SMTP class it comes to a point after trying to start a TLS connection that a variable $success is set to true if the smtp-server simply supports TLS.

After that a if condition checks if there are settings available for user, if not it passes through to the end of the method with $success set to true! A Bummer!

If there is given a username, it trys to authenticate that user and the variable $success is set to that result.

So, at least this is not a bug, because in the past or in private networks SMTP servers could be configured to work without authentication, that is what this class does.

What should I do with this situation? I tend to ignore a usage without user authentication. This way it tries everytime to establish a connection via authentication, what will fail with an empty user or pass.

Good to know that typos would be detected in the past but only empty usernames raised this behave.

Now the class gives two errors:

WireMailSmtpConfig: ERROR: SMTP settings did not work.
WireMailSmtpConfig: 535 5.7.8 Error: authentication failed
Edited by horst
  • Like 3
Link to comment
Share on other sites

I have updated the Github repo and the entry in the modules directory to version 0.1.9

@JasonS: sorry that I tend not to believe that a server says success on testconnection and then returns a "No unauthenticated relaying permitted" error. Together with the confusion that the hoster has said it needs no credentials, it sounded too unlikely for me. But it was real. I have liked all posts of you here in the thread trying to say appologize. (If you post one more I will like it too) :-[

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...
I posted 2 times in another topic about the SMTP settings for Google.  Here is the consolidated information from those posts:

If you use a Google email address, this will work:

 
post-756-0-39162500-1413431424_thumb.png
post-756-0-38911900-1413431441_thumb.png
 

Other major providers are setup similar (you just need to check what their SMTP requirements are - this information is easily found, otherwise ask your hosting provider for correct setup and ports)

-------------  Second Post -----------------

I will & here is additional information regarding Gmail.  There are specific variations that will work and sometimes people don't understand why an option is needed.  These links should educate anyone on why certain settings work and others don't.

  • Like 8
Link to comment
Share on other sites

I don't know why but when I use my work email address which is managed by Gmail, but is not a @gmail address, I need to use:

post-985-0-83709400-1413492743_thumb.png

Nothing else seems to work for me except this.

Hopefully that might be a useful addition to @cstevensjr's excellent post.

  • Like 4
Link to comment
Share on other sites

Adrian, that's a valid sequence when using SSL (Port 465 is required).  Port 587 requires TLS.  The first link I posted explains what normally works, according to Gmail themselves.  I host with Dreamhost and when I was using ActiveCollab, I could use either or both within that application.  Gmail has other relay addresses that also work.

Here's Dreamhost's take on SMTP/SSL/TLS

http://wiki.dreamhost.com/Secure_E-mail

I'm no Gmail or SMTP expert, so I usually just play around with the settings until I get one that reliably works.

I don't think there is a default right answer to getting your mail to send reliably, because we are dealing with different email vendors, Operating Systems, Webhosts and other factors we don't even know about.

  • Like 6
Link to comment
Share on other sites

  • 3 months later...

Hi Horst: When testing the connection and 'use SSL' is checked these 2 notices appear. (2.5.17 dev)

Notice: Undefined property: smtp_class::$result_code in /Users/martijn/Sites/domains/newsletters/htdocs/site/modules/WireMailSmtp/smtp_classes/smtp.php on line 1032

Notice: Undefined offset: 0 in /Users/martijn/Sites/domains/newsletters/htdocs/site/modules/WireMailSmtp/smtp_classes/smtp.php on line 1042
  • Like 1
Link to comment
Share on other sites

@Martijn: I cannot replicate this. I have no errors in the logs and I have no error outputs. Error display is set to strict all. I'm on PW 2.5.11

Which version of WiremailSMTP do you use?

Do you have set a smtp- hostname, user and pass? Or do you have left one / some fields empty?

Link to comment
Share on other sites

  • 2 months later...

Regarding No. 1:

$data = $modules->getModuleConfigData('WireMailSmtp');
$data[$key] = $value;
$modules->saveModuleConfigData('WireMailSmtp', $data);  

Fill in key and value yourself. It's the same for any other modules settings.

  • Like 2
Link to comment
Share on other sites

  • SebastianP changed the title to Strange PHP error in WireMailSmtp: unexpected fully qualified name "\getmessage"

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
×
×
  • Create New...