PHP email text to gmail account - php

Is there some way I can take text (retrieved from a form), and email it to my gmail account? I can also have the user enter their email address, and a subject. Or if not is there a better way to have users send me a message? Thanks.

Use mail function to send email to specific address:
$subject = $_REQUEST['subject'];
$message = $_REQUEST['message'];
mail("your#gmail.com", $subject, $message);
But please don't take $to parameter from form, otherwise your script will be used for spamming.

I recommend that you use PHP Mailer this program will take care of all of the message construction and works well with Gmail. There is also sample code included for Gmail.

Expanding on what Ivan wrote to add users email as sender:
$subject = $_POST['subject'];
$message = $_POST['message'];
$from = $_POST['from'];
// the sender email must be cleaned of mime header injection
$from = preg_replace_all("/[\r\n]+/", '', $from);
mail("your#gmail.com", $subject, $message, "from:$from\n");
This makes it easier to reply. However, you could just append their email address in the message body.

Related

Php email function not working properly

I know this seems like it is a duplicate but please read first:
I have the following php code:
<?php
$to = 'myemail#yahoo.com';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$headers = "From: ".$email." \r\n";
$headers .= "Reply-To: ".$email."\r\n";
mail($to, $subject, $message, $headers);
?>
I think it's the standard email sending script. However, I face an interesting bug. My website is florin-pop.com and the emails are only sending when in the email input field I put something like this: blahblah#florin-pop.com or mama#florin-pop.com or anything before #florin-pop.com.
If I try to put a something different like test#yahoo.com or even a real yahoo email address I don't get the email. Why? It's something wrong with my code? It may be from the hosting company? ( I use hostgator ).
EDIT:
If I change the Reply-To to the domains email address then it is working, but it is still not the perfect way to do it. If you press the reply button and forget about this trick, you will email yourself.
Code:
<?php
$to = 'myemail#yahoo.com';
$my_domain_email = 'myemail#mydomain.com';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$headers = "From: ".$email." \r\n";
$headers .= "Reply-To: ".$my_domain_email."\r\n";
mail($to, $subject, $message, $headers);
?>
In this case, delivery failure may be caused by Yahoo's adoption of the Domain-based Message Authentication, Reporting, and Conformance (DMARC) policy.
This means all DMARC compliant mail receivers (including Yahoo,
Hotmail, and Gmail) are now bouncing emails sent as "#yahoo.com"
addresses that aren't sent through Yahoo servers. [Yahoo]
Twitter, Facebook, Linked In, Paypal, AOL, Comcast and others have also adopted this policy. [Venture Beat]
A solution: Change the "From" header to an address at the server from which you are sending the email. This (correctly) indicates that the mail was sent from your server, and not from Yahoo. You can still use a user-submitted address in the "Reply-To" header so that the recipient can reply to the sender.
As a best practice, you should ... be using a domain you control in ... the "From:" header... [For example,] the site visitor's name is shown in the descriptive part of the "From:" header, and the "Reply-To:" header is set to the website visitor's address, but the actual address used in the "From:" header clearly indicates that your website is the origin of the message. [DMARC]

Is it possible to specify "Reply From" email address in form submission using PHP?

I have a form on my website which will send me an email using PHP. It will be sent to an email address from my hosting service, which I never check, so I set it up to forward emails sent to that address to my iCloud email address. This works great, since it preserves the "From" address so when I reply, it is sent to the person who filled out the form. The problem is, when I reply to the email sent from the form, the person who gets that reply will see it came from my iCloud account, and replies to that email will go directly to my iCloud account rather than to my hosting service email.
My question is, is there a way to specify in my PHP code what email should appear in the "From" field upon replying to the email (but of course, this is different from the "From" address in the first email sent to me (the person filling out the form)?
Essentially, I want it to work like this:
-Upon form submission, an email is sent to example#myhostingservice.com with the "From" address set to the email inputted in the form
-example#myhostingservice.com forwards the email to my iCloud account (but preserves the email address of person who filled out form in the "From" address)
-I reply to that email using my iCloud account, and the receiver sees it came from example#myhostingservice.com (not iCloud), and when they reply it goes to example#myhostingservice.com
Here is a snippet of my code to see what I'm working with:
$name = $_POST['name'];
$email_address = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
if( empty($errors))
{
$to = $myemail;
$email_subject = "$subject";
$email_body = "Name: $name \n \n $message";
$headers = "From: $email_address";
mail($to,$email_subject,$email_body,$headers);
}
Thanks!
From docs (http://php.net/manual/en/function.mail.php):
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
Although I must warn you, doing such may mark your email as spam since the email address it is being sent from does not match the From header.
I don't think this is possible using PHP - the email client you use on your iCloud account sets the new "Reply-To". However, try to add the hosting email account to your iCloud email client -(Using POP3 or IMAP)- then you will be able to "Send as" your hosting account and you won't need to forward everything to iCloud.

Exchanging emails via gmail without revealing real email addresses

I have a php forum which has 50 users and i am wondering how i can exchange emails between members without revealing their real gmail email addresses.The forum works this way.A user 'A' opens a contact form and writes and email and sends it to user 'B'.The email sent is received by user 'B' but does not reveal the real email address of user 'A'.When user 'B' replies,user 'B's' email is not attached anywhere in the email.
Are their existing solutions for this kind of thing.
Have a look at the PHP mail() function.
$to = 'send_to#gmail.com';
$subject = 'Some message subject';
$message = 'Oi, what\'s up?';
$headers = "From: Bob < some_other_email#gmail.com>\r\n";
mail($to, $subject, $message, $headers);

Send e-mail with PHP to Facebook group mail address

1, I tried to send e-mail in PHP to my Facebook group email, but these updates, doesn't appear. If I send the e-mail from my yahoo mail, it posted instantly. What kind of information should I add to the mail header?
2, Is is possible to add images as attachments?
My current mail sender code is:
<?php
$to = "mygroup#groups.facebook.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "me#yahoo.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
Does the email need to come from "me#yahoo.com"? Because Yahoo mail uses SPF, which Facebook can use to verify that your email didn't actually come from there, and discard your email as a spoofing attempt.
If the email needs to come from me#yahoo.com, you'll need to connect to Yahoo mail through SMTP, and pass your username and password through, and legitimately send your email from there.
As for image attachments, it's possible.
EDIT: As per the lovely DaveRandom's comment, the following PHP packages should make your job MUCH easier: PHPMailer or SwiftMailer

How do i create an email contact form for my website

I'm looking to create a form on my website where users can enter their email address and a message (however long). Then upon clicking on a submit button, the code, sends the message as an email to my mailbox.
I was wondering if someone knew what code i could use. I am using PHP as my server-side language.
You need to have access to an smtp server somewhere. Assuming you do, use the php mail function to send the mail like so:
$Name = "John doe"; //senders name
$email = "email#adress.com"; //senders e-mail adress
$recipient = "recipient#emailadress.com"; //recipient
$mail_body = "The text for the mail..."; //mail body
$subject = "Subject for receiver"; //subject
$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields
mail($recipient, $subject, $mail_body, $header);
The smtp server is set in the php.ini file in these two lines:
SMTP = servername
smtp_port = 25
More information at w3schools site.
while the above two answers provide a basic email sending suggestion, there's one thing you should consider, the codes are not secure. spammers can inject Cc: codes and send spam using the form. if they do, your smtp provider may ban your account. try a dedicated mailer, like phpmailer
Try to use PHPForms. It is an ultimate web form builder that allows to create professionally looking email forms within just a few minutes. You will only need to create a PHP form by means of an intuitive interface and generate a code that can be easily copied and pasted to any web page.

Categories