How to verify a mail has been sent when using Zend_Mail? - php

I am using the Zend framework to send mail. Once the config is done and the code written it all boils down to one call:
$Mail->send($Transport)
How can i check that this mail has been sent correctly? I read somewhere that Zend Mail throws an exception but other people have said this is sometimes not the case.
What's the bulletproof programmatic way to ensure mail has been sent properly when using Zend_Mail?
EDIT: When i mean sent, i mean sent to the SMTP server.

Generally Zend_Mail will throw an exception if there is something wrong going on on the send-process - but this strongly depends on the Zend_Mail_Transport_* being used.
You have two options here:
Zend_Mail_Transport_Sendmail (the default transport) relies on mail(). If mail() returns false, Zend_Mail_Transport_Sendmail throws a Zend_Mail_Transport_Exception (Unable to send mail). The return value itself is not very reliable. This is what the manual says about the return value:
Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.
It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.
Zend_Mail_Transport_Smtp sends the email using the SMTP protocol that's encapsulated in Zend_Mail_Protocol_Smtp. In this case you'll get a Zend_Mail_Protocol_Exception whenever something either violates the SMTP protocol (sending mail without giving a sender's address e.g.) or the STMP server reports an error or the connection times out.
So if no exception is thrown when talking to the STMP server, you can be sure that the remote server at least accepted your email.

I guess it's not. If "sending" failed you get an exception. But that's only a check, that the send() function worked properly. It doesn't mean the mail got send.
I guess the only way to ensude the mail was delivered is to insert a confirmation code link inte the mail and make user click it.

Related

PHP mailer sentfrom error

I am trying to send mail using PHP Mailer but there is an issue with setfrom() method
In $mail->setFrom('abcd#abcd.com', 'Jay');
When I use domain name .com mail gets sent and received properly
But when I use . co domain name example $mail->setFrom('abcd#abcd.co', 'Jay');
Its doesn't show any error and says mail sent successfully but mail isn't received on my account.Tried changing google privacy settings.
Note:Not using SMPT kept it to false using default settings
Update:Ok so the thing is It is working but the emails are being receive after hours and hours if I use .co but when I use .com email are received immediately.
Probably server problem.
This sounds like a known problem that's been answered before: gmail does not allow setting arbitrary from addresses. You can preconfigure aliases in settings, but you can't simply start using them at the sending client. You can see the submission conversation by setting $mail->SMTPDebug = 2;, but if it's sending successfully, PHPMailer has no involvement beyond that point.
Ok so the problem was the server (bluehost)
What was happening is whenever the setFrom() had an email address whose sub domain was not '.com' eg: " abcd#abcd.co,abcd#abcd.in" the mail was being received after several hours.
It would work normally if setfrom() was set to a normal address eg: a#gmail.com .
Probably a server issue.

PHP mail function always returning false

I want to send mails using php from my website. I have done the following steps but still the mail is not working. It is always returning a false.
I have created a mail id and then has changed SMTP, smtp_port and sendmail_from values in php.ini file. I have tested setting the SMTP and smtp to both default values(localhost and 25) and the values given in mail client config of cpanel (abc-22.example.net and 465). I have also checked phpinfo to check whether those values were set successfully. I have checked the error log also, no errors logged there. The php version installed on server is 5.4.24.
Always use SMTP Authentication for mail sending by using phpmailer. mail() is not allowed on some hosting side. Because of spaming.
Click here!
Are you changing the From: attribute in the header?
Some ISPs will block your mail if you do that.
See this SO post

How to Bounce E-mail Back with PHP

I am piping all e-mails through a PHP script that checks the To address against a database of valid addresses. If it exists, the rest of the script handles it. However, if it does not exist, how can I bounce the e-mail, the same way the server would if I didn't have the script? Thanks!
The mails are recieved through smtp protocol, in your case also i assume that there is some service running on port 25 which would listen to request for mail from external domains.
There are different ways to bounce the message
->bounce at smtp level itself, as in when u get the recipient list, check f
or the id existance and if does-not exists give a 4xx response. The bouncemai
l would then be generated by the senders domain automatically.
-> if you have accepted the mail from the domain say gmail.com then u will
have to make a new connection to gmail with your bouncemessage, this is same
as sending a new mail from your server to gmail.com.
Turns out this was very easy to solve: Simply echo something in the PHP script (for example, "This account does not exist.") and the mailer daemon generates a bounce-back e-mail with this output included.

PEAR Mail, SMTP Sessions for Newsletters?

when sending newsletters through a SMTP server using PEAR's Mail package, is there any way to specify some kind of "connection reuse" so that the PHP script won't have to create a new socket to the SMTP server for each individual mail?
That is of course without putting the adress of each recipient in only one e-mail, so that the indvidual recipient's won't see each others adresses.
Or doesn't SMTP allow for this?
Well I think the best solution is to put each destinee in black carbon. This guarantees that who receives the mail does not see other mail addresses and is a better solution than sending a mail for each destinee
This is feasible with php pear Mail package.
PEAR Mail seems to be a hopeless case, but Zend's framework has addressed the issue and keeps the SMTP socket open for as long as the script runs (and the object exists): http://framework.zend.com/manual/en/zend.mail.multiple-emails.html
What you should do is set the 'persist' param. And then only use the factory method once - then you ensure that it is the same socket that is used.
something like this:
static $mail;
if (!is_object($mail)) {
$mail = Mail::factory($options['mail_method'], $params);
}
$res = $mail->send($to, $mime_headers, $body);
If you call the mail::factory every time then a new socket will be created. In the above way you only create one socket.

php redirect email to file

I am using php5.
Are there some settings or a simple php.ini directive that would redirect all the emails to a folder?
I want on the development machine to have all the emails generated by the system not sent to the actual receiver but put in a folder.
Thanks.
I used to have some code like this (kinda pseudocode):
define ('DEBUG', true);
function send_email($to, $subject, $body) {
if (DEBUG) {
file_put_contents('some_folder/' . $to . date('dmY-His') . '.html', $body);
}else{
// Actual code to send email
}
}
But i agree with others, it's easier/better to setup an development email account to receive those emails.
I don't think you will be able to do something like this. Mails are sent by a mail server so it must be your mail server that writes them to a file instead of sending them.
Why not simply send it to a special development email?
Sample:
define('DEBUG', true);
if(DEBUG)
{
// Override recipient
$recipient = 'development#domain.tld';
}
// Send mail...
No settings that I'm aware of in PHP itself. However, if you're using Postfix on your development server, here's a recipe I cooked up to redirect all outbound email to a single (local) address:
/etc/postfix/main.cf: (add this to the existing file, don't replace everything)
virtual_alias_maps = regexp:/etc/postfix/virtual
/etc/postfix/virtual:
/.*/ duskwuff#localhost
You can configure your mail server to accept SMTP messages as normal, but make it unable to forward them onto another mail server. If your mail server supports it, make it redirect all messages to a postmaster account, or any other address of your choice.
This means that PHP will behave as normal, everything will appear to work straight away with the message, but it just won't go to the 'intended' recipient.
It also means that you can inspect headers (pretty much as they would normally appear), to support debugging.
There are many ways to do this. Basically, you need to define the sendmail command in your php.ini to point to a program or script which will save the mail locally.
One solution is this:
Catch emails with php script
Another is this:
Mail catcher

Categories