[英]Sending email - PHP XAMPP in MAC
我正在Mac本地開發PHP應用程序。 我需要開發某些在某些情況下需要發送電子郵件的功能。 為了開發和測試,我對如何在MAC / XAMPP中執行此操作進行了一些研究。
出於開發目的,我想使用MAC / XAMPP中的現有資源,而不是第三方資源。 希望現場所有需要做的就是使用托管電子郵件基礎結構來更改配置和代碼,使其工作正常。
你能建議怎么做嗎?
(我確實聽說過后綴,但不知道如何配置它?)
我認為此答案將幫助您在XAMPP上配置電子郵件設置。
運行mail(...);
時,可以使用此PHP腳本發送電子郵件mail(...);
。
<?php
$receiver = 'receiver_email@example.com';
$subject = 'Did you know...';
$message = "
<html>
<body>
You can use <b>HTML</b> here for formatting the content.<br>Therefore the header has to be set as text/html
</body>
</html>
";
$header = 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$header .= 'From: Example <noreply@example.com>' . "\r\n";
mail($receiver, $subject, $message, $header);
?>
請參閱文檔 。
我曾經在開發機器上遇到這個問題。 而不是嘗試正確配置SMTP,而是讓另一台服務器為我完成這項工作。 您可以使用cURL庫發布必填字段($ from,$ to,$ body等),遠程計算機上的相應腳本將為您發送電子郵件。
本地機器代碼:
<?php
function curl_post($url, array $post = array(), array $options = array()) {
$defaults = array(
CURLOPT_POST => 1,
CURLOPT_HEADER => 0,
CURLOPT_URL => $url,
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FORBID_REUSE => 1,
CURLOPT_TIMEOUT => 20,
CURLOPT_POSTFIELDS => $post
);
$ch = curl_init();
curl_setopt_array($ch, ($options + $defaults));
if( ! $result = curl_exec($ch)) {
throw new ErrorException ("curl_post error: " . stripslashes(curl_error($ch)));
}
curl_close($ch);
return $result;
}
function email($from, $to, $subject, $body) {
$result = curl_post ("http://server-with-email/my-email-controller.php", array ("to"=>$to, "from"=>$from, "subject"=>$subject, "body"=>$body));
}
// usage:
$result = email ("from@email.com", "to@email.com", "an email for you", "content of mail");
遠程機器代碼: (my-email-controller.php)
<?php
$to = $_POST["to"];
$from = $_POST["from"];
$subject = $_POST["subject"];
$body = $_POST["body"];
$headers =
"Content-Type: text/plain; charset=UTF-8" . "\r\n" .
"MIME-Version: 1.0" . "\r\n" .
"From: $from" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
if (mail ($to, $subject, $body , $headers)===TRUE) {
echo "mail was sent ok";
} else {
echo "mail failed"
}
問題是mail()以前在Xampp上不起作用,但是自從我更新到Xampp 5.6.3(在Mac上)后,它突然起作用了。 但並非所有電子郵件都會收到它。 我在gmx.net上的郵件不接受,但與我的Webhotel連接的郵件地址接受了。
但是我使用phpmailer https://github.com/PHPMailer/PHPMailer發送我的郵件,因為當您發送大量電子郵件時,mail()會在每個呼叫中打開和關閉連接,但是phpmailer可以使用smtp(例如,您可以使用gmail,雖然速度很慢),所以您可以一次性發送很多郵件。 說,如果您要發送1000封郵件,mail()並不是一個好選擇。
編輯:使用phpmailer的示例。 (我的網絡酒店也有我可以使用的smtp服務器。我只需要詢問他們並獲取其設置和端口號。在我的網絡酒店上,沒有登錄要求,但發送的電子郵件應具有連接到位於的網絡酒店的電子郵件地址發件人字段,並且無法在本地使用,因此gmail是更好的選擇,盡管身份驗證使其以這種方式發送電子郵件很慢)
require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer();
$body = file_get_contents('contents.html');
$body = eregi_replace("[\]",'',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "yourusername@gmail.com"; // GMAIL username
$mail->Password = "yourpassword"; // GMAIL password
$mail->SetFrom('name@yourdomain.com', 'First Last');
$mail->AddReplyTo("name@yourdomain.com","First Last");
$mail->Subject = "PHPMailer Test Subject via smtp (Gmail), basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "whoto@otherdomain.com";
$mail->AddAddress($address, "John Doe");
$mail->AddAttachment("images/phpmailer.gif"); // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.