Sending mails in PHP using Yahoo SMTP - php

How can I send an email via Yahoo!'s SMTP servers in PHP?

You should use something like Swift Mailer or PHPMailer. The following example is for Swift:
$message = Swift_Message::newInstance()
->setSubject('Your subject')
->setFrom(array('john#doe.com' => 'John Doe'))
->setTo(array('receiver#domain.org', 'other#domain.org' => 'A name'))
->setBody('Here is the message itself')
->addPart('<q>Here is the message itself</q>', 'text/html')
;
$transport = Swift_SmtpTransport::newInstance('smtp.mail.yahoo.com', 465, 'ssl')
->setUsername('your username')
->setPassword('your password')
;
$mailer = Swift_Mailer::newInstance($transport);
$result = $mailer->send($message);

You can use PHP's built-in mail() function to send mails, however it is generally very limited. For instance, I don't think you can use other SMTP servers than the one specified in your php.ini file.
Instead you should take a look at the Mail PEAR package. For example:
<?php
require_once "Mail.php";
$from = "Sandra Sender <sender#example.com>";
$to = "Ramona Recipient <recipient#example.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "mail.example.com";
$username = "smtp_username";
$password = "smtp_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>");
}
?>
(I stole this example from http://email.about.com/od/emailprogrammingtips/qt/PHP_Email_SMTP_Authentication.htm :P)

Read this http://php.net/manual/en/function.mail.php
<?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);
?>

PHP mailer will let you use any SMTP server you like, so long as you have the credentials to log on with.

Related

how to send attachment with pear smtp

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.

how do i configure smtp setting with 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

signing gmail with php imap

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

sending mail from my account. Not webhosts

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>");
}

Why mail() not sending email?

Why the below code not sending mail to me? What is the error?
<?php
if(isset($_POST['name'])){
$msg="Name: ".$_POST['name']."\n Email: ".$_POST['email']."\n Address: ".$_POST['city']."\n Phone: ".$_POST['phone'];
mail('sganake#gmail.com', 'New Trial Request', $msg);
echo '<h2 align="center" style="color:green">Thank you for your message.</h2>';
} ?>
No error I got. Just I have not received my email at inbox. This is running on IIS Server.
Try this
$headers = 'From: from#address.com' . "\r\n";
$validate = mail('sganake#gmail.com', 'New Trial Request', $msg, $headers);
if($validate)
{
echo '<h2 align="center" style="color:green">Thank you for your message.</h2>';
}
else
{
echo '<h2 align="center" style="color:red">Something went wrong.</h2>';
}
If you get 'Something went wrong' it means problem is in your mail server and not in PHP code.
I suggest you to use PEAR::Mail package. You can send emails via SMTP then.
require_once "Mail.php";
$from = "your#gmail.com";
$to = "sganake#gmail.com";
$subject = "New Trial Request";
$body = "Name $name, Address $address ...";
$host = "ssl://smtp.gmail.com";
$port = 465;
$username = "your#gmail.com";
$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 has been sent!</p>");
}
You might have to send Mail Using SMTP Authenticationas which is required by many mail servers.
Check this link for further details.
May be your php configuration is not complete, see in C://xampp/php/php.ini in:
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
This is for activate email. May be your setting is:
;sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
Although I didnt change anything in your code. please try this
<?php
if(isset($_POST['name'])){
$to = 'sganake#gmail.com';
$subject = 'the subject';
$message = 'hello';
$msg='Name:'.$_POST{name}. "\r\n".
'Email: '.$_POST{email}. "\r\n".
'Address: '.$_POST{city}. "\r\n".
'Phone: '.$_POST{phone}. "\r\n";
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$sent = mail($to, $subject, $message, $headers);
var_dump($sent) // just to debug
echo '<h2 align="center" style="color:green">Thank you for your message.</h2>';
} ?>

Categories