Sending mail via php and from headers? - php

When I send mail from my php script I send it like so
$to = $_POST['email'];
$subject = $_POST['username'] . ' - Validate your account at example.com!';
$message = 'http://www.example.com/activate?username=' . $_POST['username'] . '&code=' . $random;
$headers = 'From: noreply#example.com';
When the mail comes through to an email the from header shows like this,
noreply#example.com via web87.extendcp.co.uk
Is there a way I can stop the via bit from showing?

The only solution would be to use your own SMTP server, or at least another one... If your provider allows that.
It is your provider's web server which modifies the From: line here.

Yes, you need to implement DKIM signing of your e-mails so Gmail can verify that your server has the right to send e-mail for that domain.

Try using the IMAP-method from PHP
First connect with the GMail imap-server:
http://www.php.net/manual/en/function.imap-open.php
(Make sure you're using SSL/TLS)
Then send the message with imap_mail():
http://www.php.net/manual/en/function.imap-mail.php
$host="{imap.gmail.com:993/imap/ssl/novalidate-cert}";
$user=""; // Your GMail-account
$pass=""; // Your GMail-password
if ($mbox=imap_open($host, $user, $pass)) {
// Connection Success
$to = "";
$subject = "";
$headers = "";
imap_mail ($to, $subject, $message, $headers)
} else {
// Failed to connect
}
Hope this helps ;)

While fge's answer is most likely the correct one here, there's always a chance that your server's mail service won't add it if there's a "friendly" name in the header:
From: No Reply <noreply#example.com>
...instead of just:
From: noreply#example.com

Related

auto reply not working only for gmail accounts using php mail()

I have a contact form on my website which sends an email to my account and an auto-response to the users who fills the form. I could able to send an auto-reply to non-Gmail accounts but not to Gmail accounts, it's not even sent to spam. I want to know is anything missing in the code, or any settings have to be changed, let me know
code is working fine with non-Gmail accounts
<?php
$email_to = 'mailme#example.com'; //your email
$business = 'company name.,'; //business name
//$topic = $_POST['topic'];
$name = $_POST['name'];
$email_from = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$autoResponse = true; //set to false if you don't want to send an auto reply
$autoResponseSubject = "Demo Contact Form";
$autoResponseMessage = "Hi, thank you for contacting us, we will get back to you soon.";
$autoResponseHeaders = "From: $business <$email_to>\r\n";
$autoResponseHeaders .= "Reply-To: $business <$email_to>\r\n";
$headers = "From: $name <$email_from>\r\n";
$headers .= "Reply-To: $name <$email_from>\r\n";
if(#mail($email_to,$subject, $message, $headers)){
if($autoResponse === true){
mail($email_from, $autoResponseSubject, $autoResponseMessage, $autoResponseHeaders);
}
echo '1';
} else {
echo '0';
}
?>
I am not getting any errors.
Google, Microsoft, and the like, only accept email from mail servers that fulfill a number of requirements. These requirements are changing over time. This has mainly to do with preventing spam.
Things start with SPF, which is rather simple, but the normal site providing documentation has been down since feb 2019. Have a look at Wikipedia instead.
The next thing is DKIM. Without it mail certainly won't been accepted by GMail.
Then there is also DMARC.
After all of this there is still no guarantee that your mail will be accepted. Your IP could be blacklisted.
As you can probably guess by now, running your own mail server is a lot of work. I've stopped doing it years ago. I now use a third party service for this.

How to set the spf or 'sent via' in email header in php

When you receive a email (in Gmail at least), you see the following format at the top: 'John Doe johndoe#gmail.com via servername.com' I am wondering if there is a way to change the 'servername.com' after the "via" while in doing so keeping my defined headers in my PHP code below, any help would be great:
<?php
$to = 'johndoe#gmail.com';
$message = 'blah';
$subject = 'blahblah';
$headers = 'From: test#gmail.com';
mail($to,$subject,$message,$headers);
?>
That's set by your MTA. For Postfix I can tell you it's determined by the myorigin parameter in main.cf.

