PHPMailer: Set custom header to identify it inside the bounce email - php

I'm sending emails with PHPMailer. When an email is bounced it goes to an account like this: account_bounces#domain.com
Inside my email client where I manage this account (account_bounces#domain.com), I have the option to adds filters in order to redirect an email to any other email account, based on the comparation of fields like "Subject", "From", "To" and so on. That's good.
The problem is that the bounced email loses all of my headers/Subject...that I set with PHPMailer because it's ALWAYS composed by the server as it follows:
Subject: Undelivered Mail Returned to Sender
From: MAILER-DAEMON#llsd409-a04.servidoresdns.net
To: account_bounces#domain.com
Date: Today hh:mm
So I have no guide marks to use for adding a filter.
So, is there any way to set a mark(like a custom header, etc...) in PHPMailer that REMAINS in the bounced email?. For example, something like having this:
Subject: Undelivered Mail Returned to Sender (bounce_redirect)
So the word "bounce_redirect" in the Subject(or wherever) would indicate my email client that this email has to be redirected.
Thanks in advance.

Unfortunately there is no way you can force this issue in headers; The only way around it is to use VERP addressing, which is the only way that you can guarantee that it preserves info about the message and what address it was originally sent to. It's common for MS Exchange to send bounce messages that do not mention the original address the message was sent to at all, so VERP is the only solution.
For your example, a typical VERP address would be:
account_bounces-user=domain.com#domain.com
You mail server would be set to spot the account_bounces prefix and remove it, and convert the = to a # in the local part to extract the original address.
In PHPMailer you would set this as your Sender property, like:
$mail->Sender = 'account_bounces-user=domain.com#domain.com';
This will be used as the SMTP envelope sender, and converted to a Return-Path
header by the receiving server, and thus will be used as the RCPT TO address (the bounce destination) when the message gets bounced.
You can take this further and embed additional info in the Sender address that can be used to identify the mailing list, a specific mailshot etc.

Related

What's the best way to send a newsletter with PHPMailer?

I'm trying to send a newsletter with PHPMailer meanwhile protecting my customers privacy.
At first I set the receiver configuration with mail->addAddress('customerEmail']); but I found out that sending it this way, every receiver could see other customer's subscribers E-mail addresses.
I changed the addAddress to mail->addBCC('customerEmail']); so that it doesn't show every E-Mail address (in fact now it even doesn't show the recipient E-mail address of the customer it's being send to), but this way if anyone wanted to reply to the E-mail, their response would also be sent to the rest of the subscribers...
What is for you the best option to face this problem?
As I was advised, the best way to manage a newsletter is to send the email individually to every customer although it means a slight increase on the sending time. This way you can offer automated unsubscription links and other features that otherwise you couldn't.
In order to achieve that, I just made a simple loop getting the addresses from my db:
$sql = "SELECT `email` FROM `emails`";
$statement = $db->prepare($sql);
$statement->execute();
while ($fila = $statement->fetch()) {
if(!empty($fila['email'])){
$mail->addAddress($fila['email']);
$success = $mail->Send();
$mail->clearAllRecipients(); //Don't forget this!
}
}
The method clearAllRecipients(); is very important as it's the one who will clear the last lap recipient so that the 'to' section of the email doesn't show all your newsletter recipients.
Email servers do not use the message headers to deliver messages. When you send an email your email client maintains a conversation* like this:
EHLO example.com
MAIL FROM: mail#example.com
RCPT TO: info#example.net, customers#example.org, john#example.com
DATA
From: Me <webmaster#example.com>
To: Customers <whatever#example.com>
Subject: Important info
Dear Blah,
Blah blah.
Regards,
.
(*) It is a conversation because it includes server response to each command but I've omitted that part.
What really counts is the information provided in the specific commands before the DATA block. Certainly, most mail tools will create headers to match the addresses in the processing instructions but such headers are for pure informational purposes and they don't need to be identical. In fact that's how Bcc actually works: you instruct your mailer to deliver email to such address but omit it from headers.
So:
You can do it fast (Bcc and a single message for all)
You can do it nice (To and one message per recipient)
If you ask for my opinion, it's always nice to know the exact e-mail address to which a newsletter is being sent—many of us have more than one address. And it's actually mandatory to send customised messages if you want to offer automated unsubscription links.

How to make PHPMailer ignoring the 'from' email address and display only the name?

