Not able to send email to gmail address in PHP - php

I am trying to send an email from PHP.
The below code works fine when the From address is a Non-Gmail address.
However, when I use a Gmail address as a From Address, I don't receive the email. I have checked spam as well.
Can you tell any reason for this behaviour?
<?php
// validation expected data exists
if (!isset($_REQUEST['emailto']) ||
!isset($_REQUEST['emailsub']) ||
!isset($_REQUEST['emailfrom']) ||
!isset($_REQUEST['emailsub'])) {
exit;
}
$emailto = $_REQUEST['emailto'] ;
$emailsub = $_REQUEST['emailsub'] ;
$emailfrom = $_REQUEST['emailfrom'] ;
$emailmsg = $_REQUEST['emailmsg'] ;
// create email headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=utf-8\r\n";
$headers .= "From: " . $emailfrom . "\r\n";
$headers .= "Reply-To: " . $emailfrom . "\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();
mail($emailto, $emailsub, $emailmsg, $headers));
?>
Thanks

In order to use some GMAIL.COM email address as FROM address, you must use GMAIL.COM SMTP server to send out emails. Your best shot is to use phpMailer as #biesior already mentioned.

Related

mail() function not working for domain mail address [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I am trying to use php mail() function on my new web server(linux based server) for my website. The issue is, emails are not sending to domain email addresses like some1#domain.com but its working fine for gmail, yahoo. I don't know what is the issue on it?. please give me suggestions or advice how to solve this issue. I want to send emails to domain-based email addresses.
my code is
//$to = $_POST['femail'];
$to = "<toadd#domain.com>";
$message = "
<html>
<head>
<title>".$subject."</title>
</head>
<body>
<p>Registration request from site</p>
<table>
<tr>
<td>Project Requested</td>
<td>".$project."</td>
</tr>
</table>
</body>
</html>";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: <info#domain.com>' . "\r\n";
$headers .= 'Cc: <some#domain.com>' . "\r\n";
$headers .= 'X-Mailer: PHP/' .PHP_VERSION. "\r\n";
#mail($to,$subject,$message,$headers);
I have faced the same issue..after a talk with hosting service provider I came to know that either sender or receiver's email id should be of the hosting domain id like if you have a site test.com either sender or receiver should have #test.com so you may do the same or talk to your hosting service provider.
$to = "toadd#domain.com"; //to address
$subject ="Your Subject";
$message = "Your message";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: info#domain.com' . "\r\n"; //from address
if (mail($to, $subject, $message, $headers) )
{
echo "Mail sent successfully";
}
else
{
echo "failed to send mail";
}

Personalised 'To' field when sending large email list using PHP mail and multiple addresses in the Bcc

Im trying to remember a trick i was taught a while back but can not.
Basically, im using PHP mail() in this fashion:
$to = "emailAddress1#domain.com";
$subject = "Welcome to BuildSanctuary";
$messageContent = "Thankyou for registering Bla bla bla";
$message = 'SOME HTML EMAIL STUFF including the $messageContent var';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To:' . "\r\n";
$headers .= 'From: Accounts<accounts#domain.com>' . "\r\n";
$headers .= 'Bcc: emailAddress1#domain.com,emailAddress2#domain.com,emailAddress3#domain.com,emailAddress4#domain.com' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
I got told a while back that there was a way that you could put your own email in the to field, but when the users receive the emails it looks like they were personally sent the email and the to field shows just their email.
Is this possible? Cant think how.
Thanks.

Add recieving email adress in contact form

I'm working with contact form
This is part where email is being sending
$emailTo = 'myemail#gmail.com';
$subject = 'Wiadomość ze strony Mud';
$sendCopy = trim($_POST['sendCopy']);
$body = "Email: $email \n\nKind of frame: $frame \n\nColor suggestion: $color \n\nInfo about project: $comments";
$headers = 'From: ' . ' <' . $emailTo . '>' . "\r\n" . 'Reply-To: ' . $email;
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=UTF-8\r\n";
mail($emailTo, $subject, $body, $headers);
// set our boolean completion value to TRUE
$emailSent = true;
Form works good everything is fine, except when I get mail and I want IN MY MAILBOX reply email it shows adress of my webserver
It looks like $headers dont work properly, If I'm right this part of code is responsible for reply adres email "'Reply-To: ' . $email;"
(IM NOT GOOD IN PHP)
And my second optional question is how to make stylish email.
I mean for example in my email when it comes to mailbox it shows
Email: dsadas#dsdas.pl
Kind of frame: dasdsa
Color suggestion: dasdsa
Info about project: dasdsa
How can I do it bold, color etc ?
THANK YOU FOR YOUR TIME, CHEERS
Here you go for headers that will allow HTML for email and fix your reply to email:
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-Type: text/html; charset=utf-8" . "\r\n";
$headers .= "From: $emailTo <$emailTo>" . "\r\n";
Remember that there is limited support for email when it comes to CSS, you can see things that can't be used here: http://www.campaignmonitor.com/css/

how to i send bcc or cc email in my mail function but i have tried code but bcc or cc function is not working? [duplicate]

i want to use bcc or cc function in this mail function?
Here My Mail Function
<?php
//SENDS EMAIL THAT TELLS THE USER TO ACTIVATE THE ACCOUNT
$activation = 'activation.php?key='.$key;
$your_email = 'non-reply#mydomain.pk'; //CHANGE TO YOUR SETTINGS
$domain = $_SERVER["HTTP_HOST"]; //YOUR DOMAIN AND EXTENSION
$to = $email;
$subject = 'MyDomain Activate Account';
$message .='<img src="http://mydomain.com/images/securedownload.jpg"/>';
$message = 'Welcome,<br/> '.$_POST['username'].'. You must activate your account via this message to log in. Click the following link to do so: http://'.$domain.'/'.$activation;
$headers = 'From: Mydomain<'.$your_email.'#'.$domain.'>\r\n'; //MODIFY TO YOUR SETTINGS
$headers .= 'Content-type: text/html\r\n';
mail($to, $subject, $message, $headers);
?>
You should add to the to and headers part of the mail.
For TO part
$to = "to#to.com, cc#cc.com"
For HEADERS part
$headers .= "To: To Name <to#to.com>\n";
$headers .= "CC: CC Name <cc#cc.com>\n";
$headers .= "BCC: BCC Name <bcc#bcc.com>\n";
It is very simple, just sharing if anyone gets help from here:
//......
//...Other setting goes here....
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: My Name <myemail#example.com>'. "\r\n";
//Multiple CC can be added, if we need (comma separated);
$headers .= 'Cc: myboss1#example.com, myboss2#example.com' . "\r\n";
//Multiple BCC, same as CC above;
$headers .= 'Bcc: myboss3#example.com, myboss4#example.com' . "\r\n";
mail($to, $subject, $message, $headers);
Just add the Bcc/CC in your Code
<?php
//SENDS EMAIL THAT TELLS THE USER TO ACTIVATE THE ACCOUNT
$activation = 'activation.php?key='.$key;
$your_email = 'non-reply#mydomain.pk'; //CHANGE TO YOUR SETTINGS
$domain = $_SERVER["HTTP_HOST"]; //YOUR DOMAIN AND EXTENSION
$to = $email;
$subject = 'MyDomain Activate Account';
$message .='<img src="http://mydomain.com/images/securedownload.jpg"/>';
$message = 'Welcome,<br/> '.$_POST['username'].'. You must activate your account via this message to log in. Click the following link to do so: http://'.$domain.'/'.$activation;
$headers = 'From: Mydomain<'.$your_email.'#'.$domain.'>\r\n'; //MODIFY TO YOUR SETTINGS
$headers .= "BCC: email#domain.com;\r\n"
$headers .= 'Content-type: text/html\r\n';
mail($to, $subject, $message, $headers);
?>

php mail function with customizes from value

Why does this send emails?
$from_admin = $_POST[EMAIL];
$message_admin = 'some html...';
mail($to_admin,"subject",$message_admin,"FROM:$from_admin");
This email looks like it came from my gmail account.
While this doesn't send the email.
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'To:' . $to . "\r\n";
$headers .= 'From: Registration <register#myserver.org>' . "\r\n";
mail($to, $subject, $message, $headers);
When I contacted my hosting support, they said that I need to do SMTP authentication. But, then why does the first mail function work? I would be ok if its not actually using that server, but people can reply the the html email and still reach the correct account. Is there any way around this?

Categories