Processing an Email Bounce back in CakePHP and Postfix - php

I'm trying to handle bounced message and send to a responsible System Administrator.
I use CakePHP Email Component to send the message. On server side, I use postfix to transport the message.
function sendAsEmail($data) {
$Email->sendAs = 'html';
$Email->from = $user['Sender']['username'] . '#example.com';
$Email->return = Configure::read('App.systemAdminEmail');
$Email->bcc = array($data['Message']['recipient_text']);
$content = 'Some content';
$Email->send($content);
}
As you can see above, I set the $Email->return to sysadmin's email which it will send all the bounced message.
On postfix configuration, I tried creating a bounce.cf template and set bounce_template_file. http://www.howtoforge.com/configure-custom-postfix-bounce-messages
How do I get the bounced message and send it to System Administrator?

I think what you'll need to do is to use an SMTP (or I suppose POP3) connector for PHP. Then you'll basically have to create your own PHP email client that will login to the server, ask for the messages that have been bounced, and parse them appropriately.
I would think there would be a CakePHP component for this, but I can't find one.
I would recommend that you use an Envelope Header in your email. Otherwise you'll be stuck trying to parse the recipient server bounce, and those are very very inconsistent. If you use the VERP (variable envelope return protocol?) header, you can encode a unique hash into the email address which should be really easy to parse out in your PHPEmailClient.
More info on VERP: http://en.wikipedia.org/wiki/Variable_envelope_return_path
Cake-specific VERP stuff: http://www.mainelydesign.com/blog/view/setting-envelope-from-header-cakephp-email-component
I also highly recommend that you look into using SwiftMailer. It has a lot of plugins; you might find a base PHP SMTP client that you can easily modify to do what you need. http://swiftmailer.org/

Related

How Use Sendinblue with PHPMailer

I'm setting up an email configuration with PHPMailer and just work fine with local email in my office, but since I want use Sendinblue as an email API I just stucked and it didn't work.
I tried to find some suggest to fixed it but stucked. Did anyone try to figure out what I'm looking for about Sendinblue with PHPMailer?
With PHPMailer Library in background, I usually use this code to run my program
function send_mail2($from,$from_name,$to,$to_name,$cc,$cc_name,$cc2,$cc_name2,$cc3,$cc_name3,$subjek,$template,$is_smtp,$usermail,$password){
$sendmail = new PHPMailer();
if($is_smtp = 'Y'){
$sendmail->IsSMTP(); // enable SMTP
// $sendmail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$sendmail->SMTPAuth = true; // authentication enabled
$sendmail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$sendmail->Host = "192.x.x.x";
$sendmail->Port = 465; // or 587
$sendmail->Username = "xxx#xxx.com";
$sendmail->Password = "xxx";
}
$sendmail->setFrom("xxx#xxx.com","xxx#xxx.com"); //email pengirim
$sendmail->addReplyTo("xxx#xxx.com","xxx#xxx.com"); //email replay
$sendmail->addAddress($to,$to_name);
$sendmail->AddCC($cc,$cc_name);
$sendmail->AddCC($cc2,$cc_name2);
$sendmail->AddCC($cc3,$cc_name3);
$sendmail->Subject = $subjek;
$sendmail->Body=$template;
$sendmail->isHTML(true);
if(!$sendmail->Send()) {
return "failed";
}else{
return "success";
}
}
I just want to find how to use Sendinblue with PHPMailer.
First of all, it looks like you're using an old version of PHPMailer and have based your code on a very old example, so upgrade.
It doesn't look like you're even trying to use sendinblue; you're pointing your script at a local mail server. Because you're using a literal IP address, SSL will never work because it will fail to verify the certificate.
If you want to use sendinblue directly from PHPMailer, you need to use sendinblue's SMTP servers, and that will be covered in their documentation. If they don't provide an SMTP service (like say mailgun does), you will need to use their HTTP API instead (which has plenty of documentation). You can't use PHPMailer for talking to that, though you can use it for generating messages to send, for example by doing everything as you currently are except don't call send(); do this instead:
$message = $sendmail->getSentMIMEMEssage();
This will give you a fully-formatted RFC822 message ready to submit to an HTTP API.
When you need to configure sendinblue with PHPMailer,
At that time, first, get all configuration detail from your sendinblue account
like Host, Username, Password
And then your PHPMailer use getSentMIMEMEssage(); instend of $mail->isSMTP() or $mail->isMail()
Please check the below screenshot for reference.

Add mail to Send folder with SwiftMailer

I'm using SwiftMailer for PHP from swiftmailer.org
Everything works well but I wonder if there is a way to add the sent message into the sent folder from the mail account that SwiftMailer is sending from?
That's all, have a nice day.
According to the developer, swiftmailer cannot copy to Sent folder because it is a mail sender and not mailbox manager.
As mentioned on the github page:
Swiftmailer is a library to send emails, not to manage mailboxes. So, this is indeed out of the scope of Swiftmailer.
However, someone from php.net posted a solution that might work for you:
Use SwiftMailer to send the message via PHP.
$message = Swift_Message::newInstance("Subject goes here");
// (then add from, to, body, attachments etc)
$result = $mailer->send($message);
When you construct the message in step 1) above save it to a variable as follows:
$msg = $message->toString();
// (this creates the full MIME message required for imap_append()!!
// After this you can call imap_append like this:
imap_append($imap_conn,$mail_box,$msg."\r\n","\\Seen");
I've had similar problem and Sutandiono's answer got me into right direction. However because of completeness and as I had additional problem connecting to an Exchange 2007 server, I wanted to provide a complete snippet for storing message to Sent folder on IMAP:
$msg = $message->toString();
// $message is instance of Swift_Message from SwiftMailer
$stream = imap_open("{mail.XXXXX.org/imap/ssl/novalidate-cert}", "username", "password", null, 1, array('DISABLE_AUTHENTICATOR' => 'GSSAPI'));
// connect to IMAP SSL (port 993) without Kerberos and no certificate validation
imap_append($stream,"{mail.XXXXX.org/imap/ssl/novalidate-cert}Sent Items",$msg."\r\n","\\Seen");
// Saves message to Sent folder and marks it as read
imap_close($stream);
// Close connection to the server when you're done
Replace server hostname, username and password with your own information.

