This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I am using this code for my contact form in a html website but mail is not coming in Gmail Inbox.
Can any one help me i am trying to solve this issue but i don't have any guide.
<?php
session_cache_limiter( 'nocache' );
$subject = $_REQUEST['subject']; // Subject of your email
$to = "iamuser#gmail.com"; //Recipient's E-mail
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= "From: " . $_REQUEST['name'].'<'.$_REQUEST['email'] .'>'. "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$message = 'Name: ' . $_REQUEST['name'] . "<br>";
$message .= 'Company: ' . $_REQUEST['company'] . "<br>";
$message .= $_REQUEST['message'];
if (#mail($to, $subject, $message, $headers))
{
// Transfer the value 'sent' to ajax function for showing success message.
echo 'sent';
// header('Location: ../index.html');
}
else
{
// Transfer the value 'failed' to ajax function for showing error message.
echo 'failed';
}
?>
There are some problem in setting the header.
And most important is that you need to define the correct and valid Email Id in the From section because google generally used to validate the domain, the mail coming from.
If it is not white-listed at google end then it wills end the mail to Spam automatically, that is happening now as I think.
The problem is simple that the PHP-Mail function is not using a well configured SMTP Server.
Nowadays Email-Clients and Servers perform massive checks on the emails sending server, like Reverse-DNS-Lookups, Graylisting and whatevs. All this tests will fail with the php mail() function. If you are using a dynamic ip, its even worse.Use the PHPMailer-Class and configure it to use smtp-auth along with a well configured, dedicated SMTP Server (either a local one, or a remote one) and your problems are
You can try the below code.
$headers = "From: myplace#example.com\r\n";
$headers .= "Reply-To: myplace2#example.com\r\n";
$headers .= "Return-Path: myplace#example.com\r\n";
$headers .= "CC: sombodyelse#example.com\r\n";
$headers .= "BCC: hidden#example.com\r\n";
Reference Link.
https://github.com/PHPMailer/PHPMailerPHPMailer/PHPMailerPHPMailer
There is another code you can try:
function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
$file = $path.$filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed;boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/plain;charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/octet-stream;name=\"".$filename."\"\r\n";
// use different content types here$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment;filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
if (mail($mailto, $subject, "", $header)) {echo "mail send ... OK";
// or use booleans here} else {echo "mail send ... ERROR!";
}
}
If you are developing this program in local server. The emails will not be sent to your gmail account.
If you want to test your code on a local machine please install Test Mail Server Tool.
No email will be delivered while running on local machine but you will get an idea how the email will look like.
When you run the same on web hosting server, the email will be delivered to the email id specified in $to field.
Related
I am using a PHP script to send e-mails to a list of friends. This used to work successfully but now the only e-mails that are sent successfully are those inside my domain ("mycompany.com"). Any e-mails outside my domain fail to be sent. Is this a problem with permissions? Could I fix it by changing the headers I use when I call the mail() function? Here is the PHP script:
$name = "Joe Smith";
$email = "jsmith#mycompany.com";
$to = "ajones#mycompany.com, tom#abc.com, ted#def.com, mary#ghi.com";
$subject = "Dinner Invitations";
$message = "Invitation to Dinner";
$headers = "From: ".$name."<".$email.">\r\n";
$headers .= "Reply-To: ".$email."\r\n";
$headers .= "Return-Path: ".$email."\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'Content-Transfer-Encoding: quoted-printable' . "\r\n";
mail($to, $subject, $message, $headers);
Only ajones#mycompany.com was sent out by the server. tom#abc.com, ted#def.com, and mary#ghi.com did not get sent.
Any suggestions would be deeply appreciated!
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
i want to send a mail the code is given below. i cant understand waht is happening . It works on localhost but not on live server.
if (isset($_POST['test_mail'])){
$to = '2606ankit#gmail.com'; //put email address on which mail send
$subject = "Newsletter"; //Put subject of mail here
$from = 'ankit#studiokrew.com'; //put email address from
//email body start
// $body .= file_get_contents('file/'.$filename.'');
$body .= 'anio';
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
// More headers
$headers .= 'From: '.$from. "\r\n";
//if you need to send cc mail then uncomment below line and change email address
//$headers .= 'Cc: myboss#example.com' . "\r\n";
mail($to,$subject,$body,$headers);
}
try this..
<?php if (isset($_POST['test_mail'])){
$host=$_SERVER['HTTP_HOST'];
$replyto="<no-reply >";
$to ='2606ankit#gmail.com';
$subject = "Newsletter";
$from = 'ankit#studiokrew.com';
$headers = "From: \"Invoice\"<noreply#$host>\n";
$headers .= "Reply-To: ".$replyto."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"abc"\"\r\n\r\n";
$headers .= 'From: '.$from. "\r\n";
$header .= "Content-Type:text/html; charset=\"iso-8859-1\"\n";
$body = "This is a multi-part message in MIME format.\r\n";
$body .= "Content-type:text/html; charset=iso-8859-1\r\n";
$body .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$body .= 'anio';
mail($to,$subject,$body,$headers);
}
?>
check the MTA logs on your server.also web server logs the answer will be there somewhere.
Test with a simple mail() script that does not require any external data, just to make sure that your server indeed can send mail. If that fails, you'd have to contact the server admins for help.
I am using the following code for sending attachments to the email...but i am unable to get the email..
I am able to get the message for successfully sending mail like " message sent " but the email is not going to corresponding mail id..
I don't where i do wrong....
Here is php file ...
<?php
if(isset ($_POST["send"]))
{
$upload_name=$_FILES["upload"]["name"];
$upload_type=$_FILES["upload"]["type"];
$upload_size=$_FILES["upload"]["size"];
$upload_temp=$_FILES["upload"]["tmp_name"];
$message=$_POST["msg"];
$subject = $_POST["subject"];
$to=$_POST["to"];
if($message==""||$subject==""||$to=="")
{
echo '<font style="font-family:Verdana, Arial; font-size:11px; color:#F3363F; font-weight:bold">Please fill all fields</font>';
}
else
{
$fp = fopen($upload_temp, "rb");
$file = fread($fp, $upload_size);
$file = chunk_split(base64_encode($file));
$num = md5(time());
//Normal headers
$headers = "From: Info Mail<Info#example.com>\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; ";
$headers .= "boundary=".$num."\r\n";
$headers .= "--$num\r\n";
// This two steps to help avoid spam
$headers .= "Message-ID: <".gettimeofday()." TheSystem#".$_SERVER['SERVER_NAME'].">\r\n";
$headers .= "X-Mailer: PHP v".phpversion()."\r\n";
// With message
$headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
$headers .= "".$message."\n";
$headers .= "--".$num."\n";
// Attachment headers
$headers .= "Content-Type:".$upload_type." ";
$headers .= "name=\"".$upload_name."\"r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; ";
$headers .= "filename=\"".$upload_name."\"\r\n\n";
$headers .= "".$file."\r\n";
$headers .= "--".$num."--";
// SEND MAIL
mail($to, $subject, $message, $headers);
fclose($fp);
echo '<font style="color:#333333">Mail sent please check inbox and spam both <br /></font>';
}
}
?>
//HTML code for form details and adding attachment..
<form id="attach" name="attach" method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>" enctype="multipart/form-data">
//Html code for mail details
</form>
I am getting "Warning: mail() [function.mail]: Bad parameters to mail() function, mail not sent...." and also it is showing the "Mail sent please check inbox and spam both ".. what's the problem ?
Help me to fix this problem...
You're passing bad data to the mail function. Head over to http://php.net/manual/en/function.mail.php and read the documentation that tells you how to use it.
Also note that it returns a boolean value that tells you whether your attempt to mail was successful so at the very least:
....
$success = mail(...);
if ($success) {
echo "<p>mail sent</p>";
} else {
echo "<p>error: mail was not sent.</p>";
}
so that your code isn't lying about whether it sent the mail or not.
As for your massive amount of data: comment it all off. Then progressive uncomment bits until it breaks. You have now found the problem and can solve it by looking at what you're giving the mail() function that it doesn't like.
Hi I am trying to send an entire webpage via e-mail. I receive it in Mail.app in my Mac and i can see it fine. But when i see it in gmail or hotmail, It didn't get the styles
How can I do this correctly. Or how to transform the webpage to PDF or PNG and send it via Mail [I can't install anything to the server]
Edit: It send all the information but without Style
My code:
$mail = $_POST['mail'];
if(($Content = file_get_contents("http://google.com")) === false) {
$Content = "";
}
$Headers = "MIME-Version: 1.0\n";
$Headers .= "Content-type: text/html; charset=iso-8859-1\n";
$Headers .= "From: a#a.com <a#a.com>\n";
$Headers .= "Reply-To: a#a.com\n";
$Headers .= "X-Sender: <a#a.com>\n";
$Headers .= "X-Mailer: PHP\n";
$Headers .= "X-Priority: 1\n";
$Headers .= "Return-Path: <a#a.com>\n";
if(mail($mail, $subject, $Content, $Headers) == false) {
//Error
}
Thanks
Remove all external style sheets . All the styles on the page should be inline or defined within the page
I have a form to signup yourself in a mailing list.
I had set up the script to send me a mail for every signup and confirmation.
The last days I saw a bunch of empty submissions (rejected). Turns out filter_input is that good :-)
But I want to see the input of the malicious users, so I'm encoding the input and sending it to me by mail
htmlentities($_POST['userName'], ENT_QUOTES, "UTF-8")
Is that secure?
Should I add mysql_real_escape_string() ?
It is possible to craft a post in such a way that will be able to do any harm while sending the mail to me?
I'm not writing rejected submisions to the database, only mailing them to me.
Here's the relevant code.
<?php
$userName = filter_input(INPUT_POST, 'userName', FILTER_SANITIZE_STRING);
$userEmail = filter_input(INPUT_POST, 'userEmail', FILTER_VALIDATE_EMAIL);
if(!checkdnsrr(array_pop(explode("#",$userEmail)),"A"))
$hostInvalido = true;
if(!empty($userName) && $userName!==FALSE && !empty($userEmail) && $userEmail!==FALSE && !isset($hostInvalido) ) {
//All ok
} else {
echo "Datos invalidos, por favor, intenta nuevamente.";
$fromaddress="info#example.com";
$fromname="Error reporting";
$to= "webmaster <webmaster#example.com>";
$subject="MailList: Error ";
$userName = htmlentities($_POST['userName'], ENT_QUOTES, "UTF-8"); // is this secure?
$userEmail = htmlentities($_POST['userEmail'], ENT_QUOTES, "UTF-8");
$body = "Nombre: $userName<br>Email: $userEmail";
$body .= (isset($hostInvalido)) ? "<br>Host invalido" : "";
$ret = send_mail($to, $body, $subject, $fromaddress, $fromname, $attachments=false);
}
// I'm including this function so you can analyze it for any possible vulnerability
function send_mail($to, $body, $subject, $fromaddress, $fromname, $attachments=false) {
$eol="\r\n";
$mime_boundary=md5(time());
# Common Headers
$headers = "";
$headers .= "From: ".$fromname."<".$fromaddress.">".$eol;
$headers .= "Reply-To: ".$fromname."<".$fromaddress.">".$eol;
$headers .= "Return-Path: ".$fromname."<".$fromaddress.">".$eol; // these two to set reply address
$headers .= "Message-ID: <".time()."-".$fromaddress.">".$eol;
$headers .= "X-Mailer: PHP v".phpversion().$eol; // These two to help avoid spam-filters
# Boundry for marking the split & Multitype Headers
$headers .= 'MIME-Version: 1.0'.$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$mime_boundary."\"".$eol.$eol;
# Open the first part of the mail
$msg = "--".$mime_boundary.$eol;
$htmlalt_mime_boundary = $mime_boundary."_htmlalt"; //we must define a different MIME boundary for this section
# Setup for text OR html -
$msg .= "Content-Type: multipart/alternative; boundary=\"".$htmlalt_mime_boundary."\"".$eol.$eol;
# Text Version
$msg .= "--".$htmlalt_mime_boundary.$eol;
$msg .= "Content-Type: text/plain; charset=UTF-8".$eol; //iso-8859-1
$msg .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$msg .= strip_tags(str_replace("<br>", "\n", $body)).$eol.$eol; //mb_substr($body, (strpos($body, "<body>")+6))
# HTML Version
$msg .= "--".$htmlalt_mime_boundary.$eol;
$msg .= "Content-Type: text/html; charset=UTF-8".$eol;//iso-8859-1
$msg .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$msg .= $body.$eol.$eol;
//close the html/plain text alternate portion
$msg .= "--".$htmlalt_mime_boundary."--".$eol.$eol;
if ($attachments !== false)
{
for($i=0; $i < count($attachments); $i++)
{
if (is_file($attachments[$i]["file"]))
{
# File for Attachment
$file_name = mb_substr($attachments[$i]["file"], (strrpos($attachments[$i]["file"], "/")+1));
$handle=fopen($attachments[$i]["file"], 'rb');
$f_contents=fread($handle, filesize($attachments[$i]["file"]));
$f_contents=chunk_split(base64_encode($f_contents)); //Encode The Data For Transition using base64_encode();
$f_type=filetype($attachments[$i]["file"]);
fclose($handle);
# Attachment
$msg .= "--".$mime_boundary.$eol;
$msg .= "Content-Type: ".$attachments[$i]["content_type"]."; name=\"".$file_name."\"".$eol; // sometimes i have to send MS Word, use 'msword' instead of 'pdf'
$msg .= "Content-Transfer-Encoding: base64".$eol;
$msg .= "Content-Description: ".$file_name.$eol;
$msg .= "Content-Disposition: attachment; filename=\"".$file_name."\"".$eol.$eol; // !! This line needs TWO end of lines !! IMPORTANT !!
$msg .= $f_contents.$eol.$eol;
}
}
}
# Finished
$msg .= "--".$mime_boundary."--".$eol.$eol; // finish with two eol's for better security. see Injection.
# SEND THE EMAIL
ini_set('sendmail_from',$fromaddress); // the INI lines are to force the From Address to be used !
$mail_sent = mail($to, $subject, $msg, $headers);
ini_restore('sendmail_from');
return $mail_sent;
}
?>
XSS injections is mainly javascript, so using htmlentities() is sufficient :)
If you are still worried, drop the message into a < textarea>< /textarea> and then mail it.