Sending email from html form using php [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
Ive been trying this out the whole day but I cant figure out how to send an email from my html contact form containing the information from the form to my email address. Im new to php.
Ive tried running this by uploading it to free web hosting. I get the message "success!" when I press the submit button on my html form but no email is actually sent.
Any help is appreciated.
PHP script:
<?php
//Subject
$subject ="Contact Form Submission";
// Name
$name =$_POST['InputName'];
// Message
$message =$_POST['InputMessage'];
//Mail of Sender
$email =$_POST['InputEmail'];
//From
$header = "From:$name<$email>";
$send_contact=mail("myemail#gmail.com",$subject,$message,$header);
//Check if mail was sent
if($send_contact){
echo "Success!";
}
else {
echo "Error!";
}
?>
EDIT: Figured it out after one whole day of trial and error. The problem was with the free web host I was using. Changed hosts and the code started working fine. Hope this helps someone in the future. Thanks all for the help.
I have a pretty good idea why your code is not working. It happened to me a long time ago. The reason why your code is not working is because :
When you pass "from" in headers, php expects an existing email account of your
server. For example : $headers = 'From: emailacc#yourserver.com';
So first thing you gotta do is create an email account on your server. And then put the From in header to the email address that you've just created.
The From field in the $headers is not the From as you think.
<?php
$email = $_POST["InputEmail"];
$subject = $_POST["InputSubject"];
$message = "From: ".$email.", ".$_POST["InputMessage"]; // you put the email address from the input form here
$headers = 'From: emailacc#yourserver.com'; // here is the email address specified from which u want to send the email.(i.e. your server email address)
mail($to, $subject, $message, $headers)
?>
I'm sure this will do the job :)
Have a shot at this.
I changed you're $header variable around a little bit, so that rather than setting the email as "$email", It'll actually pass through the posted email entered in the form. This apply's to the name too.
I also made it so that you pass the mail function through the parameters of the if statement, rather than setting a new variable.
$headers = "From: " . $name . "<" . $email . ">"; // notice new concatenation
if(mail("myemail#gmail.com", "Contact Form Submission", $message, $headers)){
// success message
} else {
// error message
}
Really hope this helps! :)
Try adding spaces after the "=" that might be the problem,
If that doesn't work you could try to use this
<?php
$emailvariable = $_POST['InputEmail']
$to = 'example#gmail.com';
$subject = "Form"
$message = $_POST['InputMessage'];
$headers = "From: $emailvariable";
mail($to, $subject, $message, $headers);
?>
Hope this helps

Emails aren't sent in Wordpress with headers

I'm running a new WordPress website on LEMP on Debian 7, sendmail installed.
I have a custom theme installed which has contact form.
Contact form says email sent but email actually not delivered. I have tried to remove $headers from email, email delivered. Simple php mail function working properly. Same custom theme sending and delivering email through form at Shared host.
Here is part of custom form:
What is wrong with this? can anyone point me out what is wrong or how I can start sending emails?
if(!isset($hasError) && ($correct == true)) {
$admin = get_bloginfo('admin_email');
$portfolio = get_bloginfo('name');
$portfolio_url = home_url();
$emailTo = $admin;
$pro = get_the_title();
$subject = "You have an message for $pro";
$body = "Hello,\r\n\r\nYou've received an message from ".$name.", for ".$pro." fun name.\r\n\r\nHere are message details:\r\n--------------------------------\r\nBuyer Name: ".$name."\r\nEmail: ".$email."\r\nMessage: ".$message." ".$CurrencyCode."\r\n\r\nMessage: ".$message."\r\n\r\n---------\r\n".$portfolio."\r\n".$portfolio_url;
$headers = "From: ".$portfolio." <".$emailTo.">" . "\r\n" . "Reply-To: " . $email;
wp_mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
Thank you!
wp_mail has an open bug related to the Reply-To header. Try removing that header only and check if the email is sent correctly. If that's the case, you will have to create a plugin to wrap it or just avoid setting the Reply-To header.

PHP mail() contact form not delivering to Gmail - advice needed

I'm using a PHP contact form and it is sending mail to non gmail addresses, however when I set it to send to a gmail address, it doesn't get delivered (it doesn't even appear in junk mail).
I've heard of issues like this before - I'm not a web developer/expert so can anybody suggest code/configuration changes to my PHP contact form below which would essentially mean messages get delivered to gmail addresses?
I'm on a linux/WHM dedicated server.
<?php
error_reporting(E_NOTICE);
function valid_email($str)
{
return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*#([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
}
if(!empty($_POST['name']) && !empty($_POST['email']) && valid_email($_POST['email']) === true && !empty($_POST['comment']))
{
$to = "contactform#gmail.com";
$headers = 'From: '.$_POST['email'].''. "\r\n" .
'Reply-To: '.$_POST['email'].'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$subject = "Contact Form";
$message = htmlspecialchars($_POST['comment']);
if(mail($to, $subject, $message, $headers))
{
echo 1; //SUCCESS
}
else {
echo 2; //FAILURE - server failure
}
}
else {
echo 3; //FAILURE - not valid email
}
?>
Did you check to see if your PHP installation/server supports the Mail() function?
Also did you check to see if Gmail is treating your emails as spam? (they wont show up in the spam folder, they are just blocked)... Try sending it to a different address.
Set your script to send it from an email address that has an actual mailbox somewhere so you can check for bounces. If your script echoing 1, and can send to other addresses it suggests (to me at least), that it's gmail not liking you, rather than anything wrong with the script per se.
It also looks like you need to look into protecting this script from email injection. Just a suggestion though.

Categories