Get exact mail message after calling mail() - PHP

After calling the mail function I would like to get the complete mail message that I just sent. Not just the subject and content field but also auto generated information such as Date and Content-Transfer-Encoding. That is the entire message. How do I do that?
PHP doesn't actually send the mail, so it won't know about any of that. All the mail() function does is pass your stuff on to the sendmail binary (or whatever SMTP server you use instead) and return whether or not that was successful. The rest is up to the SMTP server.
Best suggestion I can offer is to have the mail BCCd to an account you control and parse the desired info from there.

Swift Mailer can address be banned for mass mailing?

I need to send over 2000 mails and I am using Swift Mailer library for it.
We have own server and it has both SMTP and sendmail transports. I am using SMTP:
$transport = Swift_SmtpTransport::newInstance('localhost', 25);
All mails are send fine to few people, but I'm afraid that we will be banned when we send mass mail.
I don't really know what means "banned" and how it looks like, but I'm afraid about the aftermath.
So, is it true, that such "ban" exists and how to implement mass mailing with Swift Mailer in a right way?
P.S.: My code looks like:
// Create the Transport
$transport = Swift_SmtpTransport::newInstance('localhost', 25);
// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
// Create a message
$message = Swift_Message::newInstance($message_theme)
->setFrom(array($sender => $name))
->setTo($emails)
->setBody($message_text,"text/html")
;
try {
// Send the message
$result = $mailer->send($message);
}
catch(Exception $e) {
echo "Error: ".$e->getMessage();
}
As I'm hoping you will not use this for spam!!!
Here are some things to do:
try to same different Emails (change name of the recipient in body)
send emails once every 3-4 seconds and not 100 emails/second - it should send 2000 emails in about 2-3 hours.
Blacklisting/graylisting does indeed exist and there are some best practices you can implement to try to avoid these issues. For 2,000 emails, as long as your headers are legitimate, there is nothing fishy going on in your body text, and your recipients are on different domains, you should not run into this issue. However, as khomyakoshka mentions, the above code is incorrect and you should use a loop to send each email. This avoids exposing your entire mail list to each user.
Some additional best practices:
1) Swiftmailer offers plugins (http://swiftmailer.org/docs/plugins.html) that will help you send bulk email. Of particular note are the Throttler and AntiFlood plugins.
2) If you intend on modifying the contents of the mail to tailor to the recipient, consider using the Decorator plugin (also mentioned on the plugins page) for this task.
Hope these tips help.

Why is email not being sent through PHP even though it is being reported as sent?

I'm trying to sends mails in PHP. The code I used for sending a mail in CakePHP is given below. I get the message 'Simple Email Sent' in my web page but the mail is not delivered to my inbox. Am I missing something?
The values in the to, subject and link fields are set with the values entered in the user interface.
$this->set('to',$this->params['form']['to']);
$this->set('subject',$this->params['form']['subject']);
$this->set('link',$this->params['form']['link']);
$this->Email->to = to;
$this->Email->subject = subject;
$this->Email->from = 'someperson#somedomain.com';
$this->Email->delivery= 'mail';
$this->Email->sendAs='text';
$this->Email->template = 'simple_message';
//$this->Email->send(link);
if ( $this->Email->send(link) ) {
$this->Session->setFlash('Simple email sent');
} else {
$this->Session->setFlash('Simple email not sent');
}
On a Linux system, you'll probably have a sendmail script installed already, and PHP will use that. If this is what you have and it's not working, then I'd look for mail configuration problems in your Linux system itself.
On a Windows system, you'll need to configure the SMTP server you want PHP to send mail to. The normal way to do this is in php.ini. The instructions for this are here.
Unless you have set Email->delivery this should be the same for CakePHP - it should default to whatever PHP uses.
Note: If you are using your own Linux install, it could just be that your ISP is blocking port 25, which your mail server is using. In that case you'll need to configure linux to route email to your ISP's email server. Maybe this will help?
Since when is 'to' (line 4) a valid destination email address?
You need to use variable syntax for setting to 'to' line, and the 'subject' line. Those lines should read
$this->Email->to = to;
$this->Email->subject = subject;
Also, I believe there is an attribute in the Email component called error (I cannot find it in the documentation currently) that will help you debug. This may not be totally correct; I use the Email component with SMTP, and there is an attribute that gets set by the Email component called smtpError. I believe there is one called error that you can use to check for an error -- it should contain code that will tell you where your problem lies.
In case that's an incorrect statement, you can always do a var_dump( $this->Email ); after you try to send an email. That will dump the entire contents of the object, so you can see if you have set attributes correctly, and it should help you find out what the error attribute is named.

Categories