I have this code to send email.and I got no error in sending email.
if(mailSender()){
echo 'meiled.';
}else {
die('not sent');
}
function mailSender() {
$to = 'info#fcrop.com';
$subject = 'An Abstract Submitted';
$message = 'An Abstract Submitted Recently In ICNBA.org';
$headers = 'Frominfo#icnba.org';
mail($to, $subject, $message, $headers);
}
it does not send email.
You need to change:
$headers = 'Frominfo#icnba.org'; to $headers = 'From: info#icnba.org';
and
mail($to, $subject, $message, $headers); to return mail(... to return the value otherwise mailSender won't return anything.
If the mail is not being sent this may have many reasons from php.ini settings over local MTA misconfiguration to unmatched SPF records. Check your mail logs to see if there are any errors.
Because the if expression evaluates a boolean, and mailSender() returns none.
Add this to your function:
return mail($to, $subject, $message, $headers);
For more information about see the php documentation: http://php.net/manual/en/control-structures.if.php
#user3173842 look standart rfc2822 for get more information last header line need \r\n\r\n http://www.faqs.org/rfcs/rfc2822.html
From: "Joe Q. Public" <john.q.public#example.com> \r\n
To: Mary Smith <mary#x.test>, jdoe#example.org, Who? <one#y.test> \r\n
Cc: <boss#nil.test>, "Giant; \"Big\" Box" <sysservices#example.net> \r\n
Date: Tue, 1 Jul 2003 10:52:37 +0200 \r\n
Message-ID: <5678.21-Nov-1997#example.com> \r\n
\r\n
Hi everyone. is body
this is answer for show example header
sorry for offtop
Related
This question already has an answer here:
php form sending email, but not in correct way [closed]
(1 answer)
Closed 5 years ago.
My contact form on my web page finally send emails, but it doesn't send it in the proper format. This is the email of what I am getting. I've hidden the email and organization for privacy reasons.
From-- norgun
Subject-- Test Email: ***#gmail.com Message: Sbsbdb
Message--
MIME-Version: 1.0
Content-type: text/plain; charset=iso-8859-1
From: Test <***#gmail.com>
Reply-To: <***#gmail.com>
X-Mailer: PHP/7.0.21
Anyway, as you can see from the above, the name, email, and message content that the person would've written in the contact form are in the subject line instead of the actual email box. Is there a way that I could format my code so that the message and the name that they wrote down are in the message box instead of the subject box, and the email gets sent from the person who put their email in the form, not "norgun" which is what I came up with for the website?
Here is my code so far:
<?php
$to = 'index#indexmarkets.biz';
$name = !empty($_POST['name']) ? filter_var(trim($_POST['name']), FILTER_SANITIZE_STRING) : '';
$from = !empty($_POST['email']) ? filter_var(trim($_POST['email']), FILTER_SANITIZE_EMAIL) : $to;
$message = !empty($_POST['message']) ? filter_var(trim($_POST['message']), FILTER_SANITIZE_STRING) : '';
$body = "Name: {$name}\r\nEmail: {$from}\r\nMessage: {$message}";
$body = wordwrap($body, 70, "\r\n");
$headers = [
'MIME-Version: 1.0',
'Content-type: text/plain; charset=iso-8859-1',
"From: $name <$from>",
"Reply-To: <$from>",
'X-Mailer: PHP/' .phpversion()
];
$success = mail($to, $body, implode("\r\n", $headers));
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') {
die(json_encode(['success' => $success]));
}
echo $success ? 'Sent Successfully.' : 'An error occurred';
ini_set('display_errors', 1); error_reporting(E_ALL);
That's because you're passing your variable $body as the subject parameter to the mail() function. This line:
$success = mail($to, $body, implode("\r\n", $headers));
Should instead be something like this:
$success = mail($to, $subject, $body, implode("\r\n", $headers));
Make sure you set $subject to something.
I have a few questions regarding sending email in PHP. I've been on google for the last few days and I'm still having trouble getting this to fully work.
My first question is how do I change the "From" section of my email? I have "To: support#mydomain.com" in my "from" section:
I'd like to have just the proper name of my domain (eg: "testingstuff.com" -> "Testing Stuff"). How could I achieve this?
Once I actually open the email everything in it is fine and correct, including the From email address being "support#mydomain.com".
Also my mail won't send to gmail addresses. It shows up in my mail queue and my logs say it is sent but it never is received on my gmail. Do I have to take extra steps for Google to accept my email? If so what are those? Do other major mail provides require the same steps, or are they different steps?
This is my code so far:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set("sendmail_from", "support#mydomain.com");
class email {
public static function send($to, $subject, $message) {
$headers = "From: Testing Stuff <support#mydomaincom>\r\n";
$headers .= "Reply-To: support#mydomain.com\r\n";
$headers .= "Content-type: text/html\r\n";
mail($to, $subject, $message, $headers);
}
}
?>
Usage:
require_once("../mail.php");
email::send("support#mydomaincom", "testing email subject", "testing email body");
Am I doing anything wrong in my code?
You need to check if the email is sent properly checking the mail() result, in this way:
$result = mail($to, $subject, $message, $headers);
if(!$result) {
echo "Error";
} else {
echo "Success";
}
this is inside your static function,
Also check your spam folder if the mail function return "success".
How can i use the mail by header in php?
This is what i use now:
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: test#gmail.com\r\nReply-To:test#gmail.com";
//send the email
$to = "test#gmail.com";
$subject = "This is a subject";
$body = "Hi there!";
if (mail($to, $subject, $body,$headers))
{
echo("\nMessages successfully sent!");
} else {
echo("\nMessages delivery failed...");
}
I get this on my gmail when i click on show details:
from test#gmail.com
reply-to testreply#gmail.com
to test#gmail.com
date Sat, May 14, 2011 at 12:06 AM
subject stefanos neofitidis! You commented on your poem:Tree present
mailed-by ip-1-1-1-1.ip.secureserver.net
i do not want to the ip-1-1-1-1.ip.secureserver.net to show up to the users...this is what i am trying to change!
Do you mean the X-Mailer?
$headers = "From: test#gmail.com\r\nReply-To:test#gmail.com\r\nX-Mailer: PHP/" . phpversion();
What you want to do is add a fifth parameter in your mail() function, which needs to use the "-f" sendmail option.
For example:
mail($to, $subject, $body, $headers, "-fsender#domain.com");
Sending a message with that last parameter will change the envelope sender to "sender#domain.com". Most of the time, email providers like gmail won't even show that address if it is set by hand(which is what you want, I'm assuming).
See http://us2.php.net/function.mail for more details.
I have successfully sent mail using PHP's mail() function before, and for my password reset notification e-mail, I copied the syntax I was using elsewhere, but I guess I messed it up, as it's not arriving at its destination. Here is the code I'm using:
$headers = 'To:'.$email."\r\n";
$headers .= 'From: webmaster#aromaclear.co.uk'."\r\n";
$to = $email."\r\n";
$subject = 'AromaClear Password Reset Notification'. "\r\n";
$msg = 'From: AromaClear'."\r\n";
$msg .='Subject: Your New Password'. "\r\n";
$msg .= 'Message: Your new password is '.$newpass."\r\n";
$msg.= 'If you have received this e-mail in error, please ignore it.'. "\r\n";
mail($to, $subject, $msg, $headers);
Any thoughts?
Try looking at your server's mail logs to see why it isn't getting forwarded. Ex., it may be that this server's sendmail wants the -f flag for the From header instead of specifying it in the header text.
mail($to, $subject, $msg, $headers, "-f $from");
Also, you seem to be doing a lot of extra/weird work. This is a lot easier:
$subject = "AromaClear Password Reset Notification";
$headers = "From: webmaster#aromaclear.co.uk";
$msg = "Your new password is $newpass\r\nIf you have received this e-mail in error, please ignore it.\r\n.";
if(mail($email, $subject, $msg, $headers))
{
//handle success
}
else
{
//handle failure
}
Change style to your preference.
have you checked the return value of mail(). If it's not FALSE then it's accepted for delivery and the code is fine, but something is messed up somewhere else.
That looks fine to me, perhaps do
if (mail($to_email,$subject,$message, $headers))
echo 'Success';
else
echo 'Error';
}
That might let you know if it's trying to send at all.
Just don't add "\r\n" everywhere, use it only to separate headers.
In the message you can use only \n, it will work.
And at the end of the subject and receiver there's no need for "\r\n".
Is the following "From" header incorect?
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'From: Mail Master <mail#mailmaster.com>' . "\r\n";
if(sendEmailNow($email, $subject, $body, $headers)){
I get the error from my mail server. It says "Mail from error: Syntax error".
Thanks all for any help.
Update
I have stripped down the SendEmailNow function to the below and I get the same error:
//send an email
function sendEmailNow($email, $subject, $body, $headers){
if (mail($email, $subject, $body, $headers)) {
##check email
##code to say email sent - compare with the number registered
return true;
}
else {
##code to report an error
return false;
}
}
Update 2
Problem solved. I am running this on a windows machine using PHP 5. Like the correct answer chosen and the comments have said. Some mail servers have trouble understanding what I had previously. But what worked for me was this:
$headers .= 'From: mail#mailmaster.com' . "\r\n";
A Google search for the error message suggests that some SMTP servers fail to parse your syntax for the From header. Can you try the following syntax to rule out this possibility?
From: mail#mailmaster.com
Unless the body is empty, you may need an additional CRLF to terminate the headers. Without knowing the API I can't say much more.
Not sure if this'll help, but here's what the output headers look like in my Python app in pure-text:
Content-Type: multipart/alternative;
boundary="10.254.26.130.1.1364.1241389770.060.1"
From: User1 <user1#domain1.com>
To: User2 <user2#domain1.com>,User3 <user3#domain.com>
Subject: Actual subject
MIME-Version: 1.0
--10.254.26.130.1.1364.1241389770.060.1
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
actual text content starting here...
Again, without knowing all the content/headers it's hard to say, but I'd guess either a) You've left out the trailing CRLFs before the content, or b) one of the earlier headers missing its CRLF.
Apologies if that takes you in a wildly wrong direction. :)
From: "User1" <user1#domain1.com>
The From header requires quotes for the name part.
If you set the "$userEmail" variable in your form, you can have it from them and a reply to. The $email_id is your email it is sent to.
$to=$email_id;
$headers = "From: " . strip_tags($userEmail) . "\r\n";
$headers .= "Reply-To: ". strip_tags($userEmail) . "\r\n";