I got a contact form that succesfully sends emails to me.
heres the extract:
$_POST['message'] = wordwrap($_POST['message'], 70);
mail ('myemail#test.com', $_POST['subject'], $_POST['message'] , $_POST['email']);
echo "<div class='registertext'>Your email was succesfully sent to a member of the administration team. Please wait 24 hours for as to reply and ensure you check your junk mail!<br />To login please click <a href='login.php'>here</a></div>";
The issue I have is, the email gets sent from my host. Not a email I want to specify. How would I overcome this?
You can specify it in the email headers:
$recipient = "recipient#test.com";
$from = "You#yoursite.com";
$replyTo = "You#yoursite.com";
$subject = "Hi!";
$text = "<p>This is a test!<p>";
$headers = "MIME-Version: 1.0\r\n"
."Content-Type: text/html; charset=utf-8\r\n"
."Content-Transfer-Encoding: 8bit\r\n"
."From: =?UTF-8?B?". base64_encode([Your Name]) ."?= <$from>\r\n"
."Reply-To: $replyTo\r\n"
."X-Mailer: PHP/". phpversion();
//send it!
if (mail($recipients, $subject, $text, $headers, "-f $from")){
echo "sent";
} else {
echo "did not send";
};
but there's a good chance it will get caught be SPAM filters. Your best bet in this case would be to use a PHP mailing library that handles SMTP emailing and uses your actual account to send mail (there are several packages that can handle this for you: Pear Mail, and PHP Mailer amongst others.
You can use PEAR mail which will use a SMTP account. Here is some code from my mail form I use
$from = "Name <webmaster#domain.com>";
$to = "Name <address#domain.com>";
$subject = "Subject";
$body = 'A message!';
$host = "ssl://domain.com";
$port = "465";
$username = "username";
$password = "password";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
Related
I want to send a file with email using pear smtp. Email is successfully going to the email. Also an attachment is going with email but with noname and no content in it. When i download the attachment it has only one line "This is a multi-part message in MIME format...".
I didn't understand whats wrong with it. I have attached the code with this question that i am using to send the attachment with email. Please guys review once and let me know what's wrong with this code.
Thanks in advance.
require_once "Mail.php";
include('Mail/mime.php');
$from = "Dispatcher <server#mymailserver.com>";
$host = "smtp.mymailserver.com";
$username = "mailuser";
$password = "password";
$to = "email#example.com";
$subject = "Subject of the email";
$hdrs = array ('From' => $from,
'To' => $to,
'Subject' => $subject,
);
$text = 'Text version of email';
$html = '<html><body>HTML version of email</body></html>';
$file = './test.txt';
$crlf = "rn";
$mime = new Mail_mime($crlf);
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addAttachment($file);
$body = $mime->get();
$hdrs = $mime->headers($hdrs);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
return 1;
}
$crlf means "carriage return, line feed" and should be that.
Use "\n" to make it work with e-mails.
I am using Php Pear mail for sending an attachment to the user who fills out a form. If I hard code the "to email" address it works fine. But when I use
$to = $_POST['email'] ; I get the following error.
Failed to add recipient: #localhost [SMTP: Invalid response code received from server (code: 501, response: <#localhost>: no local part)]
<?php
require_once 'Mail.php';
require_once 'Mail/mime.php';
$from = "email#domain.com";
$to = $_POST['email'] ;
$subject = 'Free Diagnostic Test Coupon';
$headers = array ('From' => $from,'To' => $to, 'Subject' => $subject);
$text = 'Please find attached the coupon';// text and html versions of email.
$html = '<html><body>Please find attached the coupon</body> </html>';
$file = 'img/coupon.jpg'; // attachment
$crlf = "\n";
$mime = new Mail_mime($crlf);
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addAttachment($file, 'image/jpeg');
//do not ever try to call these lines
$host = "host";
$username = "username";
$password = "password";
in reverse order
$body = $mime->get();
$headers = $mime->headers($headers);
$smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => true,
'username' => $username,'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
}
else {
echo("<p>Message successfully sent!</p>");
}
?>
Any help will be appreciated.
Check your code by adding the top of it.
error_reporting(E_ALL); ini_set('display_errors', '1');
I have the following code that sends an email properly, but the email is received with a FROM value of root#mysite.com . I would like to have the value of the received email FROM be the value email (that is entered in a contact form) so that the received email can be replied top easily. Any assistance would be appreciated.
Thanx
<?php
if ($_POST["email"]<>'')
{
require_once "Mail.php";
$from = "root#mysite.com";
$to = "info <info#mysite.com>";
$subject = "Contact us info from Website";
$body = "Name: ".$_POST["name"]."\r\n";
$body .= "Email: ".$_POST["email"]."\r\n";
$body .= "Phone: ".$_POST["phone"]."\r\n";
$body .= "Comment: ".nl2br($_POST["comment"])."";
$host = "mail.mysite.com";
$username = "root#mysite.com";
$password = "abcdefghijk";
$headers = array ('From' => $from,'To' => $to, 'Subject' => $subject);
$smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => true, 'username' => $username, 'password' => $password));
$mail = $smtp->send($to, $headers, $body, "FROM: $email","-f$replyToEmail");
mail("$toEmail", $subject, $message, "FROM: $fromEmail","-f$replyToEmail");
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>
Just set
$from = $_POST['email'];
You do it like this.
$fromEmail= 'From:'.$_POST["email"];
use $fromEmail in your mail function.
how do i configure smtp setting with mail function smtp is required because of web hosting doesn't allow to sending email without smtp setting due to security reason.
warning error: mail() has been disabled for security reasons
now how can i configure smtp setting with this mail function ()?
<?php
if(isset($_REQUEST['confirm'])){
$your_email = 'non-reply#test.com'; //CHANGE TO YOUR SETTINGS
$domain = $_SERVER["HTTP_HOST"]; //YOUR DOMAIN AND EXTENSION
$to = $email;
$subject = 'Demo Your Order';
$message = 'test1234';
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: Demo.Pk<'.$your_email.'#'.$domain.'>\r\n" .
//"Reply-To: $from \r\n" .
"X-Mailer: PHP/" . phpversion();
mail($to, $subject, $message, $headers);
}
?>
It seems that mail is disable or does not have proper setting in your PHP.ini. I think, you can still send email using SMTP set to some other SMTP server like gmail. Please check below code, it might help you in solving this issue. Below code is using Mail.php frm PEAR
<?php
require_once "Mail.php";
$from = "<me#gmail.com>";
$to = "<you#gmail.com>";
$subject = "Hi!";
$body = "Hi,\n\nHope this helps?";
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "me#gmail.com"; //<> give errors
$password = "mypassword";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>
You can also use some other Mail libraries like PHPMailer
My code
$headers = 'MIME-Version: 1.0' . PHP_EOL;
$headers .= 'Content-type: text/html; charset=iso-8859-1' . PHP_EOL;
$headers .= 'From: Me <me#gmail.com>' . PHP_EOL;
imap_mail('you#gmail.com','test',"$output","$headers");
Is there a way to sign the mail? when I use above code to test sending emails, I receive the email but I am getting the error
This message may not have been sent by: me#gmail.com Learn more Report phishing
according to gmail they attach signed data to the headers to authenticate
I am using gmail imap to send the mails
$inbox = imap_open($hostname,$username,$password)
or die('Cannot connect to Gmail: ' . imap_last_error());
Is there a way to authenticate the email using php imap?
If you want to send mails as a Gmail user I recommend using the Gmail's SMTP. As IMAP is mainly used to receive mails from the inbox.
Here's how to send mails from Gmail's SMTP.
First
Make sure the PEAR Mail package is installed.
Typically, in particular with PHP 4 or later, this will have already
been done for you. Just give it a try. (Mail.php)
Then
Sending Mail from PHP Using SMTP Authentication - Example
<?php
require_once "Mail.php";
$from = "Sender <sender#gmail.com>";
$to = "Recipient <recipient#gmail.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "smtp.gmail.com";
$username = "username#gmail.com";
$password = "Gmail Password";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>
Sending Mail from PHP Using SMTP Authentication and SSL Encryption - Example
<?php
require_once "Mail.php";
$from = "Sender <sender#gmail.com>";
$to = "Recipient <recipient#gmail.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "username#gmail.com";
$password = "Gmail Password";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>
Source about.com