1

I've set up Postfix on my Ubuntu+Nginx machine to send mail and I'm running in to one issue.

The issue is that the from name is www-data with the email www-data@ip-123-45-67-890.ap-southeast-2.compute.internal.

As this is a "send to a friend" form, the from name should be the $_POST['sendername'] so the email is sent and shows up as coming from the sending user and their email, and cannot be hard-coded in /etc/postfix/main.cf or anywhere else as it's dynamic in that sense as per the above.

Even testing the "from" header to a hard-coded value as you can see where I've commented out, it still doesn't appear as that from name and email.

Any ideas? Thanks.

    $to = $_POST['recipientemail'];
    $subject = $_POST['subject'];
    //$headers = "From: Me <me@testtesttest.com>\r\n";
    $headers = "From: " . $_POST['sendername'] . " <" . $_POST['senderemail'] . ">\r\n";
    $headers.= "Reply-To: " . $_POST['sendername'] . " <" . $_POST['senderemail'] . ">\r\n";
    $headers = "To: " . $_POST['recipientname'] . " <" . $_POST['recipientemail'] . ">\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
    $headers .= "X-Mailer: PHP/" . phpversion();
    $message = '<html><body>';
    $message .= $_POST['message'];;
    $message .= "</body></html>";

    mail($to, $subject, $message, $headers);

/etc/postfix/main.cf

smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = static:myuser:mypass
smtp_sasl_security_options = noanonymous
smtp_tls_security_level = encrypt
header_size_limit = 4096000
relayhost = [smtp.sendgrid.net]:587

postconf -n

alias_database = hash:/etc/aliases
alias_maps = hash:/etc/aliases
append_dot_mydomain = no
biff = no
config_directory = /etc/postfix
header_size_limit = 4096000
inet_interfaces = all
mailbox_size_limit = 0
mydestination = ip-123-45-67-890.ap-southeast-2.compute.internal, localhost.ap-southeast-2.compute.internal, , localhost
myhostname = ip-123-45-67-890.ap-southeast-2.compute.internal
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
myorigin = /etc/mailname
readme_directory = no
recipient_delimiter = +
relayhost = [smtp.sendgrid.net]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = static:myuser:mypass
smtp_sasl_security_options = noanonymous
smtp_tls_security_level = encrypt
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
smtpd_tls_cert_file = /etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file = /etc/ssl/private/ssl-cert-snakeoil.key
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtpd_use_tls = yes
  • Can you paste the output of postconf -n and the log lines generated by postfix to help you better. – clement Oct 24 '14 at 13:26
  • This is a old and dangerous approach and you should NOT use it! You or the person you relay thrught, sendgrid, will in most cases fail any possible test of SFP, DKIM and DMARC. Conclusion your mails will end up in users spam folder if it is accepted at all on the receiving mail server. And at worst, it might end up on a blacklist. – Anders F. U. Kiær Oct 24 '14 at 21:14
  • not SFP but SPF : ) – Anders F. U. Kiær Oct 26 '14 at 15:50
  • @clement Thanks Clement I've updated the question with the output – DT.DTDG Oct 26 '14 at 22:36
  • @AndersF.U.Kiær I completely understand AndersF.U.Kiær and I know it's an old and dangerous approach but this is purely for an internal system which the address will be whitelisted and not go to spam. Any ideas on the approach to achieve what is required? Thank you. – DT.DTDG Oct 26 '14 at 22:37
1

Must have been a long Friday when I posted the question. Now with a clear "Monday" head I was able to figure it out. The headers were not set correctly in terms of where the .= was for each one.

It should have been:

$headers = "From: " . $_POST['sendername'] . " <" . $_POST['senderemail'] . ">\r\n";
$headers .= "Reply-To: " . $_POST['sendername'] . " <" . $_POST['senderemail'] . ">\r\n";
$headers .= "To: " . $_POST['recipientname'] . " <" . $_POST['recipientemail'] . ">\r\n";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.