I'm using PHPMailer with the same SMTP configuration as my outlook.
Let's say that my email is "b#example.com" and name is "Bob, b".
When I send a mail from my outlook to "Alice" at "a#example.com". In her outlook client she will only see "Bob, b" and she will not see my email address in the header.
When sending the same mail from "PHPMailer" Alice will see in her outlook "Bob, b {b#example.com}"
When I tried to set it without the email:
$mail->setFrom('Bob, b');
The smtp added {root#mysmtpblabla.example.com}
Can PHPMailer handler such a case? sending with name only?
It really helps to actually read the docs for the function you're calling.
Applying that info to your example, you should do this:
$mail->setFrom("b#example.com", "Bob, b");
In most email clients it will display the name and not the email - in Outlook or Apple Mail it will usually show you the email address next to the name or on rollover - just because it doesn't display the address doesn't mean that there isn't one!
Some email services will not let you send from arbitrary addresses (such as gmail), so you may still find the from address itself changes.
You should not try to send without a from address for all the reasons Michael_B gave, plus that it's also very unlikely to work at all; that's why both PHP and PHPMailer add one that's generated automatically from your hostname.

PHP email form, how to bypass host limitation? (Can ONLY send/receive IF hosting sender/receiver)

My webhost ONLY allows sending/recieving emails IF either the sender or reciever is hosted with them. (freehostia.com)
This is a huge disadvantage to me (and I'm assuming everyone else), because of the way my website works.
(My website: I have a classifieds website where CustomerA posts an ad with her email and CustomerB replies via the email form with his email. Neither email is hosted with my host.)
I asked if I could use an external SMTP server (such as Gmail) to bypass the limitations, and they said "Even if you set an external MX record for your domain you will not be able to send e-mails via your mail forum, if you do not use a mailbox from your hosting account with us as a sender or recipient."
Theoretical Workaround:
Auto-enter and hide my hosted email into the "email" section of the form
Have a new section for customer to input their email
When a message is sent, embed customers message and email into a default message. It will look like this:
To: customerA#example.com
From: DONOTREPLY#example.com
Subject: You have recieved a message!
Body: Blahblahblah (customers message) blahblah. To reply, email: customerB#example.com
Sorry about all the confusion. Would this work? Should I give up? I really like my host, but should I switch? Or is there a better workaround?
While you don't need to send through a different server, you can just send to whom you need and set the reply to any address you want.
The mail function allows you to set your own headers as a final parameter.
$headers = 'Reply-To: someone#some_other_domain.com\n\r';
mail($to, $subject, $body, $headers);
You can set the reply-to address.
That way even though the email is sent from your address, when the recipient hits reply it creates an email to the address given in the reply-to.
I'm not sure what you are using to send mail but there are some examples in the PHP documentation mail function - http://php.net/manual/en/function.mail.php

How to hide sender email address using phpmailer?

I am using phpmailer to send email. I need to know how to hide or mask sender email address
You can specify any sender email address anyway, since SMTP by itself does not place any requirements on sender email addresses.
If the actual SMTP server you use places restrictions on email addresses (e.g. corporate servers which do not allow sender emails outside of the company domain) there's no way around that, unless of course you can influence the mail server configuration.
Update:
You say in a comment that you want to use gmail to send email where the sender's address is not a gmail address. There is no way to do that.
This is a rare situation you have here... if you do not have a mail server you can still tell PHPMailer to send from a different address just set the From attribute of the PHPMailer object to the address you want. But Wait! if your server doesn't exists, the client can't verify the account and then your mail will more likely be deleted (moved to spam in the more benevolent scenario). If you are trying to mimic third party mail, I'll help you no futher.
Note: Your mail server may be valid but clients are still unable to verify it, and thus you are getting mails delivered to spam or deleted. Check "Must Read" to below to have some inside on how to solve this.
On the other hand, if you already have a mail server, then tell PHPMailer you want to use it, set the Host and Port attributes to your domain name and port respectively. The same if you want to use an account form a different server, remember to set the attributes Username and Password correctly, you may also need to set SMTPAuth = true; and SMTPSecure = 'ssl'; depending on the server. [Note: Username and From may differ]
Now, if you want to use an account from Gmail, you could easily set an alias in Gmail to send as another account [Go to Settings-> Accounts And Import -> Send mail as -> (click) Send Mail From Another Address], that can be the case if you have a mail server but you cannot afford to have it online, you will need to start your server so you can receive the confirmation code Gmail generates to verify your account. Check recommended read for PHP side configuration details.
Lastly if for some rare circunstancies you can't tell PHPMailer to use your mail server, but you do in fact have one, and that one is able to recieve the mail... you can use AddReplyTo('me#example.com', 'My Name'); Most clients will understand that any reply to the message must be (unless explicitly defined by the user) directed to "me#example.com" in this case.
Disclaimer: I take no responsibility of any harm result of the use of the method I mention here, such as (but not limited to) your mail account getting banned.
Must read:
Coding Horror on sending mail via code
Recommended read: PHPMailer Tutorial (old version)
No need (neither a good way) to hide or mask whatsoever.
I assume you already know how to use the class you are talking about.
You probably have some variable for sending email, like
var $From = "someguy#whatever.com";
you can type whatever you want into that email address. Gmail dont care what email things is sent from.
And no, this dosent sound very legit.
One more thing: Gmail requires a gmail account to relay mails. Its no problem, it wont be visible.
You want to "show the company email address as sender" but you "didn't (sic) have any email server"?
Can anyone actually send you email at your company email address? If so, use that server which is hosting your email to send out from.
If you don't really have a company email address, then I suggest you get a gmail address like companyname#gmail.com and just send from that. Otherwise the email will appear as spam to a great many of your recipients.
Now, if the people you are about to send an email to actually signed up to be on your mailing list then you can use a third party application like Constant Contact to do your broadcasts from.
If they haven't, then I suggest you not send an email at all.
in mail headers you can have both a Sender: and a From: header which in most mail clients is displayed as either just the From or in some cases Sender on behalf of From, using this way is a nice and clean way to be able to send From a different mail address then the actual Sender mail server
This is highly illegal.
var $From = "someguy#whatever.com";
Is the only option your have for trying to hide email address. But no matter what your email will be inscribed with IP. Someone who knows what they are doing will still be able to trace the email back to the source.

Sending BCC emails using a SMTP server?

I've had this noted down on some of my code for a while:
/**
* Add a BCC.
*
* Note that according to the conventions of the SMTP protocol all
* addresses, including BCC addresses, are included in every email as it
* is sent over the Internet. The BCC addresses are stripped off blind
* copy email only at the destination email server.
*
* #param string $email
* #param string $name
* #return object Email
*/
I don't remember where I got it from (possible source) but that shouldn't be relevant to this question. Basically, whenever I try to send an email with BCCs via SMTP the BCC addresses are not hidden - I've read the whole RFC for the SMTP protocol (a couple years ago) and I don't think I'm missing anything.
The strange thing is, if I send an email with BCCs using the built-in mail() function everything works just right and I've no idea why - I would like to roll my own email sender but I fail to understand this.
Can someone please shed some light into this dark subject?
The BCC addresses are not stripped off at the destination email server. That's not how it works.
How SMTP actually works
The sender will send a list of RCPT TO commands to the SMTP server, one for each receiver email addresses, and this command does not distinguish whether the receiver is a normal To, CC or BCC type receiver.
Soon enough after calling the command that tells the SMTP server who's the sender, who's the server, and everything else, only then the sender will call the DATA command, in which will contain the content of the email - which consist of the email headers and body - the one that are received by email clients. Among these email headers are the usual from address, to address, CC address.
The BCC address is not shown to the receiver, simply because it's not printed out under the DATA command, not because the destination SMTP server stripped them away. The destination SMTP server will just refer to the RCPT TO for the list of email addresses that should receive the email content. It does not really care whether the receiver is in the To, CC or BCC list.
Update (to clarify): BCC email addresses must be listed in the RCPT TO command list, but the BCC header should not be printed under the DATA command.
Quoting a part of the RFC that I think is relevant to your case:
Please note that the mail data includes the memo header items such as Date, Subject, To, Cc, From [2].
Rolling out your own email sender
A couple of years ago, I frankly think, is quite a long time back to assume that you still memorize end-to-end of RFC 821. :)
Very late, but the accepted answer is essentially wrong.
First off, SMTP has nothing to do with BCC. SMTP, as a protocol, is concerned only with a return path (the MAIL request), a list of recipients (the RCPT request), and the data to be transferred (the DATA request). If you want to send an email to somebody via SMTP, then you have to supply their address in a RCPT request, period.
The contents of an email - the DATA, effectively - are specified completely separately, in RFC2822. There's a lot of latitude in how BCC should be handled. The spec gives 3 ways of handling BCC, and in only one of them is the BCC stripped out while preparing the email. If I use Thunderbird as an email client, for example, and point it to an SMTP server, and then look at the message on the line, then I find that the Thunderbird BCC has gone (from the SMTP DATA), and the SMTP connection instead contains a standard RCPT request for the bcc'ed address. So, Thunderbird converts BCC to RCPT, but that's not the only way to do it.
Another place to handle BCC is at the MTA - in other words, whatever SMTP server your mail client is pointed to. Sendmail, for example, searches all of the To, Cc, and Bcc lines in the SMTP DATA, and then constructs an address list from those lines, and then removes the Bcc line. You can persuade Sendmail to keep the Bcc if you want to. If sendmail isn't the destination MTA, then it will connect to another MTA over SMTP, and send the recipient addresses via RCPT. In other words, if sendmail is the destination MTA, and it gets a Bcc, it will strip it out, contrary to Amry's statement.
There's also some confusion in the comments. You can specify RCPT addresses to any domain, not just a list of addresses in the same domain. The MTA has to look up the MX records for the destination domains to work out where to send everything. The google.com and yahoo.com statements are wrong.

Categories