How to send -f parameter equivalent from swiftmail? - php

I am trying to migrate from PHP mail to SendGrid using swift mail. I am not able to understand -f email additional parameter.
mail($Email,$sub,$cont,$headers,'-f noreply#mydomain.com')
I am not sure what type of header is this. Should i send this as path_header?
I could not undersrtand the explanation of this here.

This is used to set the "envelope from" on your email; it's not actually a header. This post describes what that means.
When sending via SendGrid you won't need to specify this flag or explicitly set your "envelope from." Just set your from address and fromName and you'll be good to go.

Related

PHPmailer custom header set, but not shown on delivery

I am using PHPMailer6.2.0 and I am having issues setting the return path.
I have added the custom header via PHPmailer function addCustomHeader()
$mail->addCustomHeader("Return-Path", $fromemail);
and for debugging I have printed out the header content in \PHPMailer\PHPMailer.php function mailSend($header, $body) on line 1794;
var_export($header);
die();
this prints out the header content before it will be sent and it verifies that the custom header return-path is set correctly, however in action, when i receive an email to my outlook, the header return path callbacks to the domains default email user#domain.com. Perhaps this is not the last place before the email is sent and it gets lost later on?
I am using DirectAdmin as my server manager
Stop right there! Senders should not set a return-path header. That header is added by the receiver, and what goes into it is dependent on the SMTP envelope sender, the address that's used in the SMTP MAIL FROM command that delivered the message. Setting this header as a sender is a straightforward contravention of the RFCs. So what should you do instead? Set the envelope sender, and in PHPMailer you do that like this:
$mail->Sender = $fromemail;
Even when you do this, whether the server your'e sending through will accept it is a different matter. For example gmail will not allow you to use anything other than your account username address or predefined aliases, not arbitrary addresses.
Have you seen the comment above in mailSend function?
The sender gets turned into a return-path header by the receiver!
<?php
$params = null;
//This sets the SMTP envelope sender which gets turned into a return-path header by the receiver
//A space after `-f` is optional, but there is a long history of its presence
//causing problems, so we don't use one
//Exim docs: http://www.exim.org/exim-html-current/doc/html/spec_html/ch-the_exim_command_line.html
//Sendmail docs: http://www.sendmail.org/~ca/email/man/sendmail.html
//Qmail docs: http://www.qmail.org/man/man8/qmail-inject.html
//Example problem: https://www.drupal.org/node/1057954
// CVE-2016-10033, CVE-2016-10045: Don't pass -f if characters will be escaped.
if (!empty($this->Sender) && static::validateAddress($this->Sender) && self::isShellSafe($this->Sender)) {
$params = sprintf('-f%s', $this->Sender);
}
I do not think you should set the return-path header by yourself. I believe PHPMailer uses the sender to handle this automatically. But correct me if i am wrong.

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.

From header php mail() send sms to phone number

I really want to change the from parameter to absolutely anything I want, when sending sms with php's mail() function.
e.g the user sees text message from "the developer" on their phone screen.
I don't want the #domain From response, nor the email address.
$headers = "From: " . "the developer" . "\r\n";
mail('##domain', 'check', $client, $headers);`
As far as I have seen, headers are stripped / replaced by the SMS gateway with headers they prepare. So altering the FROM parameter on your end will have no effect. The only option would be to look at renting a short code number or using a price-based SMS gateway provider. I'm assuming this measure is in effect to prevent malicious intent.
You need to add a -f flag to mail in order to override the sender, but I warn you that doing so will cause an X-Authentication-Warning: hostname: www-data set sender to info#example.com using -f header on the receiving end unless this host is allowed in your /etc/mail/trusted-users which you may not have control of if you're on shared hosting.

Processing an Email Bounce back in CakePHP and Postfix

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/

PHP mail, header and call to mail() function, what has override?

I've been struggling with low level mail in PHP and I know I should be using a library for this, but that's not an option right now.
When doing mail in PHP, you can manually set additional headers, like From, Cc and Bcc, but you can also set Subject, To and a Body. When you call the function you pass the headers along to the mail() function, but that function also "asks for" a Subject, body and To.
My question then is: how does PHP handle the double intention in this? If you manually set the header to have Subject : foo, but then in the call to mail pass 'foo' along as the subject...?
I can't read C, so opening up PHP source probably won't help me here.
Thanks!
Well, no need to read C, just test it: it's a one liner :)
If you specify a subject in both places PHP does nothing special: you get an e-mail message with two subject headers. Which one gets displayed in your e-mail client is something I don't know; perhaps it's defined in e-mail protocols, perhaps it's a per-client choice.
About the "To" header, PHP sets one from the $to parameter when you don't specify a header manually; if you set one, your header prevails.
It's worth noting that the "From" and "To" headers have no effect in who sends and receives the message: they're purely informative. Mail server software requires senders and recipients to be specified implicitly; headers are not parsed for this purpose.
The question here seems to be if you specify a 'To' or a 'Subject' in the additional headers, what appears in the email produced.
If so, then the simple answer is to test it:
$add_to ='to: "added" <user#example.com>"; // substituting your email address
$param_to ='"param" <user#example.com>";
$add_subj ='subject: Subject in header';
$param_subj='Subject in param';
$add_hdr=$add_to . "\n" . $add_subj;
mail($param_to, $param_subj, "body - test", $add_hdr);
Then have a look at the message you get back.
C.
Right way - don't use Subject, To and a Body in headers. You can remove it from heders with regular expression, if you take headers as is. Other way - you can use PEAR library - http://pear.php.net/manual/en/package.mail.mail-mime.example.php

